# DLL... Can someone explain?

## Recommended Posts

beebs1    398
Hiya, I've been experimenting with exporting functions from C++ in different ways, and I was wondering if someone could explain something. Consider this exported factory function:
namespace test {
ITestInterface *CreateTestInterface();
}

When I export it using __declspec (dllexport), the function is exported as something like [i]?CreateTestInterface@test@@YGPAVITestInterface@12@XZ[i]. However, when I use the following module definition file, the function is exported as just CreateTestInterface.
EXPORTS
CreateTestInterface @1

Both work, but the second is much easier to use with the Windows LoadLibrary() function. My question is how does the code using the library know that the function will return a test::ITestInterface, if the exported name does not reflect that using name 'mangling'? Is the name decoration just used when you link with a DLL, but not when you use it with the Load/FreeLibrary functions? I hope that was clear enough! Many thanks for any explanation [smile]

##### Share on other sites
Ra    1062
Quote:
 Original post by beebs1My question is how does the code using the library know that the function will return a test::ITestInterface, if the exported name does not reflect that using name 'mangling'?

It doesn't. LoadLibrary() just gets the address of the function and does not care about the parameters. No parameter information is stored at all (apart from the size of the parameters on the stack for stdcall calls).

##### Share on other sites
SiCrane    11839
If you use LoadLibrary() and GetProcAddress() you need to cast the pointer to a function pointer type and pray that the type that you've cast it to matches the type that it actually is. There are no guarantees.

##### Share on other sites
beebs1    398
Ah, I see. Thanks for the replies!

Would you say it's better to link implicitly with the library then, instead of using Load/FreeLibrary?

I've noticed that some large engines though, notably the new CryEngine 2, look like they use a module definition file. Can it not be used safely?

Thanks again.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account