Results 1 to 10 of 10

Thread: C++ Class in windows dll

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    2

    Post C++ Class in windows dll

    Is it possible to put a c++ class into a windows dll and use it outside of the dll? If it is possible, how?
    Chimpy

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It is possible by using __declspec(dllexport) or __declspec(dllimport) respectively. But a dll created with e.g. VC++ can only be used by VC++ again.
    You must use implicit linking for this (list the .lib file that is generated during compilation of the DLL in the import library list of the project where you want to use the class).
    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.

  3. #3
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Or just create a static library and use the pragma in the file or project that uses it, like this:

    PHP Code:
    #pragma comment(lib, "library.lib") 
    on MSVC++
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Put that pragma into the class header file, that's the way MFC does it, it's quite useful.
    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.

  5. #5
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    but wouldnt that be a problem, i mean when you are compiling the the .lib? hmm, i do that too but I dont understand it very well, so to be safe, I started putting it in the .cpp of my main file. I guess I was right at first, I didnt see this in MFC or anywhere, I wanted a way to make sure a lib file was included automatically when you #include a header, so I searched google and found that MSVC has that pragma, and I was like WOWWWW... lol. But that tip didnt say where to put it, so I experimented and it was like everything worked but there where some weird things going on. So which way is the safe and correct way? The way MFC does it or the way I did it, and do you know of a pragma tutorial?

    Thanks,
    MoMad
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Since you usually have a special symbol defined when you compile a dll (that turns all DLLAPI or whatever macros into __declspec(dllexport) instead of import etc.) you can say

    #ifdef DLL_INTERNAL_COMPILE
    #define DLLAPI __declspec(dllexport)
    #else
    #define DLLAPI __declspec(dllimport)
    #pragma comment(lib, "mylib.lib")
    #endif

    Put it in some central header file that it is included by each and every file of your dll.
    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.

  7. #7
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    O cool, but I never use the pragma with the dll, I almost always use dynamic linking (LoadLibrary) when dealing with dynamic link libraries, I only use the pragma with the static link libraries.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    #ifndef LIB_INTERNAL_COMPILE
    #pragma comment(lib, "mylib.lib")
    #endif
    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.

  9. #9
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Originally posted by CornedBee
    #ifndef LIB_INTERNAL_COMPILE
    #pragma comment(lib, "mylib.lib")
    #endif
    Huh? WHats LIB_INTERNAL_COMPILE??
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A symbol you define via a /d switch when compiling your static library. The name is up to you of course.
    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.

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