我正在使用 windows MFC C++。有一个第三方应用程序调用我的CWinApp派生类中的用户定义方法。此方法在InitInstance()之后被调用。如果在此方法中出现错误,例如抛出异常并在try/catch块中捕获,则我希望在catch块中退出应用程序。什么是标准和正确的退出方式?
更新:
我相信Serge正确指出,在InitInstance()中返回false是退出应用程序的正确方式。但是,现在假设我想从CDialog派生类的OnInitDialog()处理程序退出,应该如何操作?
更新2:
对于我而言,我发现在我的非模态CDialog派生类中调用PostMessage(WM_CLOSE)是最好的退出方式。我尝试过的所有其他退出方法在某些情况下都会引发某些异常。
以下是我使用它的示例:
BOOL SomeDialog::OnInitDialog()
{
CDialog::OnInitDialog();
::OleInitialize(nullptr);
try
{
// ...load settings file here
}
catch(...)
{
PostMessage(WM_CLOSE);
return TRUE;
}
// return TRUE unless you set the focus to a control
return TRUE;
}