Creating a DLL


To create a new dynamic link library (DLL) project
1. From the File menu, select New and then Project….
2. On the Project types pane, under Visual C++, select Win32.
3. On the Templates pane, select Win32 Console Application.
4. Choose a name for the project, such as testDll, and type it in the Name field. Choose a name for the solution, such as testDll, and type it in the Solution Name field.
5. Click OK to start the Win32 application wizard. On the Overview page of the Win32 Application Wizard dialog box, click Next.
6. On the Application Settings page of the Win32 Application Wizard, under Application type, select DLL.
7. On the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty project. 8. Click Finish to create the project.

To add a function to the dynamic link library

1. To create a source, from the Project menu, select Add New Item…. The Add New Item dialog box will be displayed. On the Categories pane, under Visual C++, select Code. On the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as testDll.cpp, and click Add. A blank file will be displayed.
2. Copy the code:

#include "testDll.h"
int add(int a, int b)
{ return a+b; }
int subtract(int a, int b)
{ return a-b; }
long multiply(int a, int b)
{ return a*b; }
float divide(int a, int b)
{ if(b==0)
{ printf("Error: Divide by Zero…\n");
return -1; }
else return (float)a/b;
}

3. To create a header file, right click “Header Files” in Solution Explorer, add files. On the Templates pane, select Header File (.h). Choose a name for the header file, such as testDll.h, and click Add. A blank file will be displayed.
Copy the following code:

#include <stdio.h>
__declspec(dllexport) int add(int a, int b);
__declspec(dllexport) int subtract(int a, int b);
__declspec(dllexport) long multiply(int a, int b);
__declspec(dllexport) float divide(int a, int b);

4. Note the __declspec(dllexport) modifier in the method declarations in this code. These modifiers enable the method to be exported by the DLL so that it can be used by other applications.
Another way to export a function is: To create a module-definition (.DEF) file which informs the linker to export our function. Select New from File menu and then select text File from Files tab. Give it a name example1.def.
Here is a sample of a module-definition fie (DEF file).

LIBRARY YOURDLL
DESCRIPTION ‘This is my DLL
EXPORTS
Whatever1 @2
Whatever2 @3

The LIBRARY command must match the name of your DLL. So if your DLL is named YOURDLL.DLL, the LIBRARY command is followed by YOURDLL.

The DESCRIPTION is any description you feel is appropriate for your DLL. The actual description is preceded by a single quote (‘).

The EXPORTS lists the functions that you exported, along with an ordinal number. It doesn’t matter what ordinal numbers you use, so long as you maintain different numbers for each function, and you shouldn’t change the ordinal numbers when you create an upgraded version of your DLL — persons preferring to use ordinal numbers to call your functions will get angry.

5. To build the project into a DLL, from the Project menu, select testDll Properties…. On the left pane, under Configuration Properties, select General. On the right pane, change the Configuration Type to Dynamic Library (.dll). Click OK to save the changes.

Note: If you are building a project from the command line, use the /LD compiler option to specify that the output file should be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library).

6. Compile the dynamic link library by selecting Build Solution from the Build menu. This creates a DLL that can be used by other programs.

To create an application that references the dynamic link library
Create a normal Win32 Console application project.

To use the functionality from the class library in the console application

1. After you create a new console application, an empty program is created for you. The name for the source file is the same as the name that you chose for the project earlier. In this example, it is named testDllRef.cpp.

#include "testDll.h"
#include
#include
#pragma comment(lib, "testDll.lib")
int main()
{
printf("3+5= %d\n",add(3,5));
printf("3-5= %d\n",subtract(3,5));
printf("3*5= %ld\n",multiply(3,5));
printf("3/5= %f\n",divide(3,5));
_getch();
}

2. To reference the header files of the dynamic link library, you must modify the include directories path. To do this, on the Property Pages dialog box, expand the Configuration Properties node, expand the C/C++ node, and then select General. Next to Additional Include Directories, type the path of the location of the testDll.h header file.

3. Linking to the dll:

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

Does the job. If you don’t want to use this statement u can manually set Project> properties > Configuration Properties > Linker> Input:

Additional Dependencies – add the name of the .lib file, “testDll.lib” here.
4. Copy the testDll.dll & testDll.lib to the folder where your source file (testDllRef.cpp) is present.
5. Build the executable by selecting Build Solution from the Build menu.
6. Run the “testDllRef.exe” from Debug> Start without Debugging. It should work!
7. If you try to run the generated “testDllRef.exe” from the “Release” folder, you will have to copy the “testDll.dll” & ”testDll.lib” to the Release folder. Then it’ll run.
To avoid this, you can copy the .dll & .lib to “Release” folder & in the #pragma comment specify:

#pragma comment(lib, "../Release/testDll.lib")

It’ll also work.

Reference: MSDN Library

Advertisements

What do you think??

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s