CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Posts
    4

    Unhappy Newbie to DLLs... DLL not loading using LoadLibrary

    I'm hoping someone will take the time to help me out on this. If you're that person, thank you!!

    I'm trying to learn how to use DLLs, particularly how to leverage already existing DLLS. I followed the DLL beginner tutorial on this site, but had a few issues with it. I was able to resolve most of them, but when i go to load my DLL using LoadLibrary, it doesn't work. At first it had a problem converting the argument, so i tried LoadLibraryA. That compiled, but didn't load the library. Then i went back to LoadLibrary and cast the filename as (WCHAR*). Again, compiled fine, but didn't load the DLL. Stepping through the program, it just puts 0 in the variable LoadLibrary is assigned to.

    I thought it might be a path issue, but i've put it in the defined path, in the debug folder, hardcoded the path, and still, nothing. I'm not sure what it is i'm missing, and could really use the help of someone more experienced!

    Thanks!
    Here is my code:

    Code:
    #include <iostream>
    #include <windows.h>
    
    typedef int (*AddFunc)(int,int);
    typedef void (*FunctionFunc)();
    
    int main()
    {
    	AddFunc _AddFunc;
    	FunctionFunc _FunctionFunc;
    	HINSTANCE hInstLibrary = LoadLibrary((WCHAR*)"DLL_Tutorial.dll");
    
    	if (hInstLibrary)
    	{
    		_AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add");
    		_FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary, "Function");
    
    		if (_AddFunc)
    		{
    			std::cout << "23 + 43 = " << _AddFunc(23,43) << std::endl;
    		}
    		if (_FunctionFunc)
    		{
    			_FunctionFunc();
    		}
    
    		FreeLibrary(hInstLibrary);
    	}
    	else
    	{
    		std::cout << "DLL Failed to Load!" << std::endl;
    	}
    	std::cin.get();
    
    	return 0;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Quote Originally Posted by cyphen View Post
    I'm trying to learn how to use DLLs, particularly how to leverage already existing DLLS. I followed the DLL beginner tutorial on this site, but had a few issues with it.
    I guess that tutorial didn't teach you to call GetLastError if LoadLibrary fails:

    http://msdn.microsoft.com/en-us/library/ms886736.aspx
    Return Values

    A handle to the module indicates success. NULL indicates failure. To get extended error information, call GetLastError.
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Second, you don't cast from one char type to another:
    Code:
    HINSTANCE hInstLibrary = LoadLibrary((WCHAR*)"DLL_Tutorial.dll");
    Never do this. You cannot just change from one string type to another by applying a C-style cast. String types must be converted from one type to another using WideCharToMultiByte() or MultiByteToWideChar() functions.

    Lucky for you, the string is a string literal, therefore it must start out as the correct type. To ensure the literal is a wide character, the literal must be a wide string literal.
    Code:
    HINSTANCE hInstLibrary = LoadLibrary( _T("DLL_Tutorial.dll"));
    I guess your tutorial needs a tutorial. Note the usage of the _T() macro (you probably also need to #include <tchar.h>).

    Then i went back to LoadLibrary and cast the filename as (WCHAR*). Again, compiled fine
    When you use a C-style cast, you are telling the compiler to "shut-up". If you removed that WCHAR* cast, you would see that the code would not compile. Once the code did not compile, that is a clear indication that something is wrong -- you never just apply a cast to keep the compiler quiet, unless you know exactly what you're doing.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 18th, 2012 at 09:00 PM.

  4. #4
    Join Date
    Jun 2012
    Posts
    4

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Thanks Paul. I set the project character set to not set, rather than unicode, and LoadLibrary compiles fine without casting. Basically the same thing as LoadLibraryA now.

    Anyway, GetLastError returns 126, which looks like it's having trouble finding something. "The specified module could not be found." I've read a few things that mention that it could be a dependency, so i used dependency walker, and everything appears to be standard window DLLs that show up with no problems. I'm really scratching my head on this one!

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

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Code:
    HINSTANCE hInstLibrary = LoadLibrary( _T("DLL_Tutorial.dll"));
    Dynamic-Link Library Search Order
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Quote Originally Posted by cyphen View Post
    Thanks Paul. I set the project character set to not set, rather than unicode, and LoadLibrary compiles fine without casting. Basically the same thing as LoadLibraryA now.
    But you're missing the general point.

    Regardless of the character set used, the code I gave you is correct. The _T() (or TEXT()) macro automatically sets string literals to either ANSI or wide, depending on the build type. Using "naked" string literals (without the _T() macro) as your original code did was the actual problem.
    Anyway, GetLastError returns 126, which looks like it's having trouble finding something. "The specified module could not be found."
    Why not specify the full path of the module in the LoadLibrary() call? If you then get LoadLibrary returning non-NULL, then the reason is obvious, as Igor pointed out.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2012
    Posts
    4

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    Thanks Paul and Igor! I appreciate the tip on using _T, as well! I'm sure that will come in very handy. I should have been more thorough in my response, and let you know that i had tried a bunch of different things. But now i know to use the _T macro. I had previously used the literal path as well. That said...

    [Facepalm]
    After going through it again, i noticed a problem in the actual DLL code i created (from the tutorial - but i should have noticed as i was doing the tutorial.) The tutorial uses a header file and alternates using _declspec(dllimport) and _declspec(dllexport) depending on if DLLEXPORT is defined. In the .cpp program, the header was included prior to DLLEXPORT being defined, so the DLL wasn't exporting the functions! Changed that, and it worked fine.
    [/Facepalm]

    I appreciate the prompt help - and it wasn't at all for naught - i learned something new - so thanks!

  8. #8
    Join Date
    Jun 2012
    Posts
    4

    Re: Newbie to DLLs... DLL not loading using LoadLibrary

    okay - sorry, i built this project separately on two systems - my laptop at work, and my home system - so i was actually running into separate problems. I just wanted to post this in case other people read it and got confused. I've just confirmed that my problem on one system was actually having no underscore in the name of my DLL, but in my code, it had an underscore.

    What i just verified after fixing this error is that - while my post above is true, and the #define should have come before the #include, when it didn't and the DLL was created using _declspec(dllimport) in the header with the function definitions, but not in the cpp file. The DLL still worked properly from the calling program for some reason. I'm not exactly sure why... I would think a function that wasn't exported wouldn't be available.

Tags for this Thread

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