MFC DLL Export class2011-06-15 10:54 2718 People read comments (0) favorite reports Dllmfcinterfaceparameterslibrarynull
Method 1:
On the VC, a new MFC DLL project named DLL.
The first step is to create a interface.h file to define the interface with the following code:
File interface.h
#ifndef _interface_h_
#define _interface_h_
Interface ITest
{
virtual int Print () = 0;
Virtual ~itest () {};
};
itest* dllcreatetest ();
void Dlldestroytest (ITest *ptest);
#endif
The second step is to define a class ctest that inherits from the interface interface, with the following code:
File test.h
#ifndef _test_h_
#define _test_h_
#include "interface.h"
Class Ctest:public ITest
{
Public
CTest ();
Virtual ~ctest ();
int Print ();
}
#endif
File Test.cpp
#include "StdAfx.h"//Note that this header file needs to be included, otherwise it will be reported fatal error c1010:unexpected end of file while
Looking for precompiled header directive
#include "test.h"
Ctest::ctest ()
{
}
Ctest::~ctest ()
{
}
int CTest::P rint ()
{
printf ("ok!/n");
return 0;
}
The third step, in the Dll.cpp file, implement the Dllcreatetest and dlldestroytest two functions, the code is as follows:
File Dll.cpp
......
itest* Dllcreatetest ()
{
return new CTest ();
}
void Dlldestroytest (ITest *ptest)
{
if (pTest! = NULL) Delete pTest;
PTest = NULL;
}
The fourth step, is also the most easy to ignore the step, and so on when the above operations are completed, but also in the Dll.def file, the function to be exported dllcreatetest and dlldestroytest add in, as follows:
; Dll.def:Declares the module parameters for the DLL.
LIBRARY "DLL"
DESCRIPTION ' dll Windows Dynamic Link Library '
Exports
; Explicit exports can go here
Dllcreatetest
Dlldestroytest
At this point, the MFC DLL project has been completed. So how do I invoke a generated DLL in another project?
Dllcreatetest obtains a global variable g_test, and then uses G_test to invoke each virtual function.
ITest do not necessarily have to be pure virtual functions!
MFC DLL Export Class