From 46f60bb547adfee6c62945b054709d15ea220e5d Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 2 Feb 2020 10:00:42 +0000 Subject: [PATCH] Stop winutils.c from depending on the global HWND. The GUI version of pgp_fingerprints() is now a differently named function that takes a parent HWND as a parameter, and so does my help-enabled wrapper around MessageBox. --- windows/windlg.c | 4 ++-- windows/window.c | 4 ++-- windows/winpgen.c | 6 +++--- windows/winpgnt.c | 4 ++-- windows/winstuff.h | 4 +++- windows/winutils.c | 39 ++++++++++++++++++++++----------------- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/windows/windlg.c b/windows/windlg.c index 1b755b7b..860898ac 100644 --- a/windows/windlg.c +++ b/windows/windlg.c @@ -872,7 +872,7 @@ int win_seat_verify_ssh_host_key( char *text = dupprintf(wrongmsg, appname, keytype, fingerprint, appname); char *caption = dupprintf(mbtitle, appname); - mbret = message_box(text, caption, + mbret = message_box(hwnd, text, caption, MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3, HELPCTXID(errors_hostkey_changed)); assert(mbret==IDYES || mbret==IDNO || mbret==IDCANCEL); @@ -887,7 +887,7 @@ int win_seat_verify_ssh_host_key( int mbret; char *text = dupprintf(absentmsg, keytype, fingerprint, appname); char *caption = dupprintf(mbtitle, appname); - mbret = message_box(text, caption, + mbret = message_box(hwnd, text, caption, MB_ICONWARNING | MB_YESNOCANCEL | MB_DEFBUTTON3, HELPCTXID(errors_hostkey_absent)); assert(mbret==IDYES || mbret==IDNO || mbret==IDCANCEL); diff --git a/windows/window.c b/windows/window.c index 5accaf22..15e3e944 100644 --- a/windows/window.c +++ b/windows/window.c @@ -648,7 +648,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) "Are you really sure you want to continue?", appname); s2 = dupprintf("%s Warning", appname); - if (message_box(s1, s2, + if (message_box(NULL, s1, s2, MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2, HELPCTXID(option_cleanup)) == IDYES) { cleanup_all(); @@ -657,7 +657,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) sfree(s2); exit(0); } else if (!strcmp(p, "-pgpfp")) { - pgp_fingerprints(); + pgp_fingerprints_msgbox(NULL); exit(1); } else if (*p != '-') { cmdline_error("unexpected argument \"%s\"", p); diff --git a/windows/winpgen.c b/windows/winpgen.c index f67a3fb9..bb915810 100644 --- a/windows/winpgen.c +++ b/windows/winpgen.c @@ -657,7 +657,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state, !import_possible(type)) { char *msg = dupprintf("Couldn't load private key (%s)", key_type_to_str(type)); - message_box(msg, "PuTTYgen Error", MB_OK | MB_ICONERROR, + message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR, HELPCTXID(errors_cantloadkey)); sfree(msg); return; @@ -720,7 +720,7 @@ void load_key_file(HWND hwnd, struct MainDlgState *state, sfree(comment); if (ret == 0) { char *msg = dupprintf("Couldn't load private key (%s)", errmsg); - message_box(msg, "PuTTYgen Error", MB_OK | MB_ICONERROR, + message_box(hwnd, msg, "PuTTYgen Error", MB_OK | MB_ICONERROR, HELPCTXID(errors_cantloadkey)); sfree(msg); } else if (ret == 1) { @@ -1572,7 +1572,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "-pgpfp")) { - pgp_fingerprints(); + pgp_fingerprints_msgbox(NULL); return 1; } else if (!strcmp(argv[i], "-restrict-acl") || !strcmp(argv[i], "-restrict_acl") || diff --git a/windows/winpgnt.c b/windows/winpgnt.c index 962461da..28846355 100644 --- a/windows/winpgnt.c +++ b/windows/winpgnt.c @@ -415,7 +415,7 @@ static void win_add_keyfile(Filename *filename) } error: - message_box(err, APPNAME, MB_OK | MB_ICONERROR, + message_box(hwnd, err, APPNAME, MB_OK | MB_ICONERROR, HELPCTXID(errors_cantloadkey)); done: if (passphrase) { @@ -1271,7 +1271,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) split_into_argv(cmdline, &argc, &argv, &argstart); for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "-pgpfp")) { - pgp_fingerprints(); + pgp_fingerprints_msgbox(NULL); return 1; } else if (!strcmp(argv[i], "-restrict-acl") || !strcmp(argv[i], "-restrict_acl") || diff --git a/windows/winstuff.h b/windows/winstuff.h index 7857e623..f5991c9a 100644 --- a/windows/winstuff.h +++ b/windows/winstuff.h @@ -399,7 +399,9 @@ typedef struct filereq_tag filereq; /* cwd for file requester */ bool request_file(filereq *state, OPENFILENAME *of, bool preserve, bool save); filereq *filereq_new(void); void filereq_free(filereq *state); -int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid); +void pgp_fingerprints_msgbox(HWND owner); +int message_box(HWND owner, LPCTSTR text, LPCTSTR caption, + DWORD style, DWORD helpctxid); char *GetDlgItemText_alloc(HWND hwnd, int id); void split_into_argv(char *, int *, char ***, char ***); diff --git a/windows/winutils.c b/windows/winutils.c index 404e11a2..df0c41be 100644 --- a/windows/winutils.c +++ b/windows/winutils.c @@ -91,6 +91,8 @@ void filereq_free(filereq *state) * Message box with optional context help. */ +static HWND message_box_owner; + /* Callback function to launch context help. */ static VOID CALLBACK message_box_help_callback(LPHELPINFO lpHelpInfo) { @@ -107,10 +109,11 @@ static VOID CALLBACK message_box_help_callback(LPHELPINFO lpHelpInfo) CHECK_CTX(pgp_fingerprints); #undef CHECK_CTX if (context) - launch_help(hwnd, context); + launch_help(message_box_owner, context); } -int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid) +int message_box(HWND owner, LPCTSTR text, LPCTSTR caption, + DWORD style, DWORD helpctxid) { MSGBOXPARAMS mbox; @@ -121,7 +124,7 @@ int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid) mbox.cbSize = sizeof(mbox); /* Assumes the globals `hinst' and `hwnd' have sensible values. */ mbox.hInstance = hinst; - mbox.hwndOwner = hwnd; + mbox.hwndOwner = message_box_owner = owner; mbox.lpfnMsgBoxCallback = &message_box_help_callback; mbox.dwLanguageId = LANG_NEUTRAL; mbox.lpszText = text; @@ -135,21 +138,23 @@ int message_box(LPCTSTR text, LPCTSTR caption, DWORD style, DWORD helpctxid) /* * Display the fingerprints of the PGP Master Keys to the user. */ -void pgp_fingerprints(void) +void pgp_fingerprints_msgbox(HWND owner) { - message_box("These are the fingerprints of the PuTTY PGP Master Keys. They can\n" - "be used to establish a trust path from this executable to another\n" - "one. See the manual for more information.\n" - "(Note: these fingerprints have nothing to do with SSH!)\n" - "\n" - "PuTTY Master Key as of " PGP_MASTER_KEY_YEAR - " (" PGP_MASTER_KEY_DETAILS "):\n" - " " PGP_MASTER_KEY_FP "\n\n" - "Previous Master Key (" PGP_PREV_MASTER_KEY_YEAR - ", " PGP_PREV_MASTER_KEY_DETAILS "):\n" - " " PGP_PREV_MASTER_KEY_FP, - "PGP fingerprints", MB_ICONINFORMATION | MB_OK, - HELPCTXID(pgp_fingerprints)); + message_box( + owner, + "These are the fingerprints of the PuTTY PGP Master Keys. They can\n" + "be used to establish a trust path from this executable to another\n" + "one. See the manual for more information.\n" + "(Note: these fingerprints have nothing to do with SSH!)\n" + "\n" + "PuTTY Master Key as of " PGP_MASTER_KEY_YEAR + " (" PGP_MASTER_KEY_DETAILS "):\n" + " " PGP_MASTER_KEY_FP "\n\n" + "Previous Master Key (" PGP_PREV_MASTER_KEY_YEAR + ", " PGP_PREV_MASTER_KEY_DETAILS "):\n" + " " PGP_PREV_MASTER_KEY_FP, + "PGP fingerprints", MB_ICONINFORMATION | MB_OK, + HELPCTXID(pgp_fingerprints)); } /*