Re: How to export afunction in class?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



More explanation is needed.

Doing the __declspec(dllexport) is risky; you get into trouble when you try to use it. It
must be __declspec(dllimport) in the client modules.

There's a directive that exports the entire class, You should consider

class AFX_EXT_CLASS Point {
....
};

then in the implementation file, before you include the header file, do

#define _AFX_EXT

While superficially it appears that these apply only to MFC Extension DLLs, it appears
that they would work anywhere. If _AFX_EXT is defined, they turn into
__declspec(dllexport) otherwise they are __declspec(dllimport)

You can export individual members by doing

class Point {
public:
AFX_EXT_CLASS void dsp();

I suggest you look up AFX_EXT_CLASS in the MSDN and follow the link to importing and
exporting of classes.


The comment says that it means you don't have to use a .def file with the decorated name.

And that's part of the problem

EXPORTS
dsp

is fundamentally meaningless because the symbol dsp does not exist anywhere in your
program, as far as the linker is concerned.

Also, as long as we're talking about fundamentally meaningless information, comments like
"can't compile" are fundamentally meaningless as well, because it conveys absolutely
nothing useful. Did you get a compilation error? Or did you mean "fails to build"
because you got a linker error? Without seeing the exact messages, and if they have
source line references, a description of the exact source line and possibly preceding
lines, there is no way to know what went wrong.

Since you are doing a __declspec(dllexport) [and the AFX_EXT_CLASS macro is basically
that] there is no need to have a separate EXPORTS declaration in the .def file; it is
pointless. I've done a lot of DLLs, and I've not needed to put anything in the .DEF file
since the days of Win16. There are occasional reasons to use a .DEF file (mapping names
to indices for large DLLs, for example) but you are nowhere near that set of problems.
Therefore, you should ignore the existence of the .DEF file.
joe




On Tue, 6 Feb 2007 20:22:05 +0800, "Lee Tow" <fbjlt@xxxxxxxxxxxxx> wrote:

Hello all:
Look:
#include <iostream.h>
class Point
{
public:
__declspec(dllexport) void dsp();
};

void Point::dsp()
{
cout<<"Hello"<<endl;
}

I find that export the function dsp() and its name is mangled,and then
I write a .def file,Look:
LIBRARY DLL
EXPORT
dsp

but it cann't compile,I want to know how to export a function in class
and it don't have name mangling?
Thank you every much.

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.