設定フォルダダイアログで設定ファイルのパスをexplorerで開くとき、失敗することがあった
- 設定ファイルが存在するとき、ファイルを選択した状態でexplorerで開くようにした(r9340)
- 設定ファイルが存在しないとき、ファイルがないエラーが出て開かない
- ファイルがないとき、(ファイルは選択しないで)フォルダを開くようにした
@@ -55,6 +55,7 @@ | ||
55 | 55 | #include "codeconv.h" |
56 | 56 | #include "asprintf.h" |
57 | 57 | #include "helpid.h" |
58 | +#include "win32helper.h" | |
58 | 59 | |
59 | 60 | #include "setupdirdlg.h" |
60 | 61 |
@@ -94,15 +95,14 @@ | ||
94 | 95 | // UACが有効かどうか。 |
95 | 96 | // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\SystemのEnableLUA(DWORD値)が0かどうかで判断できます(0はUAC無効、1はUAC有効)。 |
96 | 97 | flag = 0; |
97 | - lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, | |
98 | - TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"), | |
99 | - NULL, KEY_QUERY_VALUE, &hKey | |
100 | - ); | |
98 | + lRet = RegOpenKeyExA(HKEY_LOCAL_MACHINE, | |
99 | + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", | |
100 | + 0, KEY_QUERY_VALUE, &hKey); | |
101 | 101 | if (lRet == ERROR_SUCCESS) { |
102 | 102 | dwDataSize = sizeof(lpData) / sizeof(lpData[0]); |
103 | - lRet = RegQueryValueEx( | |
103 | + lRet = RegQueryValueExA( | |
104 | 104 | hKey, |
105 | - TEXT("EnableLUA"), | |
105 | + "EnableLUA", | |
106 | 106 | 0, |
107 | 107 | &dwType, |
108 | 108 | (LPBYTE)lpData, |
@@ -146,7 +146,7 @@ | ||
146 | 146 | // return TRUE: success |
147 | 147 | // FALSE: failure |
148 | 148 | // |
149 | -static BOOL openFileWithApplication(const wchar_t *filename, const char *editor, const char *UILanguageFile) | |
149 | +static BOOL openFileWithApplication(const wchar_t *filename, const char *editor, const wchar_t *UILanguageFile) | |
150 | 150 | { |
151 | 151 | wchar_t *commandW = NULL; |
152 | 152 | BOOL ret = FALSE; |
@@ -160,7 +160,7 @@ | ||
160 | 160 | "DLG_SETUPDIR_NOFILE_ERROR", L"File does not exist.(%d)", |
161 | 161 | MB_OK | MB_ICONWARNING |
162 | 162 | }; |
163 | - TTMessageBoxA(NULL, &info, UILanguageFile, no); | |
163 | + TTMessageBoxW(NULL, &info, UILanguageFile, no); | |
164 | 164 | |
165 | 165 | goto error; |
166 | 166 | } |
@@ -182,7 +182,7 @@ | ||
182 | 182 | "DLG_SETUPDIR_OPENFILE_ERROR", L"Cannot open file.(%d)", |
183 | 183 | MB_OK | MB_ICONWARNING |
184 | 184 | }; |
185 | - TTMessageBoxA(NULL, &info, UILanguageFile, no); | |
185 | + TTMessageBoxW(NULL, &info, UILanguageFile, no); | |
186 | 186 | |
187 | 187 | goto error; |
188 | 188 | } |
@@ -199,36 +199,50 @@ | ||
199 | 199 | return (ret); |
200 | 200 | } |
201 | 201 | |
202 | -// | |
203 | -// エクスプローラでパスを開く。 | |
204 | -// | |
205 | -// return TRUE: success | |
206 | -// FALSE: failure | |
207 | -// | |
208 | -static BOOL openDirectoryWithExplorer(const wchar_t *path, const char *UILanguageFile) | |
202 | +/** | |
203 | + * エクスプローラで指定ファイルのフォルダを開く | |
204 | + * ファイルが存在する場合はファイルを選択した状態で開く | |
205 | + * ファイルが存在しない場合はフォルダを開く | |
206 | + * | |
207 | + * @param file ファイル | |
208 | + * @retval TRUE: success | |
209 | + * @retval FALSE: failure | |
210 | + */ | |
211 | +static BOOL openDirectoryWithExplorer(const wchar_t *file, const wchar_t *UILanguageFile) | |
209 | 212 | { |
210 | 213 | BOOL ret; |
211 | 214 | |
212 | - const DWORD attr = GetFileAttributesW(path); | |
215 | + DWORD attr = GetFileAttributesW(file); | |
213 | 216 | if (attr == INVALID_FILE_ATTRIBUTES) { |
214 | - // ファイルが存在しない | |
215 | - DWORD no = GetLastError(); | |
216 | - static const TTMessageBoxInfoW info = { | |
217 | - "Tera Term", | |
218 | - "MSG_ERROR", L"ERROR", | |
219 | - "DLG_SETUPDIR_NOFILE_ERROR", L"File does not exist.(%d)", | |
220 | - MB_OK | MB_ICONWARNING | |
221 | - }; | |
222 | - TTMessageBoxA(NULL, &info, UILanguageFile, no); | |
223 | - ret = FALSE; | |
217 | + // ファイルが存在しない, ディレクトリをオープンする | |
218 | + wchar_t *dir = ExtractDirNameW(file); | |
219 | + attr = GetFileAttributesW(dir); | |
220 | + if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
221 | + // フォルダを開く | |
222 | + INT_PTR h = (INT_PTR)ShellExecuteW(NULL, L"open", L"explorer.exe", dir, NULL, SW_NORMAL); | |
223 | + ret = h > 32 ? TRUE : FALSE; | |
224 | + } | |
225 | + else { | |
226 | + // ファイルもフォルダも存在しない | |
227 | + DWORD no = GetLastError(); | |
228 | + static const TTMessageBoxInfoW info = { | |
229 | + "Tera Term", | |
230 | + "MSG_ERROR", L"ERROR", | |
231 | + "DLG_SETUPDIR_NOFILE_ERROR", L"File does not exist.(%d)", | |
232 | + MB_OK | MB_ICONWARNING | |
233 | + }; | |
234 | + TTMessageBoxW(NULL, &info, UILanguageFile, no); | |
235 | + ret = FALSE; | |
236 | + } | |
237 | + free(dir); | |
224 | 238 | } else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
225 | - // フォルダを開く | |
226 | - INT_PTR h = (INT_PTR)ShellExecuteW(NULL, L"open", L"explorer.exe", path, NULL, SW_NORMAL); | |
239 | + // 指定されたのがフォルダだった、フォルダを開く | |
240 | + INT_PTR h = (INT_PTR)ShellExecuteW(NULL, L"open", L"explorer.exe", file, NULL, SW_NORMAL); | |
227 | 241 | ret = h > 32 ? TRUE : FALSE; |
228 | 242 | } else { |
229 | 243 | // フォルダを開く + ファイル選択 |
230 | 244 | wchar_t *param; |
231 | - aswprintf(¶m, L"/select,%s", path); | |
245 | + aswprintf(¶m, L"/select,%s", file); | |
232 | 246 | INT_PTR h = (INT_PTR)ShellExecuteW(NULL, L"open", L"explorer.exe", param, NULL, SW_NORMAL); |
233 | 247 | free(param); |
234 | 248 | ret = h > 32 ? TRUE : FALSE; |
@@ -310,15 +324,6 @@ | ||
310 | 324 | return ret; |
311 | 325 | } |
312 | 326 | |
313 | -static wchar_t *GetWindowTextAlloc(HWND hWnd) | |
314 | -{ | |
315 | - size_t length = GetWindowTextLengthW(hWnd); | |
316 | - length++; | |
317 | - wchar_t *buf = (wchar_t *)malloc(sizeof(wchar_t) * length); | |
318 | - GetWindowTextW(hWnd, buf, (int)length); | |
319 | - return buf; | |
320 | -} | |
321 | - | |
322 | 327 | static INT_PTR CALLBACK OnSetupDirectoryDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM lp) |
323 | 328 | { |
324 | 329 | static const DlgTextInfo TextInfos[] = { |
@@ -530,31 +535,24 @@ | ||
530 | 535 | } |
531 | 536 | |
532 | 537 | if (button_pressed) { |
533 | - wchar_t *filename_p; | |
538 | + wchar_t *filename; | |
534 | 539 | if (!IsWindowEnabled(GetDlgItem(hDlgWnd, edit_vstore))) { |
535 | - filename_p = GetWindowTextAlloc(GetDlgItem(hDlgWnd, edit)); | |
540 | + hGetWindowTextW(GetDlgItem(hDlgWnd, edit), &filename); | |
536 | 541 | } else { |
537 | - filename_p = GetWindowTextAlloc(GetDlgItem(hDlgWnd, edit_vstore)); | |
542 | + hGetWindowTextW(GetDlgItem(hDlgWnd, edit_vstore), &filename); | |
538 | 543 | } |
539 | 544 | |
540 | - const char *UILanguageFile = pts->UILanguageFile; | |
545 | + const wchar_t *UILanguageFile = pts->UILanguageFileW; | |
541 | 546 | if (open_dir) { |
542 | -#if 0 | |
543 | - // フォルダを開く | |
544 | - wchar_t *path_p = ExtractDirNameW(filename_p); | |
545 | - openDirectoryWithExplorer(path_p, UILanguageFile); | |
546 | - free(path_p); | |
547 | -#else | |
548 | 547 | // フォルダを開いて、ファイルを選択する |
549 | - openDirectoryWithExplorer(filename_p, UILanguageFile); | |
550 | -#endif | |
548 | + openDirectoryWithExplorer(filename, UILanguageFile); | |
551 | 549 | } |
552 | 550 | else { |
553 | - char *editor = pts->ViewlogEditor; | |
554 | - openFileWithApplication(filename_p, editor, UILanguageFile); | |
551 | + const char *editor = pts->ViewlogEditor; | |
552 | + openFileWithApplication(filename, editor, UILanguageFile); | |
555 | 553 | } |
556 | 554 | |
557 | - free(filename_p); | |
555 | + free(filename); | |
558 | 556 | return TRUE; |
559 | 557 | } |
560 | 558 | return FALSE; |