0

We are trying to load a dll-library from inside a 64-bit dll using LoadLibraryA function. It returns 126 error - mod not found. The dll file path given to the function is correct, we are sure.

We have tried a dummy dll to test and it worked, it is loaded.

We also tried adding the dll (which is a dependcy of the first dll that we want to load) to the dummy dll. It also worked. So the problem seems not about the dependency dlls, but the original dll that we want to load in the first place.

We also tried converting the dl to 64-bit, and tried that, still no good.

We also checked the dependencies with Dependency Walker. Everything is OK.

The operating system that we are using is Windows 8, 64bit. If it makes any difference.. Does anyone have any idea about this poblem?

EDIT: We also tried this code:

hModule = LoadLibraryW(L"C:\\temp\\dllToLoad.dll");

and received this error code:

"First-chance exception at 0x00000000 in C_Test_TSMPPKCS11.exe: 0xC0000005: Access violation at location 0x0000000000000000."

EDIT 2: The code that we used in the first place is:

hModule = LoadLibraryA((char*)aDLLFile);

EDIT 3: We are using complete path to load the dll. In order to test this we tried this code:

FILE *fp;
    int size = 0;
    fp=fopen("C:\\temp\\dllToLoad.dll", "r");
    size = fgetc(fp);
    printf("size:%d\n",size);
    fclose(fp);

There was no problem, we received the file size which is 77.

lamostreta
  • 2,359
  • 7
  • 44
  • 61
  • 2
    If you don't get a working answer here, look up "loader snaps", which are a mechanism for debugging this kind of problem. – RichieHindle Nov 15 '13 at 12:54
  • 2
    (Minor point: `fgetc` gets a byte from the file; it doesn't get the size. Not that that changes anything.) – RichieHindle Nov 15 '13 at 14:02

1 Answers1

5

We also tried converting the dl to 64-bit, and tried that, still no good.

There's no way you can load a 32-bit dll as executable code into a 64-bit process (and since you're using LoadLibraryA() that's all you can be trying to do).

Assuming the dll that you are trying to load and the process that you're loading it into are the same type then are you passing a complete path to LoadLibraryA() or a relative path or just a dll name? If you're not using a complete path then consider using LoadLibraryEx() if possible as this gives you much more control over the search path used. If you are using a complete path try opening the file using normal file operations if you fail to load the dll, does this work? If that works then try LoadLibraryEX() with LOAD_LIBRARY_AS_DATAFILE and see if that will load the dll as a simple data file (which proves that it's finding the file).

Run up Sysinternal's ProcMon and watch the code open the DLL, that may show you dependent DLL load failures.

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • Hi, we are using complete path to load dll file. Please check Edit 3. – lamostreta Nov 15 '13 at 14:22
  • That's simply a wrong statement (first sentence), given the wording. You *can* load a 32bit DLL into a 64bit process and vice versa. As you rightly point out `LOAD_LIBRARY_AS_DATAFILE` can load a DLL in order to use its resources or parse it or whatever you fancy. – 0xC0000022L Nov 15 '13 at 15:41
  • 1
    The question states that they're using LoadLibraryA() which only loads a dll as executable code. – Len Holgate Nov 15 '13 at 16:06
  • we tried LoadLibraryEX() with LOAD_LIBRARY_AS_DATAFILE, and it worked. – lamostreta Nov 18 '13 at 11:53
  • but when we tried it with NULL instead of LOAD_LIBRARY_AS_DATAFILE we received an exception. – lamostreta Nov 18 '13 at 12:04
  • 1
    In which case I refer you to my question about the build of the DLL. IS THE DLL x86 or x64??? – Len Holgate Nov 18 '13 at 12:05
  • Sorry, I think I don't get your question. In the second comment I wrote: "yes, dll is x64" with this I meant the build of the DLL is x64. Is this what you are asking for? – lamostreta Nov 18 '13 at 12:09
  • 1
    Sorry hadn't seen that. You shouldn't be getting an access violation. You should be able to debug that if you have symbols for both exe and dll. Perhaps your dll is failing to load because it does something bad in DLL main or as part of static initialisation. – Len Holgate Nov 18 '13 at 14:02
  • We do not have some kind of main function in dll, instead we are exporting a few functions in the main cpp. – lamostreta Nov 18 '13 at 15:12
  • thanks for the process monitor reccommendation. We couldn't see a missing dll, instead we found a name not found status in a registry entry.. – lamostreta Nov 18 '13 at 15:14