Exporting DLL symbols using .def file and Name mangling (Windows)

Started by
5 comments, last by spree 16 years, 3 months ago
Hi Everybody, I"m using MS VS2003, and tryig to create a basic dll + lib file without a wizard. I need help with creating a correct .def file for my exports. In the code below I wish to export the constructor, destructor and the printX methods only. The test.h file:

#ifndef _TEST_
#define _TEST_
#define _TEST() test a;
class test
{
public:
test(int);
~test();
private:
int x;
public:
void printX();
};
#endif

-------------------------------------------------------------- The test.cpp file: -------------------------------------------------------------

#include "test.h"

#include <stdio.h>

test::test(int _x):x(_x)

{

printf("test x(%d)\n",x);

}

test::~test()

{

printf("~test x(%d)\n",x);

}

void test:rintX()

{

printf("%d\n",x);

}

------------------------------------------------------------------- And I need help with creating my .def file (test.def) ------------------------------------------------------------------ LIBRARY "test.dll" EXPORTS what goes here? Thanks!!!
;)
Advertisement
I don't think C++ offers a way to explicitly export class members. I can't see how this would be useful either, as no interfaces exist to bind to a type library at run-time (unlike function resolution, which is available via GetProcAddress and its kin).

Simply declare the class as a DLL-export and the linker will take care of the rest:

class __declspec(dllexport) test {
...

Now the class's public members will appear in the binary's export table (mangled) and all the appropriate symbols will be produced for the library definition.
Ring3 Circus - Diary of a programmer, journal of a hacker.
I found something on the net http://www.kegel.com/mangle.html#operators
And this document suggests that my constructor and destructors are to be mangled like so:

LIBRARY "test.dll"EXPORTS??0test@@QAE@XZ ??1test@@QAE@XZ 


This .def file links without errors, I shall test this dll now and hopefuly it wokrs :)
;)
It worked :)
;)
Uh oh, you do not go around writing down mangled names in your DEF files. It is inherently unportable, as mangling is *compiler specific*. That mangling scheme will compile with nothing other than your own compiler. Congratulations.

Since C++ mangles symbol names, for exporting with def files, you need to turn off symbol mangling. C has no name mangling, so wrap your code around in extern "C" { ... }
The correct way of working with classes in DLLs is to provide a C interface which instantiates the class and returns a pointer to it. That way, you don't need to export constructors/destructors.

What you basically do is this:
extern "C"{    CTest *CreateTest();    void FreeTest(CTest *p);}


When you need a CTest instance, you call CreateTest() in your code, and voila, you have an instance of said class.

Toolmaker

Its OK by me that it will only work with my compiler.
If it was not a specific requirment I'd just go with the wizard build of the dll.

Another thing, I saw some def files which export the name of the functions unmangled, but it did not work for me though...
;)

This topic is closed to new replies.

Advertisement