Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / DLL Creation Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

March 01, 2005, 04:57 AM

It's a newbie question. I've a nice set of C++ classes that work well. Three applications will use this set, so my idea was to make a DLL with all my classes. But ! I'am currently using Visual Studio .NET 2003, and I see nothing to create a DLL project. There a 'MFC DLL' wizard, but I don't care about MFC. I found nothing with google, probably a too trivial task...

So how to make a simple DLL, with C++ classes inside ?
A "hand made" solution, with a NMAKE file would be nice (I don't like big IDE, prefer goog old emacs/(some tools) )

Thanks !

Erik Faye-Lund

March 01, 2005, 05:48 AM

File -> New -> Project -> Win32 Project -> [enter a name] -> Application Settings -> Application type: DLL -> Finish

I have no clue about how to export a makefile, though.


March 01, 2005, 11:48 AM

Ok, that works !!! So it was just hidden...
A last question : there is something special for the C++ classes included in my DLL ? (Calling convention, export symbols or such cabalistic things for me)

Rui Martins

March 01, 2005, 11:55 AM

If it is C++ it uses the C Standard Conventions for parameters and C++ Conventions for the rest.
If all your code is done in C++ you don't need to worry about this, since everything will match nicelly.
If you expect to use other languages, like Pascal for example, then you have to change the conventions of the calling code, to match the DLL convention.
Clients (calling applications)should conform to the convention, not the other way arround, since Your DLL can only implement one convention, unless you compile several different versions.


March 01, 2005, 12:10 PM

You mentioned you are using visual studio, so this is compiler specific.

Use __declspec(dllexport) to export your classes into a dll. This is needed to build the export lib. When importing you should change it to __declspec(dllimport) so it has no trouble reading the import lib.

You can do something like this:

  2. #define         BUILD 1
  3. #ifdef BUILD
  4. #define __DECLSPEC_USEAGE       extern "C" __declspec(dllexport)
  5. #else
  6. #define __DECLSPEC_USEAGE       extern "C" __declspec(dllimport)
  7. #endif

Then use it to declare your classes.


March 01, 2005, 12:38 PM

Hum, sorry it's still not clear for me :( What are the magics invocations I need, and where ???

I must do something like bellow ?

In my .h file

  2. extern "C" __declspec(dllexport)
  4. namespace engine {
  5.   class Entity {
  6.   private:
  7.     Entity* father;
  9.   public:
  10.     Entity();
  11.     ~Entity();
  13.     virtual void doAction();
  14.   };
  15. }

In my .cc file
  2. #include "Entity.h"
  6. engine::Entity::Entity() {
  7. ...
  8. }
  12. engine::Entity::~Entity () {
  13. ...
  14. }
  18. void
  19. engine::Entity::doAction() {
  20. ...
  21. }


March 01, 2005, 02:00 PM

Since DLL's are actually implementing a C interface (note the 'extern "C"') you can't export a class, only functions.

So you can do something like

  2. extern "C" __declspec(export) void someFunction() {
  3.     code...
  4. }

and that function will be accessable outside the DLL, by doing

  2. extern "C" __declspec(import) void someFunction();

in the program that loads the DLL. This only works for static linked DLL's though. Instead of going into that, take a look at this article: It was very helpfull to me. It also gives a nice way to work around the inability to export classes.

Hope this helps :)


March 01, 2005, 02:03 PM

I am not sure when using namespaces, but what I meant was this:

Declare functions like this:

  2. extern "C" _declspec(dllexport) void Foo()
  3. {
  4.       // do something
  5.       return;
  6. }

And classes like this:
  2. // in your header...
  3. class __declspec(dllexport) CFoo
  4. {
  5. public:  
  6.       CFoo (){}
  7.       ~ CFoo (){}
  9. public:
  10.       void HelloWorld();
  11. };
  13. // in your cpp...
  14. void CFoo:: HelloWorld ()
  15. {
  16.       printf(_T("Hello World."));
  17. }

And donít forget to add an export function for your class.
Maybe this helps too:


March 01, 2005, 02:04 PM

Okay, now that's clear !!! Some macro around and that will work... Thanks for all the replies... Perhaps I will post a little tutorial on DLL creation, for the Windows newbies like me.


March 01, 2005, 02:52 PM

Yup what Moe said, i used that quite often.

__declspec(dllexport) after 'class' type, and before the name of ur class.

Same thing in ur actual app, but dont include the cpp files, just the header. And in the header, change dllexport to dllimport (as in above).

(sry if it was already clear)

Good luck..


March 01, 2005, 04:56 PM

You can export classes, but the names will be mangled. (extern "C" basically just turns off name mangling). This means you won't be able to import the DLL in a different compiler since name mangling is compiler specific; but if he's just developing a DLL for use with his own programs this wouldn't be an issue.

This thread contains 11 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.