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

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

Assumptions
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
Sofware/Component
Image
Windows XP SP2
N/A
Visual C++ 2008 Express Edition
N/A
Links to these files can be found here


CPPDLLExample
_|_MyHook.cpp
_|_MyHook.h
_|_MyHook.def
_|_CPPDllImplicit.cpp
_|_CPPDllExplicit.cpp

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
     3. 
     4. #ifdef MYHOOK_DLLEXPORT
     5. #define MYHOOK_API __declspec(dllexport)
     6. #else
     7. #define MYHOOK_API
     8. #endif
     9. 
    10. MYHOOK_API int AddNumbers(int, int);
    11. MYHOOK_API int GetMessage(char*, int);
    12. 
    13. #endif
    Hide line numbers

  2. Create the source file and save it as MyHook.cpp
     1. #include "MyHook.h"
     2. #include <string.h>
     3. 
     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.
     2. 
     3. LIBRARY      "myhook"
     4. DESCRIPTION  "MyHook Windows Dynamic Link Library"
     5. 
     6. EXPORTS
     7.     ; Explicit exports can go here
     8.     AddNumbers
     9.     GetMessage
    10.     
    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"
     3. 
     4. using namespace std;
     5. int main(){
     6. 
     7.     char msg[64];
     8.     cout << AddNumbers(5,10) << endl;
     9.     GetMessage(msg, 64);
    10.     cout << msg << endl;
    11. 
    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>
     3. 
     4. typedef int(*pAddNumbers)(int,int);
     5. typedef int(*pGetMessage)(char*,int);
     6. 
     7. using namespace std;
     8. int main(){
     9. 
    10.     HINSTANCE hInstance;
    11.     
    12.     if(!(hInstance=LoadLibrary("myhook.dll"))){
    13.         cout << "could not load library" << endl;
    14.         goto FINISH;
    15.     }
    16. 
    17.     pAddNumbers padd = (pAddNumbers)GetProcAddress(hInstance, "AddNumbers");
    18.     pGetMessage pget = (pGetMessage)GetProcAddress(hInstance, "GetMessage");
    19. 
    20.     if(!padd || !pget){
    21.         cout << "could no load functions" << endl;
    22.         goto FINISH;
    23.     }
    24. 
    25.     char msg[64];
    26. 
    27.     cout << padd(20,35) << endl;
    28.     pget(msg, 64);
    29.     cout << msg << endl;
    30. 
    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

3 comments:

B.Janssen.NL@GMail.com 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