views:

742

answers:

2

Hi, I am unable to load my test.dll (in VC++ - MFC) using LoadLibrary() API. I get error code 126 (Module not found)using GetLastError(). By dependency walker I have come to know that my test.dll depends on "xerces-c_2_7.dll" and "Xalan-C_1_10.dll". These dlls were already present on the same path as the exe. Still I am getting the error. So I tried to load both the above mentioned third party dlls using LoadLibrary() which returned handle as 0x10000000. By GetLastError() I am getting

error code 6 (Invalid Handle).

Can anyone please guide me on why I am getting the Invalid Handle error?

Below is the code snipet:

HINSTANCE hLib = LoadLibrary(_T("Xalan-C_1_10"));
TCHAR szMessage[MAX_PATH];
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
 FORMAT_MESSAGE_FROM_SYSTEM,
 NULL, GetLastError(),
 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
 szMessage, MAX_PATH, NULL);
 hLib = LoadLibrary(_T("xerces-c_2_7"));
FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS|
 FORMAT_MESSAGE_FROM_SYSTEM,
 NULL, GetLastError(),
 MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
 szMessage, MAX_PATH, NULL);
+1  A: 

Distilling your problem, I gather:

  • You are able to load Xerces and Xalan individually, but not the test.dll file (which references the other dlls)

  • You may be misled by the value GetLastError() returns when loading either Xerces or Xalan

Remember that you must call GetLastError() immediately after the LoadLibrary call: From MSDN docs:

You should call the GetLastError function immediately when a function's return value indicates that such a call will return useful data. That is because some functions call SetLastError with a zero when they succeed, wiping out the error code set by the most recently failed function.

This is most probably a dependency/path resolution issue (i.e. LoadLibrary can't find the requested image). So, you have two options:

  • Does test.dll/Xerces/Xalan have other dependencies that you have not copied to your executable's folder?

  • Put the dll in Windows\System32 folder and try again. If this works, then you can be sure you had hit a path problem. Read the MSDN page on LoadLibrary -- it's a bit confusing but has some details that's easy to leave out.

  • Use GetCurrentDirectory and SetCurrentDirectory to switch to and from the application's executing folder and the dll's residing folder

  • Specify the full path to the dll in the call to LoadLibrary. You may face issues if the path name contains whitespace (this I recall from memory, please check with MSDN).

  • Once done, be a good citizen and call FreeLibrary!

dirkgently
A: 

Is test.dll your DLL or third party DLL?

If it is a your DLL, 

you should be easily able to see the depent DLLs in the code & its exact path.

If it is third party DLL,

you need to try windows path, program files path, your test DLL path etc... Remember it is only a try. Sometimes they may refer to the path which is only known to the creator the DLL if not documented)

Hope this answer helps!

AKN