Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
hi,all
I'm new to DLL programming in VC++ and have a question that bothers me for a long time.
Here is the situation:I created a DLL, like test.dll, with only one cpp file, test.cpp, in that project. in that .cpp file, i have only defined one function named void show(), and I put __declspec(dllexport) behind "void", make the definition looks like this:
void __declspec(dllexport) show()
{
   cout << "I am a DLL, and I'm called by you now!!" << endl;
}

After compiling this project, I got a test.dll file.
The question now is how to call that function dynamically?
I can get the handle of that dll using LoadLibrary, but can't get the address of that function using GetProcAddress().
And of course I have all the needed header files included.
Did I miss something? or I did something wrong?
Any answers or suggestions will be appreciated.
Thanks.
Posted
Updated 18-May-11 13:46pm
v3

1 solution

This is merely a problem of name mangling. You exported name is not "show". To see what is actually exported, it's good to use some binary dump tool, such as DUMPBIN.EXE (see http://msdn.microsoft.com/en-us/library/c1h23y6c(v=VS.100).aspx[^]). It is bundled with every version of Visual Studio; you can run it using "Visual Studio Command Prompt". It will show you the mangled name you should pass to GetProcAddress (see http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx[^]).

To avoid name mangling, you can use extern "C":

C++
#ifdef __cplusplus
extern "C" {  // only need to export C interface if
              // used by C++ source code
    void __declspec(dllexport) show() { /*...*/ }
#endif


Name mangling
or name decoration is the C++ feature helping developers to pretend they are working in type-safe environment, where the signatures of functions are checked. In real life, as it is all based on the gravely obsolete object file name-based linkage, this is no more than a dirty trick. Real type safety between executable modules was invented and used decades ago. In modern time it exists, for example, in the form of .NET meta-data.

For explanation of name mangling, see http://en.wikipedia.org/wiki/Name_mangling[^].

—SA
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 18-May-11 21:00pm    
OP commented:
Thank SAKryukov, your answer is important to me!!!
Sergey Alexandrovich Kryukov 18-May-11 21:01pm    
You're welcome.
If it's useful (and it certainly works), please formally accept this answer (green button).
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900