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

    how to export an class in a regular DLL?

    I have a regular dll using MFC as shared library and it exports some API functions.

    Now i want it to export an class. But i met some troubles.

    It seems that the constructor and destructor of the exported class cannot coexist with the virtual functions.

    If both of them exist in the class, the compiler generate an link error says:
    error LNK2019: unresolved external symbol "__declspec(dllimport) const CXXXX::`vftable'" (__imp_??_7CXXXX@@6B@) referenced in function "public: __thiscall CXXXX::CXXXX(void)" (??0CXXXX@@QAE@XZ)

    if i delete the constructor and the destructor, it's OK.
    or if i delete the virtual functions. it's OK, too.

    what's the problem? how to solve it?

    thank you!!!

  2. #2
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    Is it that you're just compiling the dll or is it linked with someother exe project where you;re using that dll? If so, import it in the exe project and export it in dll project.

    you know what i mean?
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  3. #3
    Join Date
    Dec 2002
    Posts
    214
    ya, i know what u mean..
    but the problem is not like what u'r talking about.

    the problem occurs when the DLL is compiled..

  4. #4
    Join Date
    May 2004
    Posts
    4
    "delete construct destruct func
    or delete virtual func" is ok?
    then a c++ class can be exported with using regular dll?


    i used to think only the ext dll can export a c++ class;
    there's some problem of sth like func name trans or linking
    when using other methods ?

    am i wrong?
    Last edited by FirstPerson; May 10th, 2004 at 05:47 AM.

  5. #5
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    If both of them exist in the class, the compiler generate an link error says:
    error LNK2019: unresolved external symbol "__declspec(dllimport) const CXXXX::`vftable'" (__imp_??_7CXXXX@@6B@) referenced in function "public: __thiscall CXXXX::CXXXX(void)" (??0CXXXX@@QAE@XZ)
    if you're compiling dll, from the above error, you seem to be importing the class. shouldn't you be exporting. i'm sorry if i'm barking at the wrong tree.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  6. #6
    Join Date
    Dec 2002
    Posts
    214
    FirstPerson, please see the MSDN at
    TN011: Using MFC as Part of a DLL

    here the words from that note.
    Regular DLLs assume interfaces between the application and the DLL are specified in normal C-like functions or explicitly exported classes. MFC class interfaces cannot be exported.

    if you're compiling dll, from the above error, you seem to be importing the class. shouldn't you be exporting. i'm sorry if i'm barking at the wrong tree.
    But the class is in this DLL. it's very simple to test what i described. Create a regular DLL which VC provides the simplese one. and add the following class:
    Code:
    .h file
    class AFX_EXT_CLASS CTest{
    CTest();
    virtual void f();
    }
    
    .cpp file
    CTest::CTest()
    {
    }
    
    void CTest::f()
    {
    }

  7. #7
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    Well I am not sure if u link dynamically (not even using the lib) u can export a class just by export keyword. The way I do when i have to export a class outof a DLL (the dll will linked dynamically) I export two functions
    1. First that returns a pointer to the class Obj after newing it.
    2. And another function that takes the pointer to that class and deletes it.

    And I declare all the functions of that class to be virtual. Another advantage is that memory allocation/deleting is handled within the same module.
    Hope this helps,
    Usman.

  8. #8
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    But the class is in this DLL. it's very simple to test what i described. Create a regular DLL which VC provides the simplese one. and add the following class:
    Well, it doesn't matter where the class is. It all depends on where the header file is being compiled from.

    if you also have another project in the same workspace as the dll is, then make sure that you've not forgoten to make your main project a dependent of dll. See "Project->Dependencies" menu.

    If you have just DLL in your work space and you're trying to compile just that, then probably you might not have _AFXEXT #defined. If so, it would have AFX_EXT_CLASS would haev meant export (not import). If this is the case, add _AFXEXT in Pre-processor definitions of C++ tab in projects settings.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  9. #9
    Join Date
    Dec 2002
    Posts
    214
    thanks, Indian_Techie..

    u're right.

    AFX_EXT_CLASS was defined as export only if _AFX_EXT was defined..

    ok, i will use
    __declspec(dllexport)

    directly..

    thanks.

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