Wednesday, April 27, 2011

C++ Tutorial: Create a DLL in VC++ 2010/2008

If the tutorial is too fast for you then read the following steps:

1. Start Visual Studio (or C++ Express Edition) -> Create a new Win32 Console Application project -> Application Settings -> Check DLL and Empy Project -> Hit Finish

2. Go to Solution Explorer -> Right Click on Header Files -> Add a new header file -> Declare your stuff (for example: a new class)

// SimpleH.h
namespace nmspace
{
 class myclass
 {
 public:
  static __declspec(dllexport) void Crap();
 };
}

NOTE: The dllexport and dllimport storage-class attributes are Microsoft-specific extensions to the C and C++ languages. They enable you to export and import functions, data, and objects to and from a DLL.

3. Go to Solution Explorer -> Right Click on Source Files -> Add a new cpp file -> Include your header here #include "YourHeader.h" and define your stuff

// SimpleCPP.cpp
#include <iostream>
using namespace std;
#include "SimpleH.h"

namespace nmspace
{
 void myclass::Crap()
 {
  cout << "I'm called within a crappy DLL!";
 }
}

4. Create a new Win32 Console Application project -> Application Settings -> Leave Console application checked and check Empty Project -> Add a new CPP file (for example: main.cpp)

5. Go to your DLL Project folder and copy the Header file (SimpleH.h), the Object Library File (SimpleDLL.lib) and the DLL itself (SimpleDLL.dll)! Paste these files into your new project folder (where main.cpp is located)

6. Include the Header file (SimpleH.h) to your new project -> Go to Solution Explorer -> Right Click on the Project name (NOT THE SOLUTION) -> References

7. Configuration Properties -> Linker -> Input -> Additional Dependencies -> Add the .lib file (SimpleDLL.lib) -> Hit OK -> HIT APPLY and then OK!

8. In main.cpp call the function from the DLL created -> Start the application

// main.cpp
#include "SimpleH.h"
#include <iostream>
using namespace std;
using namespace nmspace;

int main()
{
 myclass::Crap();
 system("PAUSE");
}

9. Go to you project folder -> Debug folder (The first Debug folder) -> Copy the DLL there -> Start the application -> And you're done!

10. If you found this confusing then watch the video tutorial!

17 comments:

  1. very informative post indeed .being enrolled in http://www.wiziq.com/course/5776-object-oriented-programming-with-c
    i was looking for such articles online to assist me and your article helped me a lot. i really like that you are providing such information.

    ReplyDelete
  2. +1 for vid
    -2 for lack of explanation behind the steps
    -1 for could of used better example

    ReplyDelete
  3. namespace definition not allowed :) ur code is bugged

    ReplyDelete
  4. Nice explaination in less description

    ReplyDelete
  5. it's very useful for me!!
    thank you!!!!!!!!!!!!

    ReplyDelete
  6. Thanks for your post it was really helping.. can you please give me some hint how can i add the dll in VB.net

    ReplyDelete
  7. Couldn't added to reference:

    "A reference to 'SimpleDLL' could not be added."

    ReplyDelete
  8. How do the same but in diferent scenario, your dll with return a string function and send a string parameter, this dll read by static library (c++) and finaly go to console aplication. (the importan case is link with the static library , because don't have linker option , have equivalent library)

    ReplyDelete
  9. Can you please explain how to use this Dll and cpp header files in a C# project?

    ReplyDelete


  10. Thanks for sharing the very useful info about clanguage and please keep updating........

    ReplyDelete
  11. Nice tutorial. Thanks for sharing the valuable info about c Training. it’s really helpful. Who want to learn c language this blog most helpful. Keep sharing on updated tutorials…..

    ReplyDelete