Jump to content
  • Advertisement
Sign in to follow this  
TheMac

Exporting a class in a C++ DLL

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using Windows, and wondering how you can export an entire class from a dll instead of just functions and variables. I would like to do it by code, but if anyone knows how to do it with a .def file, that would be great too. Thanks, Shane

Share this post


Link to post
Share on other sites
Advertisement
If you use VC++ (6.0, I'm not sure about other version) an easy way to export classes is to define either to import or export the functions in a header file:



//basic include guards
#ifndef __FILE_H__
#define __FILE_H__

//this makes the same hader file functional for both the
//dll's code and the program that uses the dll
#ifdef _ISEXPORTING //some flag declared in your program to tell it if it's the DLL exporting the functions or not
#define PROGRAM_API _declspec(dllexport) //export the functions
#else
#define PROGRAM_API _declspec(dllimport) //import from dll
#pragma comment (lib, "program_lib.lib") //the lib that has info on the dll (compiled with the dll, make sure to copy into the program's folder, but it wont need it when the program's compiled)
#endif

//need to include some functions?
int PROGRAM_API function1(int input);

//how about a class? only need PROGRAM_API for the beginning of the class
class PROGRAM_API test_class{
public:
void onefunction();
private:
int onevariable;
};

#endif






To use the dll in the other program just copy the dll, lib, and .h file to the program that uses the dll, include the .h file, and you're all set.

Hope I could help
~zix~

EDIT: My horrible English!

[Edited by - zix99 on August 24, 2005 3:15:59 PM]

Share this post


Link to post
Share on other sites
Quote:

(6.0, I'm not sure about other version)


It also works in 2003, but I think it uses two _ before declspec.

I dont think there is a way to export DLLs in a .def file, but I could be wrong. If you cant use compiler dependent code then I think you need functions to create the class, ie:

class Foo;
extern "C"
{
Foo* CreateFoo(Foo* p)
{
return new Foo;
}
}



Of course you'ld also have to destroy the Foo objects in the DLL.

Share this post


Link to post
Share on other sites
I see just delclare the class to be exported just like the function, thanks. Whats up CTar!

Shane

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!