-
April 16th, 2010, 02:16 PM
#1
calling function in dll not working
I'm trying to call a function in a dll. I created this dll myself too. When i run my code i get am 'Exception unhandled' error. Can someone please tell me what i'm doing wrong?
In my exe file i basically do this:
Code:
typedef int (__stdcall * voidFunc)(int);
//in my main function
dllEngine = LoadLibrary ( L"test_engine.dll" );
if ( dllEngine == 0 )
MessageBoxW ( NULL, L"failed loading DLL", L"ERROR", MB_OK );
testfunc = (voidFunc)GetProcAddress ( dllEngine, "testfunc" );
int x = testfunc(5);
This is the relevant code in my dll
Code:
extern int testfunc ( int x );
int testfunc (int x)
{
MessageBoxW ( NULL, L"test", L"blaat", MB_OK );
return x;
}
-
April 16th, 2010, 02:50 PM
#2
Re: calling function in dll not working
Are you checking GetProcAddress's return? I'm guessing that without "extern C" your getting a very different name exported than the "testfunc" you're expecting.
-
April 16th, 2010, 03:10 PM
#3
Re: calling function in dll not working
Besides the name mangling issue, ain't you see an inconsistency in the declarations?
Code:
typedef int (__stdcall * voidFunc)(int);
extern int testfunc ( int x );
Best regards,
Igor
-
April 16th, 2010, 03:33 PM
#4
Re: calling function in dll not working
Originally Posted by Igor Vartanov
Besides the name mangling issue, ain't you see an inconsistency in the declarations?
I don't know how i got that mixed up, but i changed it. But it's still not working.
Code:
typedef int (* testfunc)(int);
//in my main function
testfunc bla(0);
dllEngine = LoadLibrary ( L"test_engine.dll" );
if ( dllEngine == 0 )
MessageBoxW ( NULL, L"failed loading DLL", L"ERROR", MB_OK );
bla = (testfunc)GetProcAddress ( dllEngine, "testfunc" );
int x = bla(5);
The DLL file
Code:
extern int testfunc ( int x );
int testfunc (int x)
{
MessageBoxW ( NULL, L"test", L"blaat", MB_OK );
return x;
}
-
April 16th, 2010, 03:36 PM
#5
Re: calling function in dll not working
Are you sure "testfunc" exists in your dll? You're not checking the return from GetProcAddress()!
-
April 16th, 2010, 03:57 PM
#6
Re: calling function in dll not working
Originally Posted by Igor Vartanov
Besides the name mangling issue...
extern int testfunc ( int x );
As far as I understand, you compile it as C++. So this must be the actual export name of your testfunc:
008 00000000 SECT3 notype () External | ?testfunc@@YAHH@Z (int __cdecl testfunc(int))
Last edited by Igor Vartanov; April 16th, 2010 at 04:01 PM.
Best regards,
Igor
-
April 16th, 2010, 04:18 PM
#7
Re: calling function in dll not working
@hoxsiew
The return value of GetProcAddress is 0x00000000. So for some reason it can't find it...
@Igor Vartanov
You mean that the function name in my dll should also have 'extern' infront of it? Anyway, i tried it but also didn't work.
I'll post everything i have so far. Perhaps you can see where i go wrong.
exe file
Code:
#include <windows.h>
typedef int (* testfunc)(int);
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
MSG msg;
HMODULE dllEngine;
testfunc bla(0);
dllEngine = LoadLibrary ( L"test_engine.dll" );
if ( dllEngine == 0 )
MessageBoxW ( NULL, L"failed loading DLL", L"ERROR", MB_OK );
bla = (testfunc)GetProcAddress ( dllEngine, "testfunc" );
int x = bla(5);
while ( GetMessage ( &msg, NULL, 0, 0 ) > 0 )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
return msg.wParam;
}
DLL file
Code:
#include <windows.h>
extern int testfunc ( int x );
BOOL WINAPI DllMain ( HINSTANCE hInstance, DWORD fwdReason, LPVOID lpvReserved )
{
switch(fwdReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_DETACH:
break;
}
return(TRUE);
}
extern int testfunc (int x)
{
MessageBoxW ( NULL, L"test", L"blaat", MB_OK );
return x;
}
-
April 16th, 2010, 04:20 PM
#8
Re: calling function in dll not working
Try:
Code:
extern "C" int testfunc ( int x );
Viggy
-
April 16th, 2010, 04:25 PM
#9
Re: calling function in dll not working
Originally Posted by MrViggy
Try:
Code:
extern "C" int testfunc ( int x );
Viggy
Nope, didn't work either
-
April 16th, 2010, 04:34 PM
#10
Re: calling function in dll not working
You mean that the function name in my dll should also have 'extern' infront of it? Anyway, i tried it but also didn't work.
Actually I meant you have to use the actual export name while finding the proc addess. This is what hoxiew told you about from the beginning: before resolving the function name to its address you make sure you use correct export name.
Export name may be not same to function name, in general case.
Code:
bla = (testfunc)GetProcAddress ( dllEngine, "?testfunc@@YAHH@Z" );
Best regards,
Igor
-
April 16th, 2010, 04:37 PM
#11
Re: calling function in dll not working
Originally Posted by Igor Vartanov
Actually I meant you have to use the actual export name while finding the proc addess. This is what hoxiew told you about from the beginning: before resolving the function name to its address you make sure you use correct export name.
Export name is not same to function name, in general case.
Code:
bla = (testfunc)GetProcAddress ( dllEngine, "?testfunc@@YAHH@Z" );
I see what you mean, but that's how C++ export function names. But since i'm using extern "C" now i avoid that name mangling, right..?
-
April 16th, 2010, 04:40 PM
#12
Re: calling function in dll not working
I believe it needs to be:
Loader:
Code:
typedef int (__stdcall * voidFunc)(int);
DLL:
Code:
extern "C" int __stdcall testfunc(int x)
{
//...
}
If the function call isn't __stdcall, the name still gets mangled, even if you use extern "C". I remember this because I spent days trying to find the problem for the exact same reason.
-
April 16th, 2010, 04:44 PM
#13
Re: calling function in dll not working
Originally Posted by vivendi
Nope, didn't work either
Say you've built your dll alright. What is the export name inside? Did you ever hear about dumpbin.exe? depends.exe?
Best regards,
Igor
-
April 16th, 2010, 04:48 PM
#14
Re: calling function in dll not working
Originally Posted by Night_Wulfe
I believe it needs to be:
. . .
DLL:
Code:
extern "C" int __stdcall testfunc(int x)
This will produce _testfunc@4 export. So, it won't work, again.
Best regards,
Igor
-
April 16th, 2010, 04:54 PM
#15
Re: calling function in dll not working
I wonder why LoadLibrary is used with such a persistence? Ain't it easier to just build a client with .lib file and forget about the name mangling nightmare?
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|