FileOpenDialog窗口中的鼠标右键会出错

时间:2016-11-24 10:30:01

标签: c++ openfiledialog hwnd hresult

我有一个FileOpenDialog,它获取一个选定的文件夹名称,这很好用但是当我用鼠标右键单击文件夹或其他文件夹中的项目时,应用程序崩溃并抛出一个未处理的异常:未找到模块。我希望能够使用鼠标右键,所以我不想阻止它。我正在寻找一种解决方案来处理来自鼠标右键的输入,这样应用程序就不会崩溃。

HWND Hwnd;
std::vector<std::string> openFileDialog(std::vector<COMDLG_FILTERSPEC> filterSpec, bool allowMultiSelect)
{
std::vector<std::string> paths;

IFileOpenDialog *pFileOpen;

HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
    if (allowMultiSelect)
    {
        pFileOpen->SetOptions(FOS_ALLOWMULTISELECT);
    }
    pFileOpen->SetFileTypes(filterSpec.size(), filterSpec.data());

    hr = pFileOpen->Show(Hwnd);
    if (SUCCEEDED(hr))
    {
        IShellItemArray* results;
        hr = pFileOpen->GetResults(&results);

        pFileOpen->GetResults(&results);
        DWORD itemCount = 0;
        DWORD iterator = 0;
        results->GetCount(&itemCount);

        if (SUCCEEDED(hr))
        {
            for (iterator = 0; iterator < itemCount; iterator++)
            {
                PWSTR pszFilePath;
                IShellItem *pItem;
                results->GetItemAt(iterator, &pItem);
                hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
                if (SUCCEEDED(hr))
                {
                    LPWSTR path = pszFilePath;
                    std::wstring file = path;
                    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
                    paths.push_back(convert.to_bytes(file));

                }
                pItem->Release();
            }
        }
    }
}

pFileOpen->Release();
if (paths.size() == 0)
    paths.push_back("FAILED");

return paths;

}

0 个答案:

没有答案