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.
- Let's create a simple Win32 Console DLL: Visual Studio > File > New Project > Win32 Console Application
- Name it something sexy like MyDLL and press OK.
- Press Next in the following screen and choose DLL. Leave other options untouched.
- Press Finish.
Step 2: Add the code for the DLL.
I will create a new class and add some simple code.
- Right-click on Project name > Add > Class > Name it something like HelloDLL
- Add the code that follows.
- 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]
- Build > Build Solution
- 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.
- Create a new project: Visual Studio > File > New Project > Win32 Console Application. Name it Use of MyDLL and press OK.
- Press Next.
- Choose Console application.
- Press Finish.
Step 4: Logistics for the new project.
- 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"
- Move file MyDLL.dll in the same directory as the source code of your project Use of MyDLL.
- 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
Bookmark this article
page revision: 8, last edited: 14 Jun 2011 07:49







Leave a comment
Thanks for posting this valuable information and C++ source code! Very helpful. :-)
+1
Thanks coder.
May I know where is MyDLL.h file located?is it same directory with MyDLL.cpp?I cant find it?
There is no MyDLL.h or MyDLL.cpp. The only source files are HelloDLL.h and HelloDLL.cpp.
MyDLL is the name of the project.
Hi,I manage to solve it oledi,the MyDLL.h of step 4,"Add header file: Project > Properties > Configuration Properties > C/C++ > Additional Include Directories > Add the directory where the file MyDLL.h is located" should be is HelloDLL.h
You are right. I changed it. Thanks Henry :)
Hey, I have had to use dll file in some of my projects in the past. My question is, say I only have a dll file, no lib file or nothing else I only have dll and the method listing using which I can use that dll from a client program. Now, my question is, in situation such as this, how could i include the lib directory, or the header file ?
Another question would be, how to create standalone dll file, so that i wont have to include header file, lib file when i call the dll from the client program
realy nice tutorial…
thanks
great tutorial..
it helps me to solve my problem..
thanks
:-)
I think you are using the static library instead, because the dlls are load from code, for example in Win32 it is necessary to use LoadLibrary().
Hi,
i have one utility and have to automatically install the one dll to it. for that it need to ask about the utility compatibility like version and it also ask for the utility is pirate version or original version . if it is original version it need to ask for the identity and have to give the full access to it . otherwise some properties of dll has to be hidden…. then only it has to install on utility. give me some ideas to do it. pls……
and this my mail id: moc.liamg|magabnehs2liam#moc.liamg|magabnehs2liam ………
Hi, Can anybody help to suggest the steps for DLL implementation.
I have prepared dll on WinXP(my development machine) using MS Visula Studio 2010 and want to test on WinNT(My UAT server).
When I try to register the DLL on UAT server for usage..It says LoadLibrary(XXX.DLL) Failed. The module not found.
Any idea what should I do to get my dll running and picked by the application for testing.
Post preview:
Close preview