## 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!

1. Thank you ....

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

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

4. http://ccppcoding.blogspot.com/

5. namespace definition not allowed :) ur code is bugged

6. Nice explaination in less description

7. it's very useful for me!!
thank you!!!!!!!!!!!!

8. how can i read dll from visual basic

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

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

11. 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)

12. Thank you, that works for me!!!

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

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

15. 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…..

16. I really like you post.Thanks for sharing.
goldenslot
Gclub จีคลับ
gclub casino