1
votes

I am having some troubles to load a DLL in a Windows console application I am coding for a class project.I don't know why it doesn't load the library file.

Before I call LoadLibrary where I have the error, I define this before main:

#include "lomo2.h"

and

HINSTANCE cargaDLL;

Also I have tried changing HINSTANCE to HMODULE type but it is useless.

After that in main I try to import the DLL that is located in main:

cargaDLL =  LoadLibrary("C:\\lomo2.dll");
if (cargaDLL == NULL){
    PERROR ("Error en la carga de la libreria\n");
    Sleep(2000);
    exit (1);
}

When I call LoadLibrary I have also tried to use a relative path like "LoadLibrary(.\my dll directory\lomo2.dll)" but it doesn't work at all.

The result is the message where it says there has been an error while linking the library, in PERROR.

Checking the error with GetLastError() it says it cannot find the specified module.

I hope you can help me to solve this problem. Thnaks.

3
When you call LoadLibrary and specify a relative path, remember that in Visual Studio, the working directory is the project's directory by default. And I guess you expect it to be the target directory (${TargetDir}). - cemdervis
Time to check on the GetLastError(). Windows will tell you the problem. - drescherjm
Turn on loader snaps. lomo2.dll may depend on another DLL, and that other DLL is missing. - Raymond Chen
try this tool dependencywalker.com and see what exactly is missing - Exceptyon
To enable loader snaps, open the Start menu and type gflags. Pick the appropriate bitness in case you get both x86 and x64 offered. The option Show loader snaps is on the System Registry tab. @Exceptyon: Dependency Walker is pretty much dead. It hasn't been updated in years, and fails more often than it helps. There are better tools around (e.g. DUMPBIN). - IInspectable

3 Answers

0
votes

I would use something like Process Monitor which will show you what the program is doing when it attempts to load the DLL.

I had a similar scenario occur with me and what was happening with me was that Windows did not find the DLL in the local location so it started looking through well known locations. One of these was to search the PATH, in my instance the PATH contains a location that was inaccessible, this caused the search to stop and the DLL not to load.

0
votes

Use the following code:

HMODULE cargaDLL;
cargaDLL =  LoadLibrary(L"C:\\lomo2.dll");

Always work with UNICODE as the type of the project. Using relative path is better for other reasons (using "c:\lomo.dll" will work). If you collaborate with other programmers on a project, it is better to assume all files will be placed in one folder and use relative path to this folder.

-1
votes

The problem was solved by changing the character set from "Unicode" to "No Set" in the Visual Studio project settings.

Thanks all of you for your answers.