DLL – Dynamic Link Library in C – Steps to Create and Use

Example to create and use DLL – Dynamic Link Library in C on windows with simple steps with C source code.  Both implicit and explicit linking of dll from C programs example has been provided.

CREATING DLL (Dynamic Link Library in C )

Steps to create Library Project

Let’s get you started on your first DLL:

  • Start Visual Studio
  • Go to menu File -> New -> Project.
  • Select Visual C++ Project, and from the Templates, select Win32 Console Application.
  • Give the name to your project. This will be the name of your DLL file.
  • Press OK.
  • Select DLL from Application Type (In the Application Settings tab).
  • Check Empty Project and press Finish.

We need to create a header file and a source file to the blank project:

Create header file and source code:

In Solution Explorer, under project name, there are folders Header Files and Source Files. Right click to the Header Files, Add -> Add New Item and then select Header File(.h) give a name e.g. hellodll.h and add it.

Open hellodll.h and paste the below source code.

/*Use __cplusplus preprocessor macro to determine
which language is being compiled.

If we use this technique and provide header files
for the DLL, these functions can be used by C and C++
users with no change

#ifdef __cplusplus  
extern "C" {               
#endif  
  
//Your functions that can be accessible from
//C or C++ projects
  
#ifdef __cplusplus  
}  
#endif  
*/

#ifdef __cplusplus  
extern "C" {               
#endif  
  
	//Write your functions with __declspec( dllexport )
	//to available in client code
__declspec( dllexport ) void helloDLL();  
  
#ifdef __cplusplus  
}  
#endif  

Create C Source file and source code:

Create Source file hellodll.c

In Solution Explorer, under project name, there are folders Header Files and Source Files. Right click to the Source Files, Add -> Add New Item and then select C++ File(.cppgive a name e.g. hello.c  and add it.(Note: we have selected C++ file but we want to wirte code in C, so, give extention .c)

Open hellodll.c and paste the below source code.

#include<stdio.h>
#include "hellodll.h"//include dll header file

//define your functions in here .c file
void helloDLL(){

      //This will be printed when user call

      //this function from his C or C++ application

       printf ("Hello DLL.\n");

}

Build the project & Library will be ready

Build the project and go to the project folder. You should find below two files under Debug or Release folder whatever you have set. Your library is ready to use.

hellodll.dll

hellodll.lib

USING DLL – Dynamic link Library

Static or implicit linking – using dll header file and .lib file only.

Let’s get you started on Test console based application to use previously created Library:

  • Start Visual Studio
  • Go to menu File -> New -> Project.
  • Select Visual C++ Project, and from the Templates, select Win32 Console Application.
  • Give the name to your project. This is the name of test application.
  • Press OK.
  • Select Console application from Application Type (In the Application Settings tab).
  • Check Empty Project and press Finish.

Right click to the Source Files, Add -> Add New Item and then select C++ File(.cpp) give a name e.g. testdll.c  and add it.

Copy hellodll.h and hellodll.lib files from dll project, you have already created and paste it in current test project folder where you have test.vcproj.

Right click to the Header Files, Add -> Add Existsing Item and then select hellodll.h and add it.

Open  test.c file paste the below source code

#include<stdio.h>
//Include header file from dll project
#include "hellodll.h"

/* ----- Static or Implicit DLL linking ----*/

//Use pragma to load dll lib file

#pragma comment(lib, "hellodll.lib")

int main(){      

      //Call dll from library
       helloDLL();

      return 0;

}

When you build and run the test project, the sting “Hello DLL.” will be printed. as it calls helloDLL() function from library.

EXPLICIT LINKING OR DYNAMIC LINKING

By using only  .DLL file  and no dll header file or .lib file.

Create a test project (Steps are same as above) and create a source file e.g. test.c

open test.c and paste the below code

#include<stdio.h>
#include<windows.h>
//Create a function pointer for the function 
//written in DLL library
//I have written void helloDLL()in the library
//
typedef void (*MYPROCP)(); //function pointer for void helloDLL()
int main(){

	HINSTANCE hlibrary; 
	MYPROCP functionPointer;

	//create a handle to load library
	hlibrary = LoadLibrary(TEXT("hellodll.dll"));

	// If the handle is valid, try to get the function address.
	if (hlibrary != NULL){

		functionPointer = (MYPROCP)GetProcAddress(hlibrary, "helloDLL");

		if(functionPointer){
			//call function from dll
			functionPointer();
		}
		//free library
		FreeLibrary(hlibrary);
	}else{
		//dll fails to load
		printf("DLL Loading fails");
	}

	return 0;
}

Build the test project.

Copy hellodll.dll from dynamic link library project you have created. and paste it in the test project Debug or Release folder , what you have set.

Run the project and you get output as “Hello DLL.” from dynamic link library in C test program.

References:

https://msdn.microsoft.com/en-in/library/9yd93633.aspx – static and dynamic linking
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686944(v=vs.85).aspx – dynamic linking program

Related Posts