exporting classes in a DLL

  • Thread starter Thread starter Alfonso Morra
  • Start date Start date
A

Alfonso Morra

I am familiar with creating lean and mean WIN32 DLLS and exporting C
functions via a DEF file (or cconv decorators), but I am wndering how I
can export my C++ objects from my DLLs?

Ideally, I would be able to use the classes (and their methods) by
calling the objects and invoking their methods - is this possible, given
the fact that the C++ compiler decorates names ?

looking forward to an informed answer -tkx
 
// in your header...

class __declspec(dllexport) CDllTest
{
public:
CDllTest(){}
~CDllTest(){}

public:
void SayHello();
};

// in your cpp...

void CDllTest::SayHello()
{
printf(_T("Hello World!!"));
}

Another option: if you create a new dll project using VS it will create an
example dummy exported class.


--
Un saludo
Rodrigo Corral González [MVP]

FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org
 
Rodrigo said:
// in your header...

class __declspec(dllexport) CDllTest
{
public:
CDllTest(){}
~CDllTest(){}

public:
void SayHello();
};

// in your cpp...

void CDllTest::SayHello()
{
printf(_T("Hello World!!"));
}

Another option: if you create a new dll project using VS it will create an
example dummy exported class.

Thanks - but that's on the exporting side of things. I wanted to know
how to use the exported classes in a seperate project that imports the
classes and uses them. The code you gave above, only shows how to export
the classes (which I already know how to do).

mtia
 
The class declaration header file should look something like this :-

#ifdef EXPORTSCLASS
#define EXPORTCLASS_API __declspec(dllexport)
#else
#define EXPORTCLASS_API __declspec(dllimport)
#endif

class EXPORTCLASS_API CMyClass
{
public:
//...
};

When you use this in the DLL project, define EXPORTSCLASS, but refrain from
doing so in the EXE project where you call this dll.
 
Nishant said:
The class declaration header file should look something like this :-

#ifdef EXPORTSCLASS
#define EXPORTCLASS_API __declspec(dllexport)
#else
#define EXPORTCLASS_API __declspec(dllimport)
#endif

class EXPORTCLASS_API CMyClass
{
public:
//...
};

When you use this in the DLL project, define EXPORTSCLASS, but refrain from
doing so in the EXE project where you call this dll.
Thanks. this is what I was looking for.
 
Nishant said:
The class declaration header file should look something like this :-

#ifdef EXPORTSCLASS
#define EXPORTCLASS_API __declspec(dllexport)
#else
#define EXPORTCLASS_API __declspec(dllimport)
#endif

class EXPORTCLASS_API CMyClass
{
public:
//...
};

When you use this in the DLL project, define EXPORTSCLASS, but refrain from
doing so in the EXE project where you call this dll.

Nishant, I tried this (exported a single method) and then run dumbin
against my DLL. I find that the name of the method is mangled (as I had
suspected).

Two questions:

If I declare a FAR PASCAL calling convention (i.e. declspec(export)) at
the class level, do I get all the public methods exported "for free" or
do I have to manually export every public method in the classes public
interface?

2). If the names are mangled (ehich I suspect they will be because of
the overloading etc I have in my code), can I still use the unmangled
names of classes and methods in an application that links to the library?

tks
 
Alfonso said:
Nishant, I tried this (exported a single method) and then run dumbin
against my DLL. I find that the name of the method is mangled (as I had
suspected).

Two questions:

If I declare a FAR PASCAL calling convention (i.e. declspec(export)) at
the class level, do I get all the public methods exported "for free" or
do I have to manually export every public method in the classes public
interface?

OK I found the answer to do this. I just did this and the answer is yes.
2). If the names are mangled (ehich I suspect they will be because of
the overloading etc I have in my code), can I still use the unmangled
names of classes and methods in an application that links to the library?
The names are mangled as I had expected. I will try using the library
from another project and see if I can access the classses andntheir
methods using the undecorated names - unless you reply to this before
I'm through with my test ...
 
The names are mangled, but in the calling exe, if you include the header
file and add the lib (a lib is generated along with the dll) to the linker
list, you will be alright. Note that the lib does not contain the real
functions (it just contains definitions that the linker can use, so you need
to have the dll for the exe to run).
 
Back
Top