【问题标题】:DLL using MFC without CWinApp?DLL 使用没有 CWinApp 的 MFC?
【发布时间】:2011-03-10 08:27:02
【问题描述】:

我最近遇到了一个 DLL (github),它使用 MFC 对话框(例如,它导入 "afxdlg.h" 并调用 CFileDialog)并且似乎静态链接到 MFC,但没有类基于CWinApp。我有点困惑:它是不是 MFC DLL?怎么没有CWinApp

改述:在 Win32 DLL 中,我使用一些 MFC 类(例如,我包含 "afxdlgs.h" 并使用 CFileDialog)并静态链接 MFC。没有DllMain。最终的 DLL 是否有来自 Win32 或来自 MFC 的DllMain

如果它选择 MFC 版本,那么另一个问题:使用 DllMain(无线程)制作 Win32 DLL 以使用 MFC DllMain 的最简单方法是什么?以下是正确的吗?

#include "afx.h" /* correct? */

class MyDll: public CWinApp
{
public:
    /* do I need constructor and destructor here? */
    virtual BOOL InitInstance();
    virtual BOOL ExitInstance();
} theDll;

BOOL
MyDLL::InitInstance()
{
    CWinApp::InitInstance();
    /* code from old DllMain, DLL_PROCESS_ATTACH. 
       For hInst use theDll.m_hInstance */
    return TRUE;
}

BOOL
MyDLL::ExitInstance()
{
    /* code from old DllMain, DLL_PROCESS_DETACH */
    return CWinApp::ExitInstance();
}

【问题讨论】:

    标签: dll mfc


    【解决方案1】:

    我认为将标准 dll 转换为 MFC dll 的最简单方法是创建一个新的 MFC-dll-project,然后使用生成的文件并将其余代码粘贴到其中。

    AFAIK 源文件没有区别,但链接器设置有一些区别。从一个新项目开始可以节省大量时间和麻烦。

    【讨论】:

      【解决方案2】:

      CWinApp 类只不过是一个受控类,它将在 main/tmain 函数中调用,当您启动进程时,它将作为起点。由于 MFC 只是一个库,它也可以根据项目属性中给出的标志在通用控制台应用程序中使用。

      cwinapp的实例是从appmodul.cpp中的函数AfxWinMain创建的

      /////////////////////////////////////////////////////////////////////////////
      // export WinMain to force linkage to this module
      extern int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
          _In_ LPTSTR lpCmdLine, int nCmdShow);
      
      extern "C" int WINAPI
      _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
          _In_ LPTSTR lpCmdLine, int nCmdShow)
      #pragma warning(suppress: 4985)
      {
          // call shared/exported WinMain
          return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
      }
      

      所以直接在 main 函数中创建所谓的也可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-21
        • 2011-03-31
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多