DLL 使用 MFC 而不使用 CWinApp?

发布于 2024-10-21 02:13:07 字数 1095 浏览 3 评论 0原文

我最近遇到了一个使用 MFC 对话框的 DLL (github) (例如,它导入 "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();
}

I've recently came across a DLL (github) that uses MFC dialogs (it imports "afxdlg.h" and calls CFileDialog, for example) and appears to statically link to MFC, but doesn't have a class based on CWinApp. I'm somewhat confused: is it a MFC DLL or not? How come it doesn't have CWinApp?

Rephrased: In a Win32 DLL I use some MFC classes (e.g. I include "afxdlgs.h" and use CFileDialog) and link MFC statically. There's no DllMain. Will the final DLL have DllMain from Win32 or from MFC?

If it picks the MFC version, then another question: What is the simplest way to make a Win32 DLL with DllMain (no threads) to use MFC DllMain? Is the following correct?

#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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春花秋月 2024-10-28 02:13:07

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

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

I think the simplest way to convert a standard dll to a MFC dll is to make a new MFC-dll-project and then use the generated files and paste the rest of your code into it.

AFAIK there is no difference in the source files, but there are some in the linker settings. Starting with a new project saves a lot of time and trouble.

暮年 2024-10-28 02:13:07

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 函数中创建所谓的也可以。

CWinApp class is nothing but a controlled class which would be called in the main/tmain function which will be starting point when you start the process. As MFC is just a library it can be used in the general console application too as per the flags given in the project properties.

The instance of cwinapp is created from the function AfxWinMain in appmodul.cpp

/////////////////////////////////////////////////////////////////////////////
// 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);
}

So creating the so called directly in main function also works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文