开发者

DLL using MFC without CWinApp?

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


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜