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

March 01, 2005, 04:57 AM

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

 
Marmakoide

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.

 
Moe

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:

  1.  
  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
  8.  

Then use it to declare your classes.

 
Marmakoide

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

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


In my .cc file
  1.  
  2. #include "Entity.h"
  3.  
  4.  
  5.  
  6. engine::Entity::Entity() {
  7. ...
  8. }
  9.  
  10.  
  11.  
  12. engine::Entity::~Entity () {
  13. ...
  14. }
  15.  
  16.  
  17.  
  18. void
  19. engine::Entity::doAction() {
  20. ...
  21. }
  22.  

 
Hesterumpe

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

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


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

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


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: http://www.flipcode.com/articles/article_winplugins.shtml. It was very helpfull to me. It also gives a nice way to work around the inability to export classes.

Hope this helps :)

 
Moe

March 01, 2005, 02:03 PM

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

Declare functions like this:

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


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


And donít forget to add an export function for your class.
Maybe this helps too:
http://www.flipcode.com/articles/article_winplugins.shtml

 
Marmakoide

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.

 
Vast

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

 
Reedbeta

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.