Solved

Import/Export class from Dll

Posted on 2004-04-30
8
4,614 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
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
 
LVL 86

Expert Comment

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

But MyDB would still be "unresolved".
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now