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).

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"
#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));

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


Creating a setup file in VC++

Ever imagined how the setup files are made? i was curious too. Following is the procedure to create one in Visual Studio for VC++ projects.

Creating a setup file for your application using MS Visual Studio 2008

1. First build your application & generate the executable (.exe) file in “Release mode”.

2. While the solution file is still open in VS, create a deployment project (as shown in the fig 1):

File> New Project > Project types: select “Setup & Deployment”.

fig 1

3. On the Templates pane on right side, select “Setup Project”. Give a suitable name & location for the project.

4. Instead of “creating a new solution”, select the “Add to solution” option from the drop down list. The window will look like this:

fig 2

5. Add the output of the application project to the setup project. To do so, right-click on Setup1 in Solution Explorer, point to Add, and then click Project Output… fig 3.fig 3

i. In the Add Project Output Group dialog box, select Primary Output.

ii. Select Release Win32 from the Configuration drop-down box, and then click OK.

6. In the Detected Dependencies, Visual Studio adds the dependent merge module (.msm) files for the Visual C++ libraries to the setup project.

fig 4

7. IMP: if you have any other dll or lib files that your project depends on, add it to the Application folder by right clicking “Application folder” in the “File System” tab & Add > file.

fig 5 fig 6

8. Build the setup project to create the installer files (Setup1.msi and setup.exe). To do so, right click the Setup1 project node in Solution Explorer and select Build.

Visual Studio creates the installer files in the Release folder of your setup project.

fig 7

9. Run the installer on a second computer that does not have the Visual C++ libraries.

i. Copy Setup1.msi and setup.exe to the second computer.

ii. Run setup.exe on the second computer. Follow the steps that are provided by the setup wizard to complete the installation.

fig 8 fig 9

Go to the installation folder & run the .exe. It should produce the desired output.

Reference: MSDN Library

GCC for Windows

As everyone knows, MS Visual C++/ Visual Studio compiler, linker, builder error & warning reports are way too difficult to understand & debug. So I was searching for a GCC/G++ compiler for windows & found “MinGW” – “Minimalist GNU for Windows”. It is THE GCC/G++ for windows.
Tested OK. Works exactly like the GCC for linux… using the command prompt of windows as the terminal.

In addition, DevC++ is a GUI version of MinGW, which implies it uses GCC/G++. Its IDE helps to build, organise & handle bigger projects.

1. Download MinGW

2. When you run this installer, it’ll ask for –

  • Download & install or
  • Download only
  • Go for “Download only”.
    Approx 74MB of data will be downloaded.

    3. After download, run the installer again & this time select “Download & install”.
    It’ll ask for the packages you want like g++, java compiler, ada compiler etc. Select the required & proceed.

    4. Now, you need to add this MinGW to system path. For this, goto
    My Computer-> Right click-> Properties-> Advanced tab-> Environmental Variables-> System Variables
    Select “Path” -> Edit
    Then add the folder where MinGW is extracted. Suppose it is the default C:\MinGW, then add
    at the end. OK & close.

    Your MinGW is installed & ready to use.

    5. To compile a C/C++ file, open Command prompt & cd to the folder where the .c or .cpp file is stored. then type:

    (for C file)

    gcc filename.c -o output_filename.exe

    (for C++ file)

    g++ filename.cpp -o output_filename.exe

    If -o output_filename.exe is not used then it’ll create a default “a.exe” file in that folder.
    6. Run the exe. That’s it.

    For any further doubts, refer to MinGW documentation.