Skip to content

Commit 19115c7

Browse files
Friedemann KleintFriedemann Kleint
authored andcommitted
Improve error handling in Qt Mfc examples.
Task-number: QTBUG-40881 Change-Id: Iada4965609209a9926b8234acc09fc8fcf79e518 Reviewed-by: Andy Shaw <[email protected]>
1 parent fd22bee commit 19115c7

File tree

2 files changed

+102
-22
lines changed

2 files changed

+102
-22
lines changed

qtwinmigrate/examples/mfc/step1/qtmfc.cpp

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "qtmfc.h"
4646

4747
#include "mainframe.h"
48+
#include <string>
49+
#include <sstream>
4850

4951
#ifdef _DEBUG
5052
#define new DEBUG_NEW
@@ -163,20 +165,58 @@ BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
163165
//}}AFX_MSG_MAP
164166
END_MESSAGE_MAP()
165167

168+
static inline void formatWindowsErrorMessage(DWORD errorCode, std::wostream &str)
169+
{
170+
wchar_t *string = 0;
171+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
172+
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
173+
(LPWSTR)&string, 0, NULL);
174+
if (string)
175+
str << string;
176+
else
177+
str << "Unknown error";
178+
LocalFree((HLOCAL)string);
179+
}
180+
181+
static bool showQtAboutDialog(HWND parentWindow, std::wstring *errorMessage)
182+
{
183+
typedef BOOL(*pShowDialog)(HWND parent);
184+
185+
const char dllName[] = "qtdialog.dll";
186+
const char functionName[] = "showDialog";
187+
188+
const HMODULE module = LoadLibraryA(dllName);
189+
const DWORD errorCode = GetLastError();
190+
if (!module) {
191+
std::wostringstream str;
192+
str << "Unable to load '" << dllName << "': Error #" << errorCode << ": ";
193+
formatWindowsErrorMessage(errorCode, str);
194+
*errorMessage = str.str();
195+
return false;
196+
}
197+
198+
pShowDialog showDialog = (pShowDialog)GetProcAddress(module, functionName);
199+
if (!showDialog) {
200+
std::wostringstream str;
201+
str << "Unable to resolve function '" << functionName << "' in '"
202+
<< dllName << "'.";
203+
*errorMessage = str.str();
204+
return false;
205+
}
206+
207+
showDialog(parentWindow);
208+
FreeLibrary(module);
209+
return true;
210+
}
211+
166212
// App command to run the dialog
167213
void WindowsApp::OnAppAbout()
168214
{
169-
HMODULE mod = LoadLibrary( "qtdialog.dll" );
170-
if ( mod ) {
171-
typedef BOOL(*pShowDialog)(HWND parent);
172-
pShowDialog showDialog = (pShowDialog)GetProcAddress( mod, "showDialog" );
173-
if ( showDialog )
174-
showDialog( theApp.m_pMainWnd->m_hWnd );
175-
176-
FreeLibrary( mod );
177-
} else {
178-
CAboutDlg aboutDlg;
179-
aboutDlg.DoModal();
215+
const HWND parentWindow = theApp.m_pMainWnd->m_hWnd;
216+
std::wstring errorMessage;
217+
if (!showQtAboutDialog(parentWindow, &errorMessage)) {
218+
MessageBoxW(parentWindow, errorMessage.c_str(), L"QtMfc 1.0 MFC Application",
219+
MB_OK | MB_ICONWARNING);
180220
}
181221
}
182222

qtwinmigrate/examples/mfc/step2/qtmfc.cpp

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "qtmfc.h"
4646

4747
#include "mainframe.h"
48+
#include <string>
49+
#include <sstream>
4850

4951
#include <qmfcapp.h>
5052

@@ -170,20 +172,58 @@ BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
170172
//}}AFX_MSG_MAP
171173
END_MESSAGE_MAP()
172174

175+
static inline void formatWindowsErrorMessage(DWORD errorCode, std::wostream &str)
176+
{
177+
wchar_t *string = 0;
178+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
179+
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
180+
(LPWSTR)&string, 0, NULL);
181+
if (string)
182+
str << string;
183+
else
184+
str << "Unknown error";
185+
LocalFree((HLOCAL)string);
186+
}
187+
188+
static bool showQtAboutDialog(HWND parentWindow, std::wstring *errorMessage)
189+
{
190+
typedef BOOL(*pShowDialog)(HWND parent);
191+
192+
const char dllName[] = "qtdialog.dll";
193+
const char functionName[] = "showDialog";
194+
195+
const HMODULE module = LoadLibraryA(dllName);
196+
const DWORD errorCode = GetLastError();
197+
if (!module) {
198+
std::wostringstream str;
199+
str << "Unable to load '" << dllName << "': Error #" << errorCode << ": ";
200+
formatWindowsErrorMessage(errorCode, str);
201+
*errorMessage = str.str();
202+
return false;
203+
}
204+
205+
pShowDialog showDialog = (pShowDialog)GetProcAddress(module, functionName);
206+
if (!showDialog) {
207+
std::wostringstream str;
208+
str << "Unable to resolve function '" << functionName << "' in '"
209+
<< dllName << "'.";
210+
*errorMessage = str.str();
211+
return false;
212+
}
213+
214+
showDialog(parentWindow);
215+
FreeLibrary(module);
216+
return true;
217+
}
218+
173219
// App command to run the dialog
174220
void WindowsApp::OnAppAbout()
175221
{
176-
HMODULE mod = LoadLibrary( "qtdialog.dll" );
177-
if ( mod ) {
178-
typedef BOOL(*pShowDialog)(HWND parent);
179-
pShowDialog showDialog = (pShowDialog)GetProcAddress( mod, "showDialog" );
180-
if ( showDialog )
181-
showDialog( theApp.m_pMainWnd->m_hWnd );
182-
183-
FreeLibrary( mod );
184-
} else {
185-
CAboutDlg aboutDlg;
186-
aboutDlg.DoModal();
222+
const HWND parentWindow = theApp.m_pMainWnd->m_hWnd;
223+
std::wstring errorMessage;
224+
if (!showQtAboutDialog(parentWindow, &errorMessage)) {
225+
MessageBoxW(parentWindow, errorMessage.c_str(), L"QtMfc 1.0 MFC Application",
226+
MB_OK | MB_ICONWARNING);
187227
}
188228
}
189229

0 commit comments

Comments
 (0)