#include <stdio.h>
__declspec(dllexport) void DisplayHelloFromDLL()
{
printf ("Hello from DLL !\n");
}
This above code has been written using VS 2005. I opened this project as a win32 application and chose the option to build a dll and not a exe. The name of the project is DLLProctTest and the name of the file in which I have written the above code is DLLProctTest.c
I built the above project and I got the dll and the lib in the debug directory. The contents of the debug directory of the project contains these files - DllProctTest.pdb, DllProctTest.lib, DllProctTest.exp, DllProctTest.ilk, DllProctTest.dll
Now I created another new project which is again a win32 application called DLLProctTest and I chose the option to create a exe. In this new project I created a c file called DLLProctTest.c whose content is
#include "windows.h"
int main()
{
DWORD err;
HINSTANCE hDLL = LoadLibrary("DllProctTest.dll"); // Handle to DLL
if(hDLL != NULL)
{
printf("Library has been loaded\n");
}
else
{
err = GetLastError();
printf("Couldn't load dll\n");
}
DisplayHelloFromDLL();
return 0;
}
I took the DLLProctTest.dll and DLLProctTest.lib from the first project and pasted them in the second project DLLProctTest1 in the folder which contains this above c file DLLProctTest1.c
Following this I added the DLLProctTest.lib to my project DLLProctTest1 and I built my code. The code compile without a hitch. I am able to call the function from the .dll file, but I am not able to load the library using LoadLibrary. This is the output I am getting.
output:
Couldn't load dll. Error code: 126
Hello from DLL !
The reason I am calling LoadLibrary is that I want to call GetProcAddress( ) next. But without the library getting loaded I can't get the address of the loaded function.
Another thing is the error code. We get the error code 126 when the library can be located, but it can't be loaded for some reason. Any help on this is deeply appreciated