Tuesday, August 13, 2013

Create C Program with Dynamic Link Library (DLL) using Visual Studio 2012 (Implicit Link)

The following procedure is to create a C program with a dynamic link library using Microsoft Visual Studio Express 2012.

Brief Introduction to Dynamic Link Library (DLL)
The important difference between DLL and static library is that the code of external function is stored in the DLL file, whereas when linking with static library, the code of the external functions is copied to the execution file of the application program. With static library, you just need an execution file whereas with DLL you need both the execution file and the DLL file for the program to work.

Implicit or Explicit Link
When creating DLL, we have the choice of implicit linking or explicit linking. Since implicit linking is easier and common, this article will focus on implicit linking. For additional information please refer to the following:

Linking Implicitly
Linking Explicitly
Determining Which Linking Method to Use

Examples:
The following example is a normal C program with functions without using any custom static library. 

#include <stdio.h>
#define PI 3.1415

double PowerOf2 (double UserNumber);
double PowerOf3 (double UserNumber);
double CircleArea (double UserRadius);
double CircleCircum (double UserRadius);

int main ( )
{

 double p2 = 10.0;
 double p3 = 5.0;
 double radius = 4.0;

 printf ("The number %.2f to the power of 2 is %.2f. \n", p2, PowerOf2(p2));

 printf ("The number %.2f to the power of 3 is %.2f. \n", p3, PowerOf3(p3));

 printf ("A circle with a radius of %.2f, the area is %.2f. \n", radius, CircleArea(radius));

 printf ("A circle with a radius of %.2f, the circumference is %.2f. \n", radius, CircleCircum(radius));

 return 0;

}

double PowerOf2 (double UserNumber)
{
 return UserNumber * UserNumber;
}


double PowerOf3 (double UserNumber)
{
 return UserNumber * UserNumber * UserNumber;
}

double CircleArea (double UserRadius)
{
 return UserRadius * UserRadius * PI;
}
 
double CircleCircum (double UserRadius)
{
 return 2 * UserRadius * PI;
}

We will use the above example to create a dynamic link library and an application program that use this library. We will place the DLL in a shared public folder and place the application in the personal document folder.

To create a dynamic link library, it consist of 2 files MyMathDll.h and MyMathDll.c

To create Dynamic Link Library
Step 1: Create a new project as dynamic link library
Create a new project using MyMathDll as project name


Click "OK". Then click "Next". On following screen you need to specify that you are creating a DLL and make sure you check Empty project.


Step 2: Create the headers file
Under Solution Explorer, right click Headers Files >> Add >> New Item as shown below.


Select header file as file type and rename the header file to MyMathDll.h


Click "Add".

Add the following to the headers:

#ifdef MYMATHDLL_EXPORTS
#define MYMATHDLL_API __declspec(dllexport) 
#else
#define MYMATHDLL_API __declspec(dllimport) 
#endif

#define PI 3.1415

MYMATHDLL_API double PowerOf2 (double UserNumber);
MYMATHDLL_API double PowerOf3 (double UserNumber);
MYMATHDLL_API double CircleArea (double UserRadius);
MYMATHDLL_API double CircleCircum (double UserRadius);

The dllexport and dllimport attributes are extensions to the C and C++. You can use them to export and import functions to or from a DLL. When the MYMATHDLL_EXPORTS symbol is defined, the MYMATHDLL_API symbol will set the __declspec(dllexport) modifier in the member function declarations. This enables the function to be exported by the DLL so that it can be used by other applications. When MYMATHDLL_EXPORTS is undefined, MYMATHDLL_API defines the __declspec(dllimport) modifier in the member function declarations. This enables the compiler to optimize the importing of the function from the DLL for use in other applications. By default, MYMATHDLL_EXPORTS is defined when the MyMathDll project is built.

You can also define the header file this way.

#define PI 3.1415

__declspec(dllexport) double PowerOf2 (double UserNumber);
__declspec(dllexport) PowerOf3 (double UserNumber);
__declspec(dllexport) CircleArea (double UserRadius);
__declspec(dllexport) double CircleCircum (double UserRadius);

For further information on dllexport and dllimport please refer to this article.

Step 3: Create the Implementation Program
Under Solution Explorer, right click Source Files >> Add >> New Item. Rename the program to MyMathDll.c.


Enter the source code as follows

#include "MyMathDll.h"

double PowerOf2 (double UserNumber)
{
 return UserNumber * UserNumber;
}

double PowerOf3 (double UserNumber)
{
 return UserNumber * UserNumber * UserNumber;
}

double CircleArea (double UserRadius)
{
 return UserRadius * UserRadius * PI;
}
 
double CircleCircum (double UserRadius)
{
 return 2 * UserRadius * PI;
}

Step 4: Build solutions
Build solutions. You should have result as follows:


Once the compilation is complete, close the project.


Create a C Application Program Using DLL
Step 1: Create a new project
Create a new project using the name MyApp3 with Console Application as options. Remember to clear all headers and checked Empty Project.

Step 2: Establish Link with the Static Library
Under Solution Explorer, right click and select Add >> Existing Projects


Navigate to the project folders of MyMathDll and select the project file MyMathDll.vcxproj and click Open.

Your solution explorer should show two projects like below:


Select MyApp3, right click and select properties. Under Common Properties, select Framework and References. Click "Add New Reference"


You should see the following screen, there should be a project named "MyMathDll". Check the project as shown below and click "OK".


On the properties page, under Configuration Properties >> C/C++ expand the list in C/C++ and select "General" as shown below.


For first line "Additional Include Directories", click the arrow and select edit. The following dialog box shows:


Click the "New Line" icon and click on the button "...". Now you need to navigate to the DLL folder. Do not select the folder "MyMathDll" under Projects, drill one level down and select the folder "MyMathDll". The purpose is to the select the folder that contain the header file. Highlight the folder and click Select Folder.


Click OK. Then click Apply. You can close the property dialog box.

Step 3: Create the application program 
Under Solution Explorer, right click Source Files >> Add >> New Item. Rename the program to MyApp3.c.

Enter the following program:

#include <stdio.h>
#include "MyMathDll.h"

int main ( )
{

 double p2 = 10.0;
 double p3 = 5.0;
 double radius = 4.0;

 printf ("The number %.2f to the power of 2 is %.2f. \n", p2, PowerOf2(p2));

 printf ("The number %.2f to the power of 3 is %.2f. \n", p3, PowerOf3(p3));

 printf ("A circle with a radius of %.2f, the area is %.2f. \n", radius, CircleArea(radius));

 printf ("A circle with a radius of %.2f, the circumference is %.2f. \n", radius, CircleCircum(radius));

 return 0;

}

Step 4: Build the solution.
After you have built the solutions, you should received a message similar to the one below. Please note that two projects are being built.


Step 5: Run the program
You can test the project from the command prompt as shown below:



Additional Note:
Please note that for the successful execution of the program. MyApp3.exe and MyMathDll.dll should be in the same folder. Alternatively, the MyMathDll.dll should be located where the Windows system could find. The search path the DLL files for Windows are as follows:

  1. The directory where the exe file is located.
  2. The Windows system directory. 
  3. The Windows directory. 
  4. The directories listed in the PATH environment variable.


This completes the procedure for creating a DLL in C using Visual Studio Express 2012. Please note that the procedure for creating similar DLL using command line is different from using the IDE. 







*****



2 comments:

  1. This was a very helpful step by step process. Excellent help!!! Thank you very much.

    ReplyDelete
  2. Thank you for your step-by-step tutorial!
    I was struggling with the .DLL problem for a few days and now by finding your post, it is done in less than hour!
    Thank you again

    ReplyDelete

Note: Only a member of this blog may post a comment.