0

I have learned C/C++ Basics and practiced , but I have hard time understanding Microsoft documentation and find it confusing Documention example

for example : I try to create command line program that should let the user open folder dialog and choose folder , as result the folders path should be stored in variable did research and found that there is many ways to achieve this goal but the best way is using IFileDialog::GetFolder method (shobjidl_core.h) what the difference between file dialogs?

The main question : How to get folders path as string variable based on user choice from file dialog?

There is c++ resources with practical tutorials ?

I try to understand how I use the following dialog : Folder dialog

it refernces me to: BROWSEINFOA structure

Would be very helpful if someone could explain how I can use this folder dialog or something better

any great tutorial of windows/linux file system handling

None
  • 41
  • 7
  • 6
    Have a read of [Get Started with Win32 and C++ - Working with Strings](https://learn.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings) – Richard Critten Jun 06 '22 at 21:55
  • As I understand TCHAR is legacy , how can I focus on must recent C++ libraries instead of legacy ones – None Jun 07 '22 at 01:55
  • you can just use the [`en_US.UTF-8` locale](https://stackoverflow.com/a/63454192/995714) and use `char*` everywhere for simplicity and portability. All modern platforms will just work with UTF-8 – phuclv Jun 07 '22 at 02:54
  • I tried using std::locale::global(std::locale("en_US.UTF-8")); but got same error also it makes mess with chars I used in other parts in my code , I edited my question to more modern libraries as I understand – None Jun 07 '22 at 08:00
  • @None you need some more compiler flags to make `std::locale("en_US.UTF-8")` work – phuclv Jun 07 '22 at 08:57
  • I have solved the issue using , wcout but I still don't understand the differences between file dialog why they kept in different libraries , for example : SHBrowseForFolder , IFileOpenDialog , CFileDialog .... – None Jun 10 '22 at 23:44
  • @phuclv: you can't force the `*A` Windows functions to accept or return UTF-8. The only Unicode functions are the `*W` variants. You get a proper mess if you pass a non-English UTF-8 string to an Windows ANSI function. The compiler is entirely not involved in this, nor is `std::locale`. – MSalters Jun 20 '22 at 23:27
  • 1
    @MSalters did you see the link above? MS nowadays recommends to use UTF-8 locale and the `*A` Win32 APIs nowadays can accept a UTF-8 string. Both the compiler and Windows SDK involve in this – phuclv Jun 21 '22 at 00:48

1 Answers1

1

I used to wcout to print out path

    TCHAR path[260];
    BROWSEINFO bi = { 0 };
    LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
    SHGetPathFromIDList(pidl, path);
    wcout << path << '\n';

We can use com interface as well :

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow);
    {
        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
        if (SUCCEEDED(hr))
        {
            IFileOpenDialog* pFileOpen;

            // Create the FileOpenDialog object.
            hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
            // if object created ( that's com object )
            if (SUCCEEDED(hr))
            {
                // Show the Open dialog box.
                hr = pFileOpen->Show(NULL);

                // Get the file name from the dialog box.
                if (SUCCEEDED(hr))
                {
                    IShellItem* pItem;
                    hr = pFileOpen->GetResult(&pItem);
                    if (SUCCEEDED(hr))
                    {
                        PWSTR pszFilePath;
                        hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);

                        // Display the file name to the user.
                        if (SUCCEEDED(hr))
                        {
                            MessageBoxW(NULL, pszFilePath, L"File Path", MB_OK);
                            CoTaskMemFree(pszFilePath);
                            cout << pszFilePath;

                            std::string stringtoconvert;

                            std::wstring temp = std::wstring(stringtoconvert.begin(), stringtoconvert.end());
                            LPCWSTR lpcwstr = temp.c_str();
                        }
                        pItem->Release();
                    }
                }
                pFileOpen->Release();
            }
            CoUninitialize();
        }
        return 0;
    }

Important to mention : Open and Save As common dialog boxes ( commdlg.h ) have been superseded by the Common Item Dialog

For linux and other platforms there is cross platforms libraries like Qt and more , here is a link for UI solutions

and about Microsoft documentation : I think there should be simpler examples and explanations its complicated & frustrating to learn this way.

None
  • 41
  • 7