Solved

Import/Export class from Dll

Posted on 2004-04-30
8
4,643 Views
Last Modified: 2012-05-04
I have a dll, and i have an exe.

Now the dll is a plugin (so absolutely NO .lib import!!).


I have a class in the dll (MyDb). Where the header-file is shared by both the exe and the dll.
And in the dll I have a function MyDb* CreateDatabase( filename ); which does nothing more then "return new MyDb(filename);".


Now when I use this MyDb in my exe, of course it gives linker errors about unresolved symbols...


But now is the question... How can I solve this so that my exe knows that it needs to look for those functions in the dll, and not in the exe?


Thanks
0
Comment
Question by:G00fy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10959337
This should be clear from the header file. The first lines should look like

#ifdef YOURDLL_EXPORTS
#define YOURDLL_API __declspec(dllexport)
#else
#define YOURDLL_API __declspec(dllimport)
#endif

And every function which needs to be seen from outside the lib is declared in the header like
YOURDLL_API void foo (int);

Now the linker knows that this function is inside a DLL and should not complain. You just need to make sure that you are getting the function address from the DLL before calling the function.
0
 
LVL 86

Expert Comment

by:jkr
ID: 10959421
>>Now the dll is a plugin (so absolutely NO .lib import!!).

If you have no .lib,  __declspec(dllexport) will not work for you. The Common solution is pure virtual (abstract) base classes that serve as interfaces, e.g.

// common header file

struct IMyDB {

    virtual int DoSomething ( int, char*) = 0;
};

Then, you would provide an implementation in your DLL that returns an interface pointer:

class MyDB : public IMyDB {

    virtual int DoSomething ( int, char*);
};

//...

extern "C"
__declspec(dllexport)
IMyDb* CreateDatabase( filename ) { return new MyDB;} // load this one via 'LoadLibrary()'/GetProcAddress()'
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10959600
Or try loading the dll dynamically and then get the exported function. So you won't need any linking.

//this is the imported CreateDatabase
typedef MyDb* PFN(char *);  //or what you mean by 'filename'

HINSTANCE hDll = LoadLibrary("yourdll.dll");
PFN *pfn;

if(hDll)
{
  pfn = (PFN*)GetProcAddress(hDll, "CreateDatabase");
  if(!pfn)
  {//error founding proc
     FreeLibrary(hDll);
     .......
  }
  else
  {
    pfn(..);  //do your call
  }
}

....

FreeLibrary(hDll);  //somewhere
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 10959882
>>So you won't need any linking.

But MyDB would still be "unresolved".
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10959944
right
i didn't see your post by the time i did mine
0
 

Author Comment

by:G00fy
ID: 10964313
Sorry for my late respons, but your answer is the best jkr.

Only 1 thing is left ;-).


I created the class via CreateDatabase, this returns a pointer. Now when I try to delete it at the end of my exe, my program says "unhandled exception". Is it because this memory is allocated inside the dll?

Oh and btw, the IMyDb has 2 static functions and 1 static variable, all the others are virtual (except the constructor). Maybe that is why this behaves like that?
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 10964339
>> Now when I try to delete it at the end of my exe, my program says "unhandled exception". Is it because this
>>memory is allocated inside the dll?

Yup, indeed. Either export another function that does a 'DeleteDatabase()' or - what I would choose - add a method 'Release()' to the interface and have it perform a

MyDB::Release() {

    delete this;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 10964348
The following link might be helpful.
http://www.codeguru.com/Cpp/W-P/win32/article.php/c1443
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question