Solved

Import/Export class from Dll

Posted on 2004-04-30
8
4,626 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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