Click to See Complete Forum and Search --> : Exporting a class (--> .def file)
It is possible to export functions of a DLL, but how to export a whole class containing functions and variables ?
Thanks. (it would be nice to mail me, I don't watch these Forums very frequently : fury95@cyberdude.com)
Rob Wainwright
June 15th, 1999, 03:33 PM
The only way to export C++ classes from a DLL within the Windows environment and using
VC++ is to implement your DLL as an MFC extension DLL. This can be generated very quickly
(I think) from the wizards. Then it is just a matter of decorating the class name with some keyword
that escapes me. The only thing is that this ties you into MFC.
I think the reason that you have to have some special trickery is related to the extra bumpf that the compiler
carries around with classes (i.e. the this pointer and the v-table).
HTH
Rob.
Wayne Fuller
June 15th, 1999, 04:46 PM
Rob is right, the following will tie you to MFC in order to export a whole class.
class AFX_EXT_CLASS CMyClass
{
// Do whatever
};
Hope that is what you are talking about,
Wayne
P.S. If you are just trying to export certain functions, say public functions, without MFC, generate a MAP file,
and then add the names to the def file.
John Holifield
June 15th, 1999, 05:07 PM
Since this question seems to get asked every day, I thought perhaps someone could find this usefull. This is the method I prefer for exporting (and importing) custom classes in DLLs. Either add this header file to your project, or put similar code in your projects main header file. Then go in your Project Settings and add MY_DLL_DLL as a preprocessor definition. Obviously this is just a sample, so change MY_DLL_DLL to something more appropriate to your project.
/*
* MyDLLBase.h
*
* Shows how to import/export classes properly
* in a DLL.
*
*/
#ifndef __MY_DLL_H__
#define __MY_DLL_H__ 1
#ifdef __cplusplus
#include <assert.h>
extern "C" {
#endif
#ifdef WIN32
#include <Windows.h>
/*
* MY_DLL_DLL is defined only when we build
* the library itself
*/
#if defined(MY_DLL_LIB)
#define MYCLASSDECLSPEC
#elif defined(MY_DLL_DLL)
#define MYCLASSDECLSPEC __declspec( dllexport )
#else
#define MYCLASSDECLSPEC __declspec( dllimport )
#endif
#else
#define MYCLASSDECLSPEC
#endif
/*
* DLL Entry/Exit Point
*/
BOOL WINAPI DllMain(HANDLE , DWORD , LPVOID );
#ifdef __cplusplus
}
#endif
#endif /* __MY_DLL_H__ */
Now in the header files for the class(es) you want to export, change the class definitions similar to this:
class MYCLASSDECLSPEC AClass
{
public:
AClass();
~AClass();
// blah, blah, blah...
};
All you have to do is add the MYCLASSDECLSPEC to your class definitions and you're good to go. Plus you get the added benefit of reusing the header files in other projects where you'll actually use the exported classes.
John Holifield
June 15th, 1999, 05:10 PM
It does not have to be an MFC Extension DLL, just a DLL.
two cents...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.