How Do I . . . Make a Class Accessible from a DLL

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

How Do I . . . Make a Class Accessible from a DLL

 
0
  #1
Nov 20th, 2004
How do you make a class accessible from a dynamic link library? It is a commonly asked question with a solution that is surprisingly simple. In this article I will show you how to do it in Visual C++.

First, you need to create a new project using the Win32 Dynamic-Link Library project wizard.

Then, create a new class. Call it CInDLL. We now have a class with a constructor and a destructor.

We want to access this class from the DLL, so we need to export it. When the DLL is built, it also builds something called an export library. The export library is similar to a regular library. It contains all of the information necessary to dynamically link to the DLL at runtime. When we export a function or a class, the function name and location is stored in the export library. The program using the DLL links in the export library to access the DLL.

There are two ways to export a function or class from a DLL. One is to use the __declspec(dllexport) directive. The other is to use definition files. The former is easier and we will be covering it first.

Change the class definition to look like the following:
[php]class __declspec(dllexport) CInDLL[/php]

Compile the DLL. Everything should work correctly. If you look in your destination directory you should see the DLL and export library for this project. Congratulations, you have just created your first DLL!
We now need to use the DLL.

Create a new Win32 Console Application in the same directory and workspace. Call it TestClassInDLL. Be sure and make it dependent on ClassInDLL. Watch the directory. Put it in the same directory as the DLL - not below it. Open the project settings. Be sure you are editing the Debug build settings. Add Debug\ClassInDLL.lib to the list of Object/Libaray Modules under the link tab. Add a corresponding file for the release build.

Create and add a file called TestClassInDLL.cpp with the following code:[php]
int main()
{
return 0;
}[/php]


The program should compile although it obviously doesn't do much. We need to import the class from our DLL into the program. We are currrently not set up to do that. Where the DLL had to export the class, the application must import it by adding the expression __declspec(dllexport) in the class definition. We want to modify the header file to serve both purposes.

Go back to the DLL project and add a file called ClassInDLL.h with the following source: [php]
#ifndef ClassInDLL_H
#define ClassInDLL_H

#ifdef _CLASSINDLL
#define CLASSINDLL_CLASS_DECL __declspec(dllexport)
#else
#define CLASSINDLL_CLASS_DECL __declspec(dllimport)
#endif

#endif // ClassInDLL_H[/php]



This creates a macro that, depending on a #define, switches between importing and exporting. Add the _CLASSINDLL preprocessor to your DLL project settings under C/C++-Preprocessor. Include ClassInDLL.h in InDLL.H and change the CInDLL declaration to the following: [php]
class CLASSINDLL_CLASS_DECL CInDLL[/php]

Include InDLL.h in TestClassInDLL.cpp. The DLL defines _CLASSINDLL so that CLASSINDLL_CLASS_DECL becomes an export instruction. The application does not; therefore, CLASSINDLL_CLASS_DECL becomes an import instruction by default. CInDLL is now accessible from your application. Create an instance of it in your main function: [php]
int main()
{
CInDLL classFromDLL;

return 0;
}[/php]



Rebuild everything and run the program. Be sure you rebuild the DLL first!

You can see that it doesn't do much. Let's add a function to CInDLL. You don't need to do anything special. Just add the method prototype in the header, the function body in the cpp file, and any necessary include files: [php]void CInDLL::TestIt()
{
cout << "Hello World from a DLL." << endl;
}[/php]



Call Testit() from your application. Compile and run the DLL and application. Congratulations, you have just put a class in a DLL.
Attached Files
File Type: zip class_dll.zip (3.7 KB, 795 views)
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #2
Dec 15th, 2004
any idea how to do it in other compilers, or do it i general?? i have devc++ and i believe the required code might be differeny...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #3
Dec 17th, 2004
had a go in dev c++, was the same as everything was pre-defined (__declspec ect...) but the template put a BOOL APIENTRY main function in mine with different states? is that a basis of threading? (it had things like attaching / detaching ect.. in it)
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1
Reputation: raz_1 is an unknown quantity at this point 
Solved Threads: 0
raz_1 raz_1 is offline Offline
Newbie Poster

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #4
Feb 28th, 2005
good job - no BS - straight to the point and well explained
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: hakkuss is an unknown quantity at this point 
Solved Threads: 0
hakkuss hakkuss is offline Offline
Newbie Poster

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #5
Jun 11th, 2008
Well you use "library".h and don't import anything , this is a waggish example. He-he, but I used some of what you show here, thanks a lot.
Last edited by hakkuss; Jun 11th, 2008 at 10:09 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 36
Reputation: Necrolis is an unknown quantity at this point 
Solved Threads: 4
Necrolis's Avatar
Necrolis Necrolis is offline Offline
Light Poster

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #6
Jun 12th, 2008
Originally Posted by 1o0oBhP View Post
had a go in dev c++, was the same as everything was pre-defined (__declspec ect...) but the template put a BOOL APIENTRY main function in mine with different states? is that a basis of threading? (it had things like attaching / detaching ect.. in it)
This is how the dll is loaded, it has a switch for the type of call(loadlibaray or freelibrary), use this if you need to init/deinit anything at those points
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 2
Reputation: hakkuss is an unknown quantity at this point 
Solved Threads: 0
hakkuss hakkuss is offline Offline
Newbie Poster

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #7
Jun 13th, 2008
Originally Posted by Necrolis View Post
This is how the dll is loaded, it has a switch for the type of call(loadlibaray or freelibrary), use this if you need to init/deinit anything at those points
Ok, now I understand how this is working. Till now I used dll's only in C# and it is quite different from C++. I used yours indications and this works, thanks.
Last edited by hakkuss; Jun 13th, 2008 at 2:28 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1
Reputation: isaace is an unknown quantity at this point 
Solved Threads: 0
isaace isaace is offline Offline
Newbie Poster

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #8
Sep 17th, 2008
Hi,
Can someone add an example ot a project that use the DLL, I get the following error
error LNK2001: unresolved external symbol "__declspec(dllimport)

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How Do I . . . Make a Class Accessible from a DLL

 
0
  #9
Sep 17th, 2008
Yes, it's the simple truth but not the complete truth, see also
http://www.codeproject.com/KB/cpp/ho...p_classes.aspx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC