DLL 使用 MFC 而不使用 CWinApp?
我最近遇到了一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为将标准 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.
CWinApp 类只不过是一个受控类,它将在 main/tmain 函数中调用,该函数将在您启动进程时作为起点。由于 MFC 只是一个库,它也可以根据项目属性中给出的标志在通用控制台应用程序中使用。
cwinapp 的实例是从 appmodul.cpp 中的函数 AfxWinMain 创建的,
因此直接在 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
So creating the so called directly in main function also works.