CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 1999
    Posts
    19

    How do I export a class?

    Hiya!

    I have created a MFC extension dll and in that dll I would like to have a class that I would like to export to my main program.
    Can I just send in an reference and assign it to my class in the dll and then use it in my main program, of course delete it as well in the main program.

    How do I load my dll in the main program?

    TIA
    Magnus


  2. #2
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    dllexport

    To export your class use the following :

    #define DllExport __declspec( dllexport )

    class DllExport CMyClass
    {
    };

    Now CMyClass can be used by other applications when you link your application with the generated .lib file of your dll.


  3. #3
    Join Date
    May 1999
    Location
    Toulouse, France
    Posts
    171

    Re: How do I export a class?

    a way to do this is to use dllexport / dllimport.
    for example:
    class __declspec(dllimport) CMyEdit: public CEdit{...} //used in your main program
    class __declspec(dllexport) CMyEdit: public CEdit{...} // used in your DLL.

    Don't forget to add the lib file of your DLL in the setting of your main project.

    dllimport / dllexport "loads" and "unloads" the DLL implicitly in your main prog., so you don't have to care about this.

    you may find more infos in the docs.

    HTH.

    K.



    Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.
    We're talking ****, 'cause life is a 'biz
    You know it is
    Everybody tryin' to get rich
    God ****!
    All I wanna do is live !

    KoRn, Children of the Korn

  4. #4
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: How do I export a class?

    To expand a little on what Karl wrote, I often do this in DLLs.

    #define DllExport __declspec(dllexport)
    #define DllImport __declspec(dllimport)

    #ifdef MY_LIBRARY_CODE // define this in project of DLL
    #define DllClass DllExport
    #else
    #define DllClass DllImport
    #endif

    class DllClass CMyClass
    {
    ...
    };

    When MY_LIBRARY_CODE is defined, which should only be in the library's
    project, then DllClass is exported. All other times it is imported.
    Also, check out post 1286 on this board. It was on page 44 for me.
    You can then use the class just like any other class.



  5. #5
    Guest

    Re: How do I export a class?

    I had the same question about Importing Classes from a DLL. Thanks!

    Now my question is: Can this import be done explicitly (i.e. using GetProcAddress etc...) and not requiring the .LIB file?

    Thanks,

    Jeff


  6. #6
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: How do I export a class?

    Technically, yes but it is far from easy.
    Each member of the class will usually be exported individually
    and if you throw in name-mangling things get very difficult.
    Try "dumpbin /exports SomeLibrary.dll" to see what I mean.
    There have been several posts and a few KB articles on this
    topic. They might be more encouraging than I am.



  7. #7
    Join Date
    Mar 2001
    Posts
    5

    Re: dllexport

    Hi,
    I have a question.
    what about exporting class from main program to DLL..
    How should that work...

    Thanks
    amit



  8. #8
    Join Date
    Oct 2000
    Location
    Ottawa, CANADA. (CNADA ROCKS!!)
    Posts
    1,895

    prescribed way to export a class

    extern "C" AFX_EXT_API void WINAPI YourDllName();


    class AFX_EXT_CLASS CYourClassName : public xxxx
    {
    }

    Then in your main program, you include the .lib from the dll,
    include the header for the class taht you are exporting and just use it as normal.

    Rate this post if helped.
    and Canada rocks!! Peace bro.

  9. #9
    Join Date
    Jun 2000
    Posts
    351

    You can export a class from an *.exe just as....

    well as a *.dll.

    #ifdef YOUR_EXE_BUILD
    #define YOUR_EXPORT_API __declspec(dllexport) // works with *.exe's too
    #else
    #define YOUR_EXPORT_API __declspec(dllimport)
    #endif // YOUR_EXE_BUILD

    class YOUR_EXPORT_API CExample
    {
    // normal
    };



    And an import library will be made for your *.exe just like one for a *.dll. You can also export from an *.exe using a .def file, just like a *.dll as well.


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