mirror of
https://github.com/QuasarApp/qt-solutions.git
synced 2025-05-20 16:59:33 +00:00
Improve error handling in Qt Mfc examples.
Task-number: QTBUG-40881 Change-Id: Iada4965609209a9926b8234acc09fc8fcf79e518 Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
parent
fd22bee222
commit
19115c764a
@ -45,6 +45,8 @@
|
||||
#include "qtmfc.h"
|
||||
|
||||
#include "mainframe.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define new DEBUG_NEW
|
||||
@ -163,20 +165,58 @@ BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
||||
//}}AFX_MSG_MAP
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
static inline void formatWindowsErrorMessage(DWORD errorCode, std::wostream &str)
|
||||
{
|
||||
wchar_t *string = 0;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR)&string, 0, NULL);
|
||||
if (string)
|
||||
str << string;
|
||||
else
|
||||
str << "Unknown error";
|
||||
LocalFree((HLOCAL)string);
|
||||
}
|
||||
|
||||
static bool showQtAboutDialog(HWND parentWindow, std::wstring *errorMessage)
|
||||
{
|
||||
typedef BOOL(*pShowDialog)(HWND parent);
|
||||
|
||||
const char dllName[] = "qtdialog.dll";
|
||||
const char functionName[] = "showDialog";
|
||||
|
||||
const HMODULE module = LoadLibraryA(dllName);
|
||||
const DWORD errorCode = GetLastError();
|
||||
if (!module) {
|
||||
std::wostringstream str;
|
||||
str << "Unable to load '" << dllName << "': Error #" << errorCode << ": ";
|
||||
formatWindowsErrorMessage(errorCode, str);
|
||||
*errorMessage = str.str();
|
||||
return false;
|
||||
}
|
||||
|
||||
pShowDialog showDialog = (pShowDialog)GetProcAddress(module, functionName);
|
||||
if (!showDialog) {
|
||||
std::wostringstream str;
|
||||
str << "Unable to resolve function '" << functionName << "' in '"
|
||||
<< dllName << "'.";
|
||||
*errorMessage = str.str();
|
||||
return false;
|
||||
}
|
||||
|
||||
showDialog(parentWindow);
|
||||
FreeLibrary(module);
|
||||
return true;
|
||||
}
|
||||
|
||||
// App command to run the dialog
|
||||
void WindowsApp::OnAppAbout()
|
||||
{
|
||||
HMODULE mod = LoadLibrary( "qtdialog.dll" );
|
||||
if ( mod ) {
|
||||
typedef BOOL(*pShowDialog)(HWND parent);
|
||||
pShowDialog showDialog = (pShowDialog)GetProcAddress( mod, "showDialog" );
|
||||
if ( showDialog )
|
||||
showDialog( theApp.m_pMainWnd->m_hWnd );
|
||||
|
||||
FreeLibrary( mod );
|
||||
} else {
|
||||
CAboutDlg aboutDlg;
|
||||
aboutDlg.DoModal();
|
||||
const HWND parentWindow = theApp.m_pMainWnd->m_hWnd;
|
||||
std::wstring errorMessage;
|
||||
if (!showQtAboutDialog(parentWindow, &errorMessage)) {
|
||||
MessageBoxW(parentWindow, errorMessage.c_str(), L"QtMfc 1.0 MFC Application",
|
||||
MB_OK | MB_ICONWARNING);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,6 +45,8 @@
|
||||
#include "qtmfc.h"
|
||||
|
||||
#include "mainframe.h"
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include <qmfcapp.h>
|
||||
|
||||
@ -170,20 +172,58 @@ BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
||||
//}}AFX_MSG_MAP
|
||||
END_MESSAGE_MAP()
|
||||
|
||||
static inline void formatWindowsErrorMessage(DWORD errorCode, std::wostream &str)
|
||||
{
|
||||
wchar_t *string = 0;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR)&string, 0, NULL);
|
||||
if (string)
|
||||
str << string;
|
||||
else
|
||||
str << "Unknown error";
|
||||
LocalFree((HLOCAL)string);
|
||||
}
|
||||
|
||||
static bool showQtAboutDialog(HWND parentWindow, std::wstring *errorMessage)
|
||||
{
|
||||
typedef BOOL(*pShowDialog)(HWND parent);
|
||||
|
||||
const char dllName[] = "qtdialog.dll";
|
||||
const char functionName[] = "showDialog";
|
||||
|
||||
const HMODULE module = LoadLibraryA(dllName);
|
||||
const DWORD errorCode = GetLastError();
|
||||
if (!module) {
|
||||
std::wostringstream str;
|
||||
str << "Unable to load '" << dllName << "': Error #" << errorCode << ": ";
|
||||
formatWindowsErrorMessage(errorCode, str);
|
||||
*errorMessage = str.str();
|
||||
return false;
|
||||
}
|
||||
|
||||
pShowDialog showDialog = (pShowDialog)GetProcAddress(module, functionName);
|
||||
if (!showDialog) {
|
||||
std::wostringstream str;
|
||||
str << "Unable to resolve function '" << functionName << "' in '"
|
||||
<< dllName << "'.";
|
||||
*errorMessage = str.str();
|
||||
return false;
|
||||
}
|
||||
|
||||
showDialog(parentWindow);
|
||||
FreeLibrary(module);
|
||||
return true;
|
||||
}
|
||||
|
||||
// App command to run the dialog
|
||||
void WindowsApp::OnAppAbout()
|
||||
{
|
||||
HMODULE mod = LoadLibrary( "qtdialog.dll" );
|
||||
if ( mod ) {
|
||||
typedef BOOL(*pShowDialog)(HWND parent);
|
||||
pShowDialog showDialog = (pShowDialog)GetProcAddress( mod, "showDialog" );
|
||||
if ( showDialog )
|
||||
showDialog( theApp.m_pMainWnd->m_hWnd );
|
||||
|
||||
FreeLibrary( mod );
|
||||
} else {
|
||||
CAboutDlg aboutDlg;
|
||||
aboutDlg.DoModal();
|
||||
const HWND parentWindow = theApp.m_pMainWnd->m_hWnd;
|
||||
std::wstring errorMessage;
|
||||
if (!showQtAboutDialog(parentWindow, &errorMessage)) {
|
||||
MessageBoxW(parentWindow, errorMessage.c_str(), L"QtMfc 1.0 MFC Application",
|
||||
MB_OK | MB_ICONWARNING);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user