?
Solved

Import/Export class from Dll

Posted on 2004-04-30
8
Medium Priority
?
4,649 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

765 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