MFC DLL Export Class

Source: Internet
Author: User
Tags export class

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.