How to create and use DLL in C++

Blog ยป How to create and use DLL in C++

Posted on 06 May 2011 13:24

The following steps apply for visual studio 2008.

Step 1: Create the project for the DLL.

  1. Let's create a simple Win32 Console DLL: Visual Studio > File > New Project > Win32 Console Application
  2. Name it something sexy like MyDLL and press OK.
  3. Press Next in the following screen and choose DLL. Leave other options untouched.
  4. Press Finish.

Step 2: Add the code for the DLL.
I will create a new class and add some simple code.

  1. Right-click on Project name > Add > Class > Name it something like HelloDLL
  2. Add the code that follows.
  3. To create the DLL export library: Project > MyDLL properties… > Configuration Properties > C/C++ >Preprocessor > Append, or insert, "DLLDIR_EX" (without the quotation marks) to the Preprocessor Definition text box > OK. This will prevent compiler assumptions and warnings. [1]
  4. Build > Build Solution
  5. Your DLL should be ready. You should have files MyDLL.dll and MyDLL.lib in directory project-directory\Debug.

HelloDLL.h

#pragma once
 
//more about this in reference 1
#ifdef DLLDIR_EX
   #define DLLDIR  __declspec(dllexport)   // export DLL information
 
#else
   #define DLLDIR  __declspec(dllimport)   // import DLL information
 
#endif 
 
class DLLDIR HelloDLL
{
public:
    HelloDLL(void);
    ~HelloDLL(void);
 
      void hello();
     static void helloStatic();
 
};

HelloDLL.cpp

#include "StdAfx.h"
#include "HelloDLL.h"
#include <iostream>
 
using namespace std; 
 
HelloDLL::HelloDLL(void)
{
}
 
HelloDLL::~HelloDLL(void)
{
}
 
void HelloDLL::hello()
{
    cout << "Hello World of DLL" << endl;
}
 
void HelloDLL::helloStatic()
{
    cout << "Hello World of DLL static" << endl;
}

Step 3: Use the DLL in a new project.

  1. Create a new project: Visual Studio > File > New Project > Win32 Console Application. Name it Use of MyDLL and press OK.
  2. Press Next.
  3. Choose Console application.
  4. Press Finish.

Step 4: Logistics for the new project.

  1. Add file MyDLL.lib in your project: Project > Properties > Configuration Properties > Linker > Input > Write the full path (including the filename) in quotes. Example: "C:\Examples\MyDLL\Debug\MyDLL.lib"
  2. Move file MyDLL.dll in the same directory as the source code of your project Use of MyDLL.
  3. Add header file: Project > Properties > Configuration Properties > C/C++ > Additional Include Directories > Add the directory where the file HelloDLL.h is located.

Step 5: Test your DLL.
I used the following code:

#include "stdafx.h"
#include "HelloDLL.h"
 
int _tmain(int argc, _TCHAR* argv[])
{
 
     HelloDLL helloDLL;
     helloDLL.hello();
     HelloDLL::helloStatic();
 
    getchar();
 
    return 0;
}

Rate this artile

rating: 0+x

Bookmark this article

FacebookRedditdiggFurldel.icio.us

Leave a comment

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.