4

I'm developing a desktop application with ImGui in C++, and I need the user to be able to search for a file on their machine with a file explorer. It was extremely easy to do with Python tkinter, but how am I able to do this with C++?

heap underrun
  • 1,846
  • 1
  • 18
  • 22
snakelovah18119
  • 121
  • 1
  • 7
  • Multiplatform or single OS? Is it supposed to by OS explorer or an application's window (there is imgui-filebrowser)? – Swift - Friday Pie Jul 31 '21 at 10:36
  • A general approach for operation systems which support file URI MIME, is to generate URL based on the actual path and open it, I suspect that Python does the same? – Swift - Friday Pie Jul 31 '21 at 10:41

2 Answers2

3

You could use NativeFileDialog its quite easy to implement, they have an example on they're GitHub too!

  • Please do add more information to your answer in case the link gets broken in the future. Read about why link only answers are discouraged [here](https://meta.stackoverflow.com/tags/link-only-answers/info) – Michael Kotzjan Aug 17 '21 at 08:46
1

Native Windows Solution: This will open the "Windows File Selection" prompt, I included it in a custom function that will also store the result and file name to a string variable.

I use a variation of the include method for my custom front end launcher. Hope you make some use of it , even tho you asked this question 9 months ago

#include <Windows.h>
#include <string>
#include <shobjidl.h> 

std::string sSelectedFile;
std::string sFilePath;
bool openFile()
{
    //  CREATE FILE OBJECT INSTANCE
    HRESULT f_SysHr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (FAILED(f_SysHr))
        return FALSE;

    // CREATE FileOpenDialog OBJECT
    IFileOpenDialog* f_FileSystem;
    f_SysHr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&f_FileSystem));
    if (FAILED(f_SysHr)) {
        CoUninitialize();
        return FALSE;
    }

    //  SHOW OPEN FILE DIALOG WINDOW
    f_SysHr = f_FileSystem->Show(NULL);
    if (FAILED(f_SysHr)) {
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  RETRIEVE FILE NAME FROM THE SELECTED ITEM
    IShellItem* f_Files;
    f_SysHr = f_FileSystem->GetResult(&f_Files);
    if (FAILED(f_SysHr)) {
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  STORE AND CONVERT THE FILE NAME
    PWSTR f_Path;
    f_SysHr = f_Files->GetDisplayName(SIGDN_FILESYSPATH, &f_Path);
    if (FAILED(f_SysHr)) {
        f_Files->Release();
        f_FileSystem->Release();
        CoUninitialize();
        return FALSE;
    }

    //  FORMAT AND STORE THE FILE PATH
    std::wstring path(f_Path);
    std::string c(path.begin(), path.end());
    sFilePath = c;

    //  FORMAT STRING FOR EXECUTABLE NAME
    const size_t slash = sFilePath.find_last_of("/\\");
    sSelectedFile = sFilePath.substr(slash + 1);

    //  SUCCESS, CLEAN UP
    CoTaskMemFree(f_Path);
    f_Files->Release();
    f_FileSystem->Release();
    CoUninitialize();
    return TRUE;
}

bool result = FALSE;
int main()
{
    result = openFile();
    switch (result) {
        case(TRUE): {
            printf("SELECTED FILE: %s\nFILE PATH: %s\n\n", sSelectedFile.c_str(), sFilePath.c_str());
            system("pause");
        }
        case(FALSE): {
            printf("ENCOUNTERED AN ERROR: (%d)\n", GetLastError());
            system("pause");
        }
    }
    return 0;
}
xCENTx
  • 61
  • 5