Results 1 to 21 of 21

Thread: Class in a DLL

  1. #1

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Put the definition of the class in a header file and include it in whatever you want to share the class with. If you want an instance of the class (an object), you can put that in a DLL and share it.
    Harry.

    "From one thing, know ten thousand things."

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, with vc++ you best create a sample win32 dll that exports a few symbols and see how it's done.
    With others I don't know how, but it's surely possible.

    All the buzzt
    CornedBee

  3. #3
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Vlatko: You mean just include it like any normal file and compile it into the program?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Umm do you mean me?

    Anyway, yeah just include the header file in whatever C++ project with which you want to access the DLL containing the shared object.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Yeah, sorry, for some reason I thought you were him.

    Anyway, I don't know what you mean. I know how to include the file, link it, etc., but how would you set it up to export the class?
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What do you mean 'export the class' ? You can put an instance of a class in a DLL and share it with your exe code. You don't get the class definition from the DLL.

    I think if you made the class COM-compatible you could probably register it with Windows and let Windows deal with it.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    class __declspec(dllexport) MyClass {
        // Contents here
    };
    Use dllimport for the import header file, and dllexport when creating the DLL. You'd need to link with the created .lib file when you make the DLL.

    Oh, and I think you need to use the same version of VC++ for the DLL and the using program.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    What would you put in the header file if you're exporting the class definition from the DLL?
    Harry.

    "From one thing, know ten thousand things."

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    __declspec(dllexport) as I said earlier
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok, the DLL compiles fine, but I get a link error when I try to write a simple program using the class. I attached my source so you can see where it went wrong.

    error LNK2001: unresolved external symbol "__declspec(dllimport) class MyClass inst" (__imp_?inst@@3VMyClass@@A)
    Attached Files Attached Files
    Alcohol & calculus don't mix.
    Never drink & derive.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You need two copies of the header. One to create the DLL, and one to use it. I use a preprocessor directive to decide what to use, that way I can get by with the same header. A specific symbol is defined in project settings for the DLL project.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    What preprocessor directve would that be?
    Alcohol & calculus don't mix.
    Never drink & derive.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, something like this:
    Code:
    #ifdef _IKDLL_INT
    #define IKDLL __declspec(dllexport)
    #else
    #define IKDLL __declspec(dllimport)
    #endif
    
    class IKDLL MyClass {
    // blah
    };
    Then in project settings add _IKDLL_INT to the list of predefined symbols. Pretty simple really
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    error C2071: 'inst' : illegal storage class
    Alcohol & calculus don't mix.
    Never drink & derive.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    this one works

    note that i set the library to single threaded, because I forgot to install the multithread CRT dlls


    All the buzzt
    CornedBee

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    *sigh*

    forgot again
    Attached Files Attached Files

  17. #17

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ah right that code was meant for the headers not the DLL.....

    Hang on a minute... that's what I was suggesting in the first place, minus the __declspec bit. What's that for that just including the class definition wouldn't do?
    Harry.

    "From one thing, know ten thousand things."

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It tells the linker not to search for the class in a normal library or module, but rather the import library. (I think)
    Also, I'm not sure if I posted the right files before. Tell me if not.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Don't you still give the class definition in the header though? Or do you only define the interface and leave the implementation in the library?
    Harry.

    "From one thing, know ten thousand things."

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The implementation is in the library.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Gotcha. Thanks
    Harry.

    "From one thing, know ten thousand things."

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