Simple Windows C++ DLL Example with Implicit and Explicit Calls

The aim of this Windows C++ tutorial is to build a simple Dynamic Link Library or DLL using the command line Visual C++ compiler (cl.exe0. Then create two small C++ test programs that will call this DLL implicitly by linking with the lib file and explicitly using LoadLibrary and GetProcAddress. Also note that the explicit linking requires a "def" definition file with the exported funtion names.

This article assumes that you have a compatible version of Visual C++ installed and you have run vcvars32. The file is called MyHook because it will be used later as a windows keyboard hook dll.

Versions used in this example
Windows XP SP2
Visual C++ 2008 Express Edition
Links to these files can be found here


The dll requires 3 files. Source file, header file and a definitions file. This def file is required to expose the functions for explicit linking only

Create the DLL
  1. Create the source file and save it as MyHook.h
     1. #ifndef MYHOOK_H
     2. #define MYHOOK_H
     4. #ifdef MYHOOK_DLLEXPORT
     5. #define MYHOOK_API __declspec(dllexport)
     6. #else
     7. #define MYHOOK_API
     8. #endif
    10. MYHOOK_API int AddNumbers(int, int);
    11. MYHOOK_API int GetMessage(char*, int);
    13. #endif
    Hide line numbers

  2. Create the source file and save it as MyHook.cpp
     1. #include "MyHook.h"
     2. #include <string.h>
     4. MYHOOK_API int AddNumbers(int x, int y){
     5.     return x + y;
     6. }
     7. MYHOOK_API int GetMessage(char* x, int length){
     8.     if(length>32){
     9.         strcpy(x, "hello from dll");
    10.         return 0;
    11.     }
    12.     else
    13.         return -1;
    14. }
    Hide line numbers

  3. Finally create the definitions file and save it as MyHook.def
     1. ; MyHook.def : Declares the module parameters for the DLL.
     3. LIBRARY      "myhook"
     4. DESCRIPTION  "MyHook Windows Dynamic Link Library"
     6. EXPORTS
     7.     ; Explicit exports can go here
     8.     AddNumbers
     9.     GetMessage
    11. SECTIONS
    12.     ; Pragma sections
    Hide line numbers

  4. Build the library on the command line

    ...CPPDLLExample>cl -o myhook.dll myHook.cpp /D MYHOOK_DLLEXPORT /link /DLL /DEF:"MyHook.def"

Write and Compile Implicit caller
  1. Write implicit compiler and save it as CallDLLImplicit.cpp
     1. #include <iostream>
     2. #include "MyHook.h"
     4. using namespace std;
     5. int main(){
     7.     char msg[64];
     8.     cout << AddNumbers(5,10) << endl;
     9.     GetMessage(msg, 64);
    10.     cout << msg << endl;
    12. }
    Hide line numbers

  2. Build the program using,

    ...CPPDLLExample>cl CallDllImplicit.cpp MyHook.lib

Write and compile the explicit client
  1. Write the explicit caller and save it as CallDllExplicit.cpp
     1. #include <iostream>
     2. #include <windows.h>
     4. typedef int(*pAddNumbers)(int,int);
     5. typedef int(*pGetMessage)(char*,int);
     7. using namespace std;
     8. int main(){
    10.     HINSTANCE hInstance;
    12.     if(!(hInstance=LoadLibrary("myhook.dll"))){
    13.         cout << "could not load library" << endl;
    14.         goto FINISH;
    15.     }
    17.     pAddNumbers padd = (pAddNumbers)GetProcAddress(hInstance, "AddNumbers");
    18.     pGetMessage pget = (pGetMessage)GetProcAddress(hInstance, "GetMessage");
    20.     if(!padd || !pget){
    21.         cout << "could no load functions" << endl;
    22.         goto FINISH;
    23.     }
    25.     char msg[64];
    27.     cout << padd(20,35) << endl;
    28.     pget(msg, 64);
    29.     cout << msg << endl;
    31. FINISH:
    32.     cout<<"finished"<<endl;
    33. }
    Hide line numbers

  2. Now cd out to the workingdirectory build it

    ...CPPDLLExample>cl CallDLLExplicit.cpp

Running the executables
  1. Open a command prompt into your working directory and the executables. You should be able to see the results. Delete the dll and run them again - you should get an error message

Back to the tutorial trail | Home

5 comments: said...

Great, just removed the goto FINISH:
Exellent small snippet.

Anonymous said...

thx needed the /D option

Anonymous said...

Good article. Just one typo (in step 1), you said:
- Create the source file and save it as MyHook.h

This should be:
+ Create the header file and save it as MyHook.h

Naviya Nair said...

Very interesting and good Explanation
ASP NET Training
ASP NET Training
ASP NET Online Training
C-Sharp Training
Dot Net Training in Chennai
Online .Net Training

MVC Training
WCF Training
Web-API Training
LINQ Training
Entity Framework

Dot Net Interview Questions

Morris Sterling said...

Thank you for sharing. Very good tutorial. It helped me. I have a good website that will help you solve a problem with this file D3DCompiler_43.dll. Here is a link to the website . Maybe I can help someone.