From e1c6f61985a7fb04e7336b7483bae6f30314678f Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Mon, 29 May 2023 15:30:57 +0100 Subject: [PATCH] New Windows utility function: request_file_w. Just like the existing request_file, but wide-character oriented. --- windows/platform.h | 2 ++ windows/utils/request_file.c | 43 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/windows/platform.h b/windows/platform.h index 65b6308b..9463be4e 100644 --- a/windows/platform.h +++ b/windows/platform.h @@ -395,6 +395,8 @@ void init_common_controls(void); /* also does some DLL-loading */ */ typedef struct filereq_tag filereq; /* cwd for file requester */ bool request_file(filereq *state, OPENFILENAME *of, bool preserve, bool save); +bool request_file_w(filereq *state, OPENFILENAMEW *of, + bool preserve, bool save); filereq *filereq_new(void); void filereq_free(filereq *state); void pgp_fingerprints_msgbox(HWND owner); diff --git a/windows/utils/request_file.c b/windows/utils/request_file.c index 1602942e..57363cad 100644 --- a/windows/utils/request_file.c +++ b/windows/utils/request_file.c @@ -9,6 +9,7 @@ struct filereq_tag { TCHAR cwd[MAX_PATH]; + WCHAR wcwd[MAX_PATH]; }; /* @@ -58,10 +59,52 @@ bool request_file(filereq *state, OPENFILENAME *of, bool preserve, bool save) return ret; } +/* + * Here's the same one again, the wide-string version + */ +bool request_file_w(filereq *state, OPENFILENAMEW *of, + bool preserve, bool save) +{ + WCHAR cwd[MAX_PATH]; /* process CWD */ + bool ret; + + /* Get process CWD */ + if (preserve) { + DWORD r = GetCurrentDirectoryW(lenof(cwd), cwd); + if (r == 0 || r >= lenof(cwd)) + /* Didn't work, oh well. Stop trying to be clever. */ + preserve = false; + } + + /* Open the file requester, maybe setting lpstrInitialDir */ + { + of->lStructSize = sizeof(*of); + of->lpstrInitialDir = (state && state->wcwd[0]) ? state->wcwd : NULL; + /* Actually put up the requester. */ + ret = save ? GetSaveFileNameW(of) : GetOpenFileNameW(of); + } + + /* Get CWD left by requester */ + if (state) { + DWORD r = GetCurrentDirectoryW(lenof(state->wcwd), state->wcwd); + if (r == 0 || r >= lenof(state->wcwd)) + /* Didn't work, oh well. */ + state->wcwd[0] = L'\0'; + } + + /* Restore process CWD */ + if (preserve) + /* If it fails, there's not much we can do. */ + (void) SetCurrentDirectoryW(cwd); + + return ret; +} + filereq *filereq_new(void) { filereq *state = snew(filereq); state->cwd[0] = '\0'; + state->wcwd[0] = L'\0'; return state; }