开发者

How do I start up an MFC application from scratch?

开发者 https://www.devze.com 2023-03-29 21:36 出处:网络
In other words from a blank win32 project (no wizard). This is where I am at: Preprocessor Definitions: WIN32

In other words from a blank win32 project (no wizard).

This is where I am at:

Preprocessor Definitions: WIN32

Linker->System->Subsystem = Console

int _tmain()
{
    int nRetCode = 0;

    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        return nRetCode = 1;
    }

    MyWinApp* app = new MyWinApp();

    app->InitApplication();
    app->InitInstance();

    app->Run();

    AfxWinTerm();

    return 0;
}


class MyWinApp: public CWinApp
{
public:
    BOOL InitInstance();

    int Run();
};


BOOL MyWinApp::InitInstance()
{
    return TRUE;
}

int MyWinApp::Run()
{
    return CWinThread::Run();
}

Skipping over the CWinApp::Run() because it looks for a main window.

In CWinThread::Run() however, the ASSERT_VALID fails. At the top of quickwatch for this it says MyWinApp 开发者_如何学Pythonis invalid.

Do I need to create MyWinApp in another way?


You're probably failing because you're creating the CWinApp after you're calling AfxWinInit. In a regular MFC app, the CWinApp is a global variable, which is constructed before main. This way, when MFC is initialized, it has a valid global CWinApp in place. Try:

MyWinApp* app = new MyWinApp();   // ^moved up^

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
    // TODO: change error code to suit your needs
    _tprintf(_T("Fatal Error: MFC initialization failed\n"));
    return nRetCode = 1;
}
0

精彩评论

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

关注公众号