Solved

Import/Export class from Dll

Posted on 2004-04-30
8
4,605 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>So you won't need any linking.

But MyDB would still be "unresolved".
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

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

Author Comment

by:G00fy
Comment Utility
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
Comment Utility
>> 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
Comment Utility
The following link might be helpful.
http://www.codeguru.com/Cpp/W-P/win32/article.php/c1443
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

9 Experts available now in Live!

Get 1:1 Help Now