Reputation: 11
I'm a something of a beginner in programming. Here's a snippet from a C Program I wrote for Windows, trying to use NCurses library, compiled with mingw32.
char NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
char *dllDirectory = malloc(strlen(NPath) + strlen("\\dlls") + 1);
strcpy(dllDirectory, NPath);
strcat(dllDirectory, "\\dlls");
SetDllDirectoryA(dllDirectory);
HINSTANCE hGetProcIDDLL = LoadLibrary("libncursesw6.dll");
if (!hGetProcIDDLL) {
printf("libncursesw6.dll could not be loaded!");
return -1;
} else {
printf("loaded libncursesw6.dll\n");
}
I tried to load the libncursesw6.dll from the dlls subfolder, from the same folder of the executable. The above code works perfectly and displays, the dll is loaded.
But when I try to use use the library functions, I get an error box telling me libncursesw6.dll was not found. The program then works fine if is place the dll with the executable (which is what I'm trying to avoid).
When the following lines are added after the previous snippet, I get a run time error.
initscr();
addstr("Hello World");
refresh();
getch();
endwin();
I've included the necessary header files and got no compilation errors or warnings.
Am I doing something wrong??
Upvotes: 1
Views: 273
Reputation: 18420
The problem is, that you load the library after you have started the program, but if you use the functions in the code, the runtime linker, that tries to resolve unresolved symbols, needs to find the library before it passes control to the actual program.
So, you cannot do it this way. It would be possible to not use the ncurses
functions directly, but to define a bunch of function pointers and resolve the symbols you need manually with GetProcAddress
, but this is quite cumbersome.
For example like this:
void (*initscr)() = GetProcAddress(hGetProcIDDLL, "initscr");
Another possibility is, to link the ncurses
library statically, so it isn't needed at runtime.
Upvotes: 1