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

     

    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不一定非得为纯虚函数!

  • 相关阅读:
    RPC笔记之初探RPC:DIY简单RPC框架
    zookeeper笔记之基于zk实现分布式锁
    scala笔记之惰性赋值(lazy)
    Hive笔记之宏(macro)
    Zookeeper笔记之使用zk实现集群选主
    Zookeeper笔记之基于zk的分布式配置中心
    Zookeeper笔记之四字命令
    Zookeeper笔记之命令行操作
    复盘2018,展望2019
    爬虫笔记之w3cschool注册页面滑块验证码破解(巨简单滑块位置识别,非鼠标模拟轨迹)
  • 原文地址:https://www.cnblogs.com/jack-jia-moonew/p/4230157.html
Copyright © 2011-2022 走看看