From f93b335c9a720a55269ff69583196f233bc87448 Mon Sep 17 00:00:00 2001 From: Wouter Date: Wed, 27 May 2020 00:40:01 +0200 Subject: [PATCH] Replace assertions --- options.c | 100 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/options.c b/options.c index 02cb19a..651fa1d 100644 --- a/options.c +++ b/options.c @@ -37,9 +37,6 @@ #include #include "options.h" - -#include - #include "main.h" #include "openvpn-gui-res.h" #include "localization.h" @@ -466,54 +463,67 @@ BrowseFolder (const WCHAR* initial_path, WCHAR* selected_path) // Create dialog initResult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); - assert(SUCCEEDED(initResult)); - + if (FAILED(initResult)) + { + return false; + } + result = CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_ALL, &IID_IFileOpenDialog, &pfd); - assert(SUCCEEDED(result)); - - // Select folders, not files + if (SUCCEEDED(result)) { - DWORD dwOptions; - result = pfd->lpVtbl->GetOptions(pfd, &dwOptions); - assert(SUCCEEDED(result)); - dwOptions |= FOS_PICKFOLDERS; - result = pfd->lpVtbl->SetOptions(pfd, dwOptions); - assert(SUCCEEDED(result)); - } - - // Set initial path - { - IShellItem* psi; - result = SHCreateItemFromParsingName(initial_path, NULL, &IID_IShellItem, &psi); - if (SUCCEEDED(result)) + // Select folders, not files { - pfd->lpVtbl->SetDefaultFolder(pfd, psi); - psi->lpVtbl->Release(psi); + DWORD dwOptions; + result = pfd->lpVtbl->GetOptions(pfd, &dwOptions); + if(SUCCEEDED(result)) + { + dwOptions |= FOS_PICKFOLDERS; + result = pfd->lpVtbl->SetOptions(pfd, dwOptions); + } } + + // Set initial path + { + IShellItem* psi; + result = SHCreateItemFromParsingName(initial_path, NULL, &IID_IShellItem, &psi); + if (SUCCEEDED(result)) + { + pfd->lpVtbl->SetDefaultFolder(pfd, psi); + psi->lpVtbl->Release(psi); + } + } + + // Show dialog and copy the selected file path if the user didn't cancel + dialogResult = pfd->lpVtbl->Show(pfd, NULL); + if (SUCCEEDED(dialogResult)) + { + IShellItem* psi; + LPOLESTR path = NULL; + + result = pfd->lpVtbl->GetResult(pfd, &psi); + if(SUCCEEDED(result)) + { + result = psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH, &path); + } + + if (SUCCEEDED(result)) + { + wcsncpy_s(selected_path, MAX_PATH, path, MAX_PATH); + selected_path[MAX_PATH - 1] = 0; + + CoTaskMemFree(path); + + psi->lpVtbl->Release(psi); + }else + { + dialogResult = E_FAIL; + } + } + + // Cleanup + pfd->lpVtbl->Release(pfd); } - // Show dialog and copy the selected file path if the user didn't cancel - dialogResult = pfd->lpVtbl->Show(pfd, NULL); - if (SUCCEEDED(dialogResult)) - { - IShellItem* psi; - LPOLESTR path = NULL; - - assert(SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &psi))); - - assert(SUCCEEDED(psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH, &path))); - - wcsncpy_s(selected_path, MAX_PATH, path, MAX_PATH); - selected_path[MAX_PATH - 1] = 0; - - CoTaskMemFree(path); - - psi->lpVtbl->Release(psi); - } - - // Cleanup - pfd->lpVtbl->Release(pfd); - if (initResult != RPC_E_CHANGED_MODE && SUCCEEDED(initResult)) { CoUninitialize(); //All successful CoInitializeEx calls must be balanced by a corresponding CoUninitialize