zoukankan      html  css  js  c++  java
  • MFC DLL导出类

    方法1:
       在VC上new一个名为dll的MFC DLL工程。

         第一步,创建一个interface.h文件来定义接口,代码如下:

        //file interface.h

        #ifndef _INTERFACE_H_

       #define _INTERFACE_H_

        interface ITest

        {

            virtual int Print()=0;

            virtual ~ITest(){};

        };

        ITest* DllCreateTest();

        void DllDestroyTest(ITest *pTest);

       #endif

       第二步,定义一个继承自接口interface的类CTest,代码如下:

       //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"     //注意这里需要包含这个头文件,否则会报fatal error C1010: unexpected end of file while

                                             // looking for precompiled header directive

       #include "test.h"

        CTest::CTest()

       {

       }

       CTest::~CTest()

       {

       }

       int CTest::Print()

       {

          printf("ok!/n");

          return 0;

       }

       第三步,在dll.cpp文件里,实现DllCreateTest和DllDestroyTest两个函数,代码如下:

       //file dll.cpp

       ......

       ITest* DllCreateTest()

      {

          return new CTest();

       }

       void DllDestroyTest(ITest *pTest)

       {

          if(pTest != NULL) delete pTest;

          pTest = NULL;

       }

       第四步,也是最容易忽略的一步,等以上操作都完成之后,还要在dll.def文件里,把要导出的函数DllCreateTest和DllDestroyTest加进去,如下:

    ; 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

      至此,MFC DLL的工程已经完成。那么如何在其他工程中调用生成的dll呢

     DllCreateTest取得一个全局变量g_Test,之后用g_Test去调用各个虚函数.

    ITest不一定非得为纯虚函数!

  • 相关阅读:
    开篇词The Start以及[Vjudge][HDU2242]空调教室
    [故地重游][NOIP2019]格雷码
    关于非触
    致诸君
    三角形的概率
    [HDU5970] 最大公约数
    [51Nod1534] 棋子游戏
    [TJOI2018] 数学计算
    [CF938D] Buy a Ticket
    [HDU4143] A Simple Problem
  • 原文地址:https://www.cnblogs.com/hgy413/p/3693635.html
Copyright © 2011-2022 走看看