CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Nov 2005
    Posts
    102

    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;
    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    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.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  4. #4
    Join Date
    Nov 2005
    Posts
    102

    Re: calling function in dll not working

    Quote Originally Posted by Igor Vartanov View Post
    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;
    }

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: calling function in dll not working

    Are you sure "testfunc" exists in your dll? You're not checking the return from GetProcAddress()!

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: calling function in dll not working

    Quote 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

  7. #7
    Join Date
    Nov 2005
    Posts
    102

    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;
    }

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640

    Re: calling function in dll not working

    Try:
    Code:
    extern "C" int testfunc ( int x );
    Viggy

  9. #9
    Join Date
    Nov 2005
    Posts
    102

    Re: calling function in dll not working

    Quote Originally Posted by MrViggy View Post
    Try:
    Code:
    extern "C" int testfunc ( int x );
    Viggy
    Nope, didn't work either

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  11. #11
    Join Date
    Nov 2005
    Posts
    102

    Re: calling function in dll not working

    Quote Originally Posted by Igor Vartanov View Post
    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..?

  12. #12
    Join Date
    Apr 2010
    Location
    Dayton, OH
    Posts
    16

    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.

  13. #13
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: calling function in dll not working

    Quote Originally Posted by vivendi View Post
    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

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Best regards,
    Igor

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured