Doubt in DLL
-
Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:
-
Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:
Because your two first functions probably don't have any arguments. When you are using extern "C", it means that the function should be exported as a C function. In C, there is no function overloading so a function is identified only by its name. In C++, you can overload a function (a function with the same name can have different arguments). Which means that the name is not enough to identify the function. Thus, in C++ is introduced what is called name mangling: the types (and number) of parameters of the function are also used to identify the function. When you use GetProcAddress, you need to use the mangled name of the function (if there's no argument, the mangled name is the same as the function name). So, by using a C function, you avoid the name mangling. Hope it makes sense.
Cédric Moonen Software developer
Charting control [v1.4] -
Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:
__declspec(dllexport/dllimport) uses C++ naming convention, so that the name exported from the dll is name mangled with C++ language convention, so when you try to get the address of the exported function using GetProcAddress(), the function name string won't match with name of the exported function which is decorted with c++ style, when using extern "C" linkage specifier naming convention used is that of C language which has the same undecorated name as that of the function.
-
Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:
If you don't use
extern "C"
the name of the function is mangled theC++
way (you can see this effect viewing yourDLL
withDependecy Walker
). That means you need to pass the mangled name toGetProcAddress()
. On the other hand, usingextern "C"
makes the function haveC
linkage, hence, roughly speaking, without mangling. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
__declspec(dllexport/dllimport) uses C++ naming convention, so that the name exported from the dll is name mangled with C++ language convention, so when you try to get the address of the exported function using GetProcAddress(), the function name string won't match with name of the exported function which is decorted with c++ style, when using extern "C" linkage specifier naming convention used is that of C language which has the same undecorated name as that of the function.
Hi, It is very clear now. Thanks for your reply. :)
-
If you don't use
extern "C"
the name of the function is mangled theC++
way (you can see this effect viewing yourDLL
withDependecy Walker
). That means you need to pass the mangled name toGetProcAddress()
. On the other hand, usingextern "C"
makes the function haveC
linkage, hence, roughly speaking, without mangling. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeHI, I got the point. Thanks for your reply. :)
-
HI, I got the point. Thanks for your reply. :)
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Hi, I created a MFC Regualr DLL using shared MFC. DLL contains 4 functions which are exported using __declspec(dllexport). Now in the client application i tried to make use of this DLL. two of exported functions are imported using __declspec(dllimport). They are working fine. Now i tried to make use of third function using LoadLibrary and GetProcAddress. This time i am getting NULL pointer instead of function pointer. Again i placed extern "C" while exporting the function and tried to make use of it.This time i am getting the function pointer and every thing is fine. Usage of extern "C" makes it work fine. Can any one explain me what actually happening here?:confused: Thanks in advance...:rose:
one last solution to avoid using "extern C" is to put a .DEF file in your dll project, which tells to export a symbol as you name it in the file.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
one last solution to avoid using "extern C" is to put a .DEF file in your dll project, which tells to export a symbol as you name it in the file.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
Hi, Thaks for your solution. :)