Copyright ©

Making DLL's in MicroSoft Visual C++ 6.0

previous next


Example 1 : Working from the command line

Now we make a one-line DLL. Here's the source:

extern "C" __declspec(dllexport) void myfun(int * a){*a = - *a; }

Save this to file myfun.cpp and compile it from the DOS prompt with

cl -LD myfun.cpp

The -LD switch says to generate a DLL. Next we make an executable which calls the DLL. Here's the source:

#include iostream.h

extern C __declspec(dllimport) void myfun ( int * a);

void main(void)
 int a = 6;
 int b = a;

 cout << '-' << a << " is " << b << "! \n";

Save this to file main.cpp. Then compile and link from the command prompt with

cl main.cpp /link myfun.lib

Execute it from the command line (just type 'main') and watch with awe!

Example 2 : Using VC++ IDE to create DLL

The DLL entry point

When you create a DLL, you can optionally specify an entry point function. The entry point function is called when processes or threads attach themselves to the DLL or detached themselves from the DLL. You can use the entry point function to initialize data structures or to destroy data structures as required by the DLL. Additionally, if the application is multithreaded, you can use thread local storage (TLS) to allocate memory that is private to each thread in the entry point function. The following code is an example of the DLL entry point function.

HANDLE hModule,	// Handle to DLL module
	DWORD ul_reason_for_call,
	LPVOID lpReserved ) // Reserved
	switch ( ul_reason_for_call )
		// A process is loading the DLL.
		// A process is creating a new thread.
		// A thread exits normally.
		// A process unloads the DLL.
	return TRUE;

When the entry point function returns a FALSE value, the application will not start if you are using load-time dynamic linking. If you are using run-time dynamic linking, only the individual DLL will not load.

The entry point function should only perform simple initialization tasks and should not call any other DLL loading or termination functions. For example, in the entry point function, you should not directly or indirectly call the LoadLibrary function or the LoadLibraryEx function. Additionally, you should not call the FreeLibrary function when the process is terminating.

WARNING: In multithreaded applications, make sure that access to the DLL global data is synchronized (thread safe) to avoid possible data corruption. To do this, use TLS to provide unique data for each thread.

Exporting DLL functions

To export DLL functions, you can either add a function keyword to the exported DLL functions or create a module definition (.def) file that lists the exported DLL functions.

To use a function keyword, you must declare each function that you want to export with the following keyword:


To use exported DLL functions in the application, you must declare each function that you want to import with the following keyword:


Typically, you would use one header file that has a define statement and an ifdef statement to separate the export statement and the import statement.

You can also use a module definition file to declare exported DLL functions. When you use a module definition file, you do not have to add the function keyword to the exported DLL functions. In the module definition file, you declare the LIBRARY statement and the EXPORTS statement for the DLL. The following code is an example of a definition file.

// SampleDLL.def


Write Sample DLL

In Microsoft Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type.

The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.

// SampleDLL.cpp

#include "stdafx.h"
#include "sampleDLL.h"

                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
    return TRUE;

void HelloWorld()
   MessageBox( NULL, TEXT("Hello World"), TEXT("In a DLL"), MB_OK);

// File: SampleDLL.h
#ifndef INDLL_H
#define INDLL_H

extern __declspec(dllexport) void HelloWorld() ;
extern __declspec(dllimport) void HelloWorld() ;


Calling Sample DLL in your Program

The following code is an example of a Win32 Application project that calls the exported DLL function in the SampleDLL DLL.

// SampleApp.cpp 

#include "stdafx.h"
#include "sampleDLL.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
	return 0;

NOTE: In load-time dynamic linking, you must link the SampleDLL.lib import library that is created when you build the SampleDLL project.

In run-time dynamic linking, you use code that is similar to the following code to call the SampleDLL.dll exported DLL function.

DLLPROC HelloWorld;

hinstDLL = LoadLibrary("sampleDLL.dll");
if (hinstDLL != NULL)
    HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");
    if (HelloWorld != NULL)

    fFreeDLL = FreeLibrary(hinstDLL);

When you compile and link the SampleDLL application, the Windows operating system searches for the SampleDLL DLL in the following locations in this order:

  1. The application folder
  2. The current folder
  3. The Windows system folder
    NOTE: The GetSystemDirectory function returns the path of the Windows system folder.
  4. The Windows folder
    NOTE: The GetWindowsDirectory function returns the path of the Windows folder.

previous next

Copyright ©