Update file dialog for windows

This commit is contained in:
Justin Scofield
2023-11-24 22:01:02 -05:00
parent 44cb122e51
commit f771926ed1

View File

@@ -5,14 +5,16 @@
#include <shobjidl.h> #include <shobjidl.h>
#include <windows.h> #include <windows.h>
std::string ShowOpenFileDialog() { class FileDialogWrapper {
public:
static std::string ShowOpenFileDialog() {
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
IFileDialog *pfd = NULL; IFileDialog *pfd = NULL;
HRESULT hr = HRESULT hr =
CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog, CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
reinterpret_cast<void **>(&pfd)); IID_IFileDialog, reinterpret_cast<void **>(&pfd));
std::string file_path_windows;
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr)) {
// Show the dialog // Show the dialog
hr = pfd->Show(NULL); hr = pfd->Show(NULL);
@@ -23,6 +25,9 @@ std::string ShowOpenFileDialog() {
// Get the file path // Get the file path
PWSTR pszFilePath; PWSTR pszFilePath;
psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
char str[128];
wcstombs(str, pszFilePath, 128);
file_path_windows = str;
psiResult->Release(); psiResult->Release();
CoTaskMemFree(pszFilePath); CoTaskMemFree(pszFilePath);
} }
@@ -31,8 +36,9 @@ std::string ShowOpenFileDialog() {
} }
CoUninitialize(); CoUninitialize();
return L""; // Return an empty string if no file was selected or an error return file_path_windows;
} }
};
#elif defined(__APPLE__) #elif defined(__APPLE__)