Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoadLibrary fails to load dll with error code 126, when building in Visual Studio

This project consists of a single .cpp file which calls LoadLibrary() to load a dll-file.

What happens is that if I run the following through the Microsoft Visual Studio 2012 Command Prompt:

cl /nologo Test.cpp

Everything works fine.

On the other hand, if I start up a simple Visual C++ project, add the file and compile it through there, the LoadLibrary() call fails with code 126: The specified module could not be found.

The .exe I build from the command line is more than twice the size of the one built in Visual Studio. So I guess there's some static linking going on.

The .dll-file is fine, and it's in the same directory as the .exe-file.

like image 735
Hans Sjunnesson Avatar asked Oct 04 '12 20:10

Hans Sjunnesson


People also ask

How do I fix LoadLibrary failed with error 126?

You may encounter the loadlibrary failed with error 126 when the problematic application does not have the privileges to access a protected system resource. In this case, launching the problematic application as an administrator may solve the problem.

What DLL is LoadLibrary?

Kernel32. dll is loaded into every Windows process, and within it is a useful function called LoadLibrary .

Can not load DLL?

If a DLL fails to load, there can be numerous reasons for the error, listed below are the most common. The DLL or referenced DLL is not in a directory specified in the path. The DLL references another DLL that is not present. Hard disk error has corrupted or damaged a DLL file.


1 Answers

I figured it out.

By default the cl command uses the multibyte character set. While new projects set up in Visual Studio are configured for unicode.

like image 74
Hans Sjunnesson Avatar answered Sep 21 '22 04:09

Hans Sjunnesson