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;
}
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.
Re: calling function in dll not working
Besides the name mangling issue, ain't you see an inconsistency in the declarations?
Quote:
Code:
typedef int (__stdcall * voidFunc)(int);
extern int testfunc ( int x );
Re: calling function in dll not working
Quote:
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;
}
Re: calling function in dll not working
Are you sure "testfunc" exists in your dll? You're not checking the return from GetProcAddress()!
Re: calling function in dll not working
Quote:
Originally Posted by Igor Vartanov
Besides the name mangling issue...
Quote:
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:
Quote:
008 00000000 SECT3 notype () External | ?testfunc@@YAHH@Z (int __cdecl testfunc(int))
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;
}
Re: calling function in dll not working
Try:
Code:
extern "C" int testfunc ( int x );
Viggy
Re: calling function in dll not working
Quote:
Originally Posted by
MrViggy
Try:
Code:
extern "C" int testfunc ( int x );
Viggy
Nope, didn't work either :(
Re: calling function in dll not working
Quote:
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. :D
Export name may be not same to function name, in general case.
Code:
bla = (testfunc)GetProcAddress ( dllEngine, "?testfunc@@YAHH@Z" );
Re: calling function in dll not working
Quote:
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. :D
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..?
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.
Re: calling function in dll not working
Quote:
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?
Re: calling function in dll not working
Quote:
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. :)
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?
Re: calling function in dll not working
A lib file would be easier, but in my case i really need a dll.
When i used dumpbin on my dll it gave me the following output:
dumpbin \EXPORTS path\to\file.dll
File Type: DLL
Summary
1000 .data
1000 .idata
2000 .rdata
1000 .reloc
1000 .rsrc
4000 .text
10000 .textbss
Re: calling function in dll not working
Got it fixed. I had to use declspec(export). This is how it looks right now:
Code:
extern "C" __declspec(dllexport) int testfunc ( int x );
..
..
int testfunc (int x)
{
MessageBoxW ( NULL, L"test", L"blaat", MB_OK );
return x;
}
Thanks alot guys for your help!
Re: calling function in dll not working
Quote:
A lib file would be easier, but in my case i really need a dll.
You know, when you build a dll with export, the lib file is created as well. The one is called DLL's import library. Just search around for simple dll samples. :)