Advertisement Jump to content
Sign in to follow this  

Exporting a class in a C++ DLL

This topic is 4900 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
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
#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)

//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{
void onefunction();
int onevariable;


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

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

(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!


Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!