CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    __declspec / DLL / C++ confusion

    I just came across this article. If I'm reading it right, the author seems to suggest that one cannot export C++ classes (either in whole or in part) from a DLL (i.e. using the __declspec modifier). Instead, you should use a C style function to create an instance of the class and export that instead. However, a quick search of this forum reveals loads of posts which suggest the opposite. Who's right and who's wrong?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: __declspec / DLL / C++ confusion

    You can not export a class, but you can export the methods and variables individually, you can also export these with C++ way of exporting. You have Visual C++ project types like MFC DLLs that export classes.

    The mechanism of exporting a method with C style which returns an instance of given class is a very much tradditional way of doing it.
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: __declspec / DLL / C++ confusion

    Quote Originally Posted by John E View Post
    I just came across this article. If I'm reading it right, the author seems to suggest that one cannot export C++ classes (either in whole or in part) from a DLL (i.e. using the __declspec modifier). Instead, you should use a C style function to create an instance of the class and export that instead. However, a quick search of this forum reveals loads of posts which suggest the opposite. Who's right and who's wrong?
    You read it wrong. This is the quote:
    You can not directly export a C++ class from a dynamically loadable DLL.
    The main point author makes is about plug-ins, i.e. a situation when dynamic dll loading [Edit: and explicit linkage!] is unconditionally implied.

    Indeed, you just cannot explain your compiler that you intend to link your classes constructors/destructors/members later at runtime, somehow, maybe... That's why C-style export is mandatory for plug-ins. Besides, plug-ins are most of the times about cross-language usage, which is another reason for C-style export.

    But in case you never need your dll to be loaded dynamically, you surely may export/import classes with no problem (well, problem might happen, but in this case it's gonna be very specific )
    Last edited by Igor Vartanov; September 29th, 2010 at 09:29 AM.
    Best regards,
    Igor

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: __declspec / DLL / C++ confusion

    Sorry Igor, you've confused me now. ALL dll's are dynamically loadable aren't they? Or are you referring to DLL functions loaded using LoadLibrary/GetProcAddress etc?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: __declspec / DLL / C++ confusion

    Yes, and thank you for noticing that. Been talking about 'dynamic loading' I meant (and I believe, the author did as well) explicit linkage by means of GetProcAddress. Maybe that was the main point of your confusion from the very beginning.
    Best regards,
    Igor

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: __declspec / DLL / C++ confusion

    Yes, I think it was, now you've pointed it out..!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: __declspec / DLL / C++ confusion

    Another question along the same lines....

    With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: __declspec / DLL / C++ confusion

    Quote Originally Posted by John E View Post
    Another question along the same lines....

    With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky
    The DEF applies to functions only. With regard to classes, it's almost not worth exporting a class unless you [re]compile the dll each time you compile the app. The name mangling must match exactly and can change from compiler version to version.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: __declspec / DLL / C++ confusion

    Quote Originally Posted by John E View Post
    Another question along the same lines....

    With a standard 'C' source file, a .DEF file can be used to export functions if the program was originally written without __declspec modifiers. Can the same be done for C++ (i.e. class member) functions? I've never seen it done but it'd be interesting to know whether or not it's possible. At the very least, I assume that name mangling is going to make this tricky
    Anyway, it's possible. Though not without certain limitations...
    Attached Files Attached Files
    Last edited by Igor Vartanov; October 1st, 2010 at 03:10 PM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured