Skip to main content
gamedev.net
🔒 Locked

DLLs

Started by A_dams Oct 27, 2002 at 11:35 AM 5 replies 250+ views
Original Post
A_dams
A_dams
hello! I''ve got a problem with getting address of procedure in dlls. When I''m trying to get address of function in specified dll, the program crashes, and I don''t know why. Below I present the code: -------- dll.cpp: -------- dll_export int AddTwo(int a, int b) { return a+b; } -------- -------- dll.h: -------- #define dll_export __declspec(dllexport) extern dll_export int AddTwo(int a, int b); -------- dll_main - program which reads the address of dll''s function: ------------- dll_main.cpp: ------------- typedef int (CALLBACK* LPFNADD) (int, int); int main(int argc, char* argv[]) { HINSTANCE hDll = LoadLibrary("dll.dll"); LPFNADD add = (LPFNADD) GetProcAddress(hDll, "AddTwo"); if(!add) { printf("Error"); FreeLibrary(hDll); return 0; } else { printf("%d", add(2,4)); } FreeLibrary(hDll); return 0; } Does anybody know what''s wrong with the code? I would be grateful for any help.
niyaw
niyaw
quote:
Original post by A_dams
When I''m trying to get address of function in specified dll, the program crashes, and I don''t know why.

GetProcAddress crashes? are you sure the dll is actually loaded?
A_dams
A_dams
Sorry, I didn''t describe it clearly. So, when I''m trying to get the address of the function in my DLL, the program returns error code 127. As you may know it says that the function wasn''t found in the DLL.
Do you know what to do?
niyaw
niyaw
put an extern "C" on the exported function to prevent name mangling.
A_dams
A_dams
yea, it works. thanks!
niyaw
niyaw
suppose you have two functions:

int add(int a, int b);
float add(float a, float b);

exported from your dll. if you then call GetProcAddress("add"), which function do you want? the compiler distinguishes between those by mangling their name; for instance, on msvc7, the above functions become

?add@@YAHHH@Z
?add@@YAMMM@Z

respectively. the process is transparent so long as you don''t need to know the actual function names; however, for dll importing via GetProcAddress, you do. extern "C" disables name mangling, so the add function is exported as

add

and GetProcAddress("add") works. obviously, you cannot have two extern "C" functions with the same name, because the compiler won''t be able to tell one from another.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.