Sign in to follow this  
ForgottenMaster

DLL Not Found?

Recommended Posts

Hi guys, I am in the process of converting one of my linux applications to windows, as part of the application, I require loading of DLLs at run time from a set folder. I have not been able to get this to work as LoadLibrary keeps failing with error code 126....after looking this up, it seems to mean that a dependency is missing. I have created a very small program in order to see if I can get loading of DLLs at runtime working, and yet, it still does not work, even with the very small number of dependencies (I have used depends.exe), all of which exist on the system I dont know if there is a problem with my code, so I would appreciate you taking a look ------------------------------------TestDLL/Main.h------------------------------ #ifndef MAIN_H #define MAIN_H class __declspec(dllexport) Something { public: int add( int one, int two ); }; #endif -----------------------------------TestDLL/Main.cpp---------------------------- #include "Main.h" #include <windows.h> BOOL APIENTRY DllMain(HANDLE, DWORD, LPVOID) { return TRUE; } int Something::add( int one, int two ) { return ( one + two ); } -----------------------------------UseDLL/Driver.cpp--------------------------- #include <windows.h> #include <iostream> int main( int argc, char **argv ) { // load the library HINSTANCE hinst = LoadLibrary( LPCWSTR( "TestDLL.dll" ) ); if( hinst == NULL ) { std::cout << "noooooooooo" << std::endl; std::cout << GetLastError( ) << std::endl; } char c; std::cin >> c; return 0; } ------------------------------------------------------------------------------- TestDLL is the project building the very simple DLL, and UseDLL will use it. I have checked that TestDLL.dll is in the same directory as UseDLL.exe, and I have also run depends on TestDLL.dll which gives the following: KERNEL32.DLL MSVCR90.DLL NTDLL.DLL TESTDLL.DLL I have checked that these all exist, and they do, in directories on the PATH I am not sure what else the problem could be here to be honest, my application works perfectly under linux, but I have not had much experience building DLLs in windows, so it may be a problem with the way I have done it that causes the LoadLibrary function to fail with that error code Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by ForgottenMaster
I have not been able to get this to work as LoadLibrary keeps failing with error code 126....after looking this up, it seems to mean that a dependency is missing.
Error 126 actually means "The specified module could not be found. "

Quote:
Original post by ForgottenMaster

// load the library
HINSTANCE hinst = LoadLibrary( LPCWSTR( "TestDLL.dll" ) );
Read This. You can't do that any more than you can do:

float f = 123.456;
LPCWSTR s = LPCWSTR(f);

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
You can't do that any more than you can do:

<nitpick>Bad phrasing here. Since his code will compile and your example won't, he can, in fact, do that more than you could do a floating point to pointer cast. :)</nitpick>

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by Evil Steve
You can't do that any more than you can do:

<nitpick>Bad phrasing here. Since his code will compile and your example won't, he can, in fact, do that more than you could do a floating point to pointer cast. :)</nitpick>
Ah yes - somewhere in my brain I thought that'd compile, obviously not. Ok, pretend f is a pointer to a float then [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by ForgottenMaster
HINSTANCE hinst = LoadLibrary( LPCWSTR( "TestDLL.dll" ) );


A typical approach to dealing with string literals is to wrap them in the TEXT macro.

HINSTANCE hinst = LoadLibrary( TEXT( "TestDLL.dll" ) );

When compiling with UNICODE defined, the TEXT macro prefixes an "L" to the string, which tells the compiler to treat the string as UNICODE. When compiling with UNICODE not defined, the TEXT macro leaves the string as is, so the compiler treats the string as ANSI. The TEXT macro is defined in winnt.h, which should automatically be included in your project when windows.h is included.

One thing to note. LoadLibrary is usually used in conjunction with GetProcAddress, which expects an ANSI string argument. So don't wrap that argument with TEXT.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this