Win32 Message Handler Error Propagation

I am writing an application (C ++) that uses a single dialog box. After setting up the message pump and handler, I began to wonder how I would distribute C ++ exceptions to my source code (for example, code that calls CreateDialogParam, for example).

Here is an example of a skeleton that I have in mind:

BOOL CALLBACK DialogProc(HWND, UINT msg, WPARAM, LPARAM)
{
    if(msg == WM_INITDIALOG) //Or some other message
    {
        /*
            Load some critical resource(s) here. For instnace:

            const HANDLE someResource = LoadImage(...);

            if(someResource == NULL)
            {
            ---> throw std::runtime_error("Exception 1"); <--- The exception handler in WinMain will never see this!
                Maybe PostMessage(MY_CUSTOM_ERROR_MSG)?
            }
        */

        return TRUE;
    }

    return FALSE;
}

//======================

void RunApp()
{
    const HWND dlg = CreateDialog(...); //Using DialogProc

    if(dlg == NULL)
    {
        throw std::runtime_error("Exception 2"); //Ok, WinMain will see this.
    }

    MSG msg = {};
    BOOL result = 0;

    while((result = GetMessage(&msg, ...)) != 0)
    {
        if(result == -1)
        {
            throw std::runtime_error("Exception 3"); //Ok, WinMain will see this.
        }

        //Maybe check msg.message == MY_CUSTOM_ERROR_MSG and throw from here?

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

//======================

int WINAPI WinMain(...)
{
    try
    {
        RunApp();
        //Some other init routines go here as well.
    }

    catch(const std::exception& e)
    {
        //log the error
        return 1;
    }

    catch(...)
    {
        //log the error
        return 1;
    }

    return 0;
}

As you can see, it WinMainwill handle Exception 2 and 3, but not Exception 1.

My fundamental question is simple; what would be an elegant way to propagate such errors to the source "calling" code?

, , , throw - ( RunApp()), , , Windows .

, , . , - (.. , ), ?

+3
4

Window . , , .

, catch 3 . , CreateDialog. . GetMessage/Translate/Dispatch. , , , try/catch WinMain.

, ?

0

AFAIK WinAPI (, window/dialog/thread) . , WinAPI ( ) . , , WinAPI , .

( Visual Studio) , , , . , , . , , WinAPI , - , .

, ( - ). , , , . , . , , , - ? , , Windows? - - ? ( .) , .

Boost.Exception. - ( , ) . , throw { ... } catch(...) { ... }, catch .

: ++ 11 Boost.Exception , STD. std::exception_ptr .

. - ( , ). , . ? ? / ?

WM_INITDIALOG ( ), , ( ). , , . .

+6

, . , , .

1. OutputDebugString().
, - , . , -, ,

2. MessageBox.
, 1, .

3. .
, "", , "", Win32 :

if(msg == WM_INITDIALOG) //Or some other message
{
    /*
        Load some critical resource(s) here. For instnace:

        const HANDLE someResource = LoadImage(...);

        if(someResource == NULL)
        {
                  LogError("Cannot find resource 'foo');
        }
    */

    return TRUE;
}
+1

Source: https://habr.com/ru/post/1713293/


All Articles