#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

Remove the DllProctTest.lib from the DLLProctTest1 project -- load library is probably failing because it has already been statically linked to the *.exe program (just guessing). Also, the dll must be in one of the directories in the PATH statement or the current program's working directory. You might have to move the dll somewhere else.

You call functions that are loaded like you do in line 18 of the code you posted. Here is an example program that shows what to do. Note that it uses function pointers to call the functions.

Thanks for replying AncientDragon. Well, I just solved my trouble. It was a very small niggle that was the troublemaker here. I developed the initial code as a project in vs.net 2005. After I spent the entire morning with it, I rebuilt the entire project this time in vs.net 2003 and it worked without a hitch. After this I realized the trouble. vs.net 2005 has a default charset of Unicode, while vs.net 2003 has a default charset of multibyteset. Changing the charset in vs.net 2005 to multibyteset or noset kills the issue.


I have one more question. I am not sure if this I should ask this in a new thread. It is related to my current project anyways. This is the question. My first project here above is DLLProctTest. When I created this project I selected the option to create a dll and not a executable binary. Now my question is why does it build a DLLProctTest.lib along with DLLProctTest.dll when I build the project. Isn't a DLLProctTest.dll sufficient for any other application that wants to make use of the exported libraries? If I had to use this dll from any other application I also had to provide the link to this .lib file along with .dll file.

Thanks for replying AncientDragon. Well, I just solved my trouble. It was a very small niggle that was the troublemaker here. I developed the initial code as a project in vs.net 2005. After I spent the entire morning with it, I rebuilt the entire project this time in vs.net 2003 and it worked without a hitch. After this I realized the trouble. vs.net 2005 has a default charset of Unicode, while vs.net 2003 has a default charset of multibyteset. Changing the charset in vs.net 2005 to multibyteset or noset kills the issue.

I don't like that default UNICODE mode either because I always turn it off.

I have one more question. I am not sure if this I should ask this in a new thread. It is related to my current project anyways. This is the question. My first project here above is DLLProctTest. When I created this project I selected the option to create a dll and not a executable binary. Now my question is why does it build a DLLProctTest.lib along with DLLProctTest.dll when I build the project. Isn't a DLLProctTest.dll sufficient for any other application that wants to make use of the exported libraries? If I had to use this dll from any other application I also had to provide the link to this .lib file along with .dll file.

Many programs statically link to the dll and the *.lib is required to do that. You do it all the time when using win32 api function calls because somewhere in the windows.h there are paragmas that force the application program to link with many microsoft *.lib that are located in the \lib directory where you installed the compiler. I almost never dynamically load the dlls because I don't like using the function pointers -- would rather use normal function calls as you would with the win32 api functions.

This article has been dead for over six months. Start a new discussion instead.