This example will demonstrate how you can export functions from DLL so that it can be accessed by other application. Let’s start to create a new win32 DLL project. Let’s  name the project as “TestDLL”.

Exporting C++ Functions from DLLExporting C++ Functions from DLL

Make sure that you select DLL as the application type. Click Finish to start the coding part.

Exporting C++ Functions from DLL

Add the following code to the TestDLL’s CPP file.
1
2
3
4
5
6
7
8
#include "Windows.h"

#define EXPORT extern "C" __declspec (dllexport)

EXPORT void PopMsg(char *msg)
{
MessageBox(NULL, msg, "Export Function Demo", MB_OK);
}
If you try to compile now, you should see the TestDLL.dll file being generated. Let’s  create a new C++ win32 console application to access the PopMsg() function in TestDLL.dll. Copy and paste the following code into the main function and compile it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <windows.h>
#include <string.h>
#include <iostream>

int _tmain(int argc, char* argv[])
{
// Load the TestDLL.dll
HINSTANCE hDLL = LoadLibrary("D:\\askyb\\release\\TestDLL.dll");

if(hDLL == NULL)
{
printf("Failed to load DLL");
return 1;
}

// define the function prototype here
typedef void(*fnPtr)(char*);

fnPtr pfn;

// Get the address of the function you wish to call
pfn=(fnPtr)GetProcAddress(hDLL,"PopMsg");

if(pfn)
{
// call the PopMsg in TestDLL
pfn("Exporting C++ Functions from DLL");
}

FreeLibrary(hDLL);

return 0;
}
Run the EXE and you should see the popup by now. Exporting C++ Functions from DLL Download sample source code: C++ Export Function