MichaelMitchell
MichaelMitchell

Reputation: 1167

VS2013 - LoadLibrary cannot find dll

ISSUE: dll was compiled to 64-bit, program using dll was being compiled to 32-bit.

Solution: compiled the program using the 64-bit dll in 64-bit.


I have a dll that I made called mydll.dll. I have been able to compile the .cpp file outside of VS2013 and was able to load the dll. Once I try to make the same .cpp file in VS2013 and LoadLibrary(mydll.dll) with the dll in the same folder as my .cpp it cannot find the dll, and even if I path directly to the dll it still will not work.

My dll has been shown to work outside of VS2013 so I do not believe that is the source of the problem. The likely source is my ignorance.

Code:

(Main.cpp)

int main(void){
    HINSTANCE dllHandle;
    dllHandle = LoadLibrary("mydll.dll");
    if (!dllHandle){
        printf("dll no load\n");
        system("pause");
        return 1;
    }
    else{
        printf("dll load!\n");
    }
}

Upvotes: 1

Views: 6214

Answers (2)

drescherjm
drescherjm

Reputation: 10857

If your dll is in the search paths that windows is using make sure you are not mixing 32 bit and 64 bit. Windows will not load a 32 bit dll into a 64 bit application or a 64 bit dll into a 32 bit application.

Note: See the answer by @Nard for how windows searches for dlls: https://stackoverflow.com/a/26435819/487892)

Upvotes: 4

Nard
Nard

Reputation: 1006

Since the DLL is loaded when the program runs, you should either make sure the DLL path supplied to LoadLibrary is relative to the executable, or supply an absolute path. Refer to MSDN documentation on how DLLs are located when you do not provide an absolute path:

Dynamic-Link Library Search Order

The compiler will not be the one handling the loading of the DLL in this case so the cpp file does not need to locate the file but instead, the executable will need to locate the file.

Upvotes: 3

Related Questions