无法获取Windows 7 C ++上虚拟文件夹的路径(与外壳名称空间扩展有关) [英] cannot get the path for the virtual folder on windows 7 C++(shell namespace extension related)

查看:262
本文介绍了无法获取Windows 7 C ++上虚拟文件夹的路径(与外壳名称空间扩展有关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用Microsoft Windows SDK 7.0,explorerDataProvider,我在Windows 7上安装了一个虚拟文件夹.

By using Microsoft windows SDK 7.0, explorerDataProvider, I installed a virtual folder on windows 7.

当我从应用程序打开文件浏览对话框时,

When I open the file browse dialog from an application,

CFileDialog dlg(TRUE, NULL, 0, OFN_ENABLESIZING | OFN_ALLOWMULTISELECT, L"all(*.*)|*.*||", this);

INT_PTR result = dlg.DoModal();

if (result == IDOK)
{
   .
   . 
   .
   //some actions
}

它还可以显示虚拟文件夹:

it can also display the virtual folder:

但是当我选择文件时,例如零"可以单击打开",

But when I select the file, like "Zero" can then clicks "Open",

我试图向

INT_PTR result = dlg.DoModal();

但是似乎此错误发生在DoModal()内部.

But it seems that this error happens inside DoModal().

我正在研究并修改虚拟文件夹的代码:

I doing some research and revise the code of virtual folder:

HRESULT CFolderViewImplFolder::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl, ULONG *rgfInOut)
{
// If SFGAO_FILESYSTEM is returned, GetDisplayNameOf(SHGDN_FORPARSING) on that item MUST
// return a filesystem path.
HRESULT hr = E_INVALIDARG;

DWORD dwAttribs = 0;
dwAttribs |= SFGAO_BROWSABLE;
if (1 == cidl)
{
    int nLevel = 0;
    hr = _GetLevel(apidl[0], &nLevel);
    if (SUCCEEDED(hr))
    {
        BOOL fIsFolder = FALSE;
        hr = _GetFolderness(apidl[0], &fIsFolder);
        if (SUCCEEDED(hr))
        {

            if (fIsFolder)
            {
                dwAttribs |= SFGAO_FOLDER;

                dwAttribs |= SFGAO_FILESYSANCESTOR;
            }
            else
            {
                dwAttribs |= SFGAO_SYSTEM;

                dwAttribs |= SFGAO_FILESYSTEM;
            }

            if (nLevel < g_nMaxLevel)
            {
                dwAttribs |= SFGAO_HASSUBFOLDER;
            }
        }
    }
}

*rgfInOut &= dwAttribs;

return hr;
}

并返回GetDisplayNameOf()

HRESULT CFolderViewImplFolder::GetDisplayNameOf(PCUITEMID_CHILD pidl, SHGDNF shgdnFlags, STRRET *pName)
{
HRESULT hr = S_OK;
if (shgdnFlags & SHGDN_FORPARSING)
{
    WCHAR szDisplayName[MAX_PATH];
    if (shgdnFlags & SHGDN_INFOLDER)
    {
        // This form of the display name needs to be handled by ParseDisplayName.
        hr = _GetName(pidl, szDisplayName, ARRAYSIZE(szDisplayName));
    }
    else
    {
        PWSTR pszThisFolder = L"Computer\\Jerry";

            StringCchCopy(szDisplayName, ARRAYSIZE(szDisplayName), pszThisFolder);
            StringCchCat(szDisplayName, ARRAYSIZE(szDisplayName), L"\\");

            WCHAR szName[MAX_PATH];
            hr = _GetName(pidl, szName, ARRAYSIZE(szName));
            if (SUCCEEDED(hr))
            {
                StringCchCat(szDisplayName, ARRAYSIZE(szDisplayName), szName);
            }
    }
    if (SUCCEEDED(hr))
    {
        hr = StringToStrRet(szDisplayName, pName);
    }
}
else
{
    PWSTR pszName;
    hr = _GetName(pidl, &pszName);
    if (SUCCEEDED(hr))
    {
        hr = StringToStrRet(pszName, pName);
        CoTaskMemFree(pszName);
    }
}
return hr;
}

但是它仍然在应用程序中统计错误消息路径不存在". 我在GetDisplayNameOf中添加了断点,但是当应用程序打开文件浏览对话框时,它永远不会触发.但是,如果我仅通过双击计算机"打开一个常规文件夹,就会触发它.

But it still stats error message of "Path does not exist" in the application. I add breakpoints in GetDisplayNameOf, but it is never triggered when the application opens a file browse dialog. But if I open a regular folder just by double click "Computer", it will be triggered.

似乎无法弃用或覆盖路径不存在"的错误消息.我可以修改虚拟文件夹以返回正确路径的任何方法吗?

The error message of "Path does not exist" seems cannot be deprecated or covered. Any way that I can revise the virtual folder to return the correct path ?

更新:我尝试了IFileDialog,我从MSDN示例中复制了代码,代码,并添加了断点以查看将要发生的情况.但是,

Update: I tried IFileDialog, I copied the code from the MSDN sample, code, and add breakpoints to see what will happen. However,

// Show the dialog
hr = pfd->Show(NULL);//the breakpoint is added here
if (SUCCEEDED(hr))
{
  // Obtain the result once the user clicks 
 // the 'Open' button.
 // The result is an IShellItem object.
 IShellItem *psiResult;
 hr = pfd->GetResult(&psiResult);
 if (SUCCEEDED(hr))
 {
   //do something
 }
}

当我单击打开"按钮时,仍然显示路径不存在,请检查路径并重试"的错误消息框.单击打开"按钮后,它永远不会出现在pfd->Show(NULL)中.

When I click "Open" button, the error message box of "Path does not exist, check the path and try again" still show up. And it never comes out of pfd->Show(NULL) after I click Open" button.

基于示例代码中的注释,它应该从hr = pfd->Show(NULL);中出来并到达下一行.但是错误消息出现在hr = pfd->Show(NULL);内部.

Based on the comments in the sample code, it should come out of hr = pfd->Show(NULL); and reach to the next line.but the error message happens inside the hr = pfd->Show(NULL);.

更新:

IFileDialog中,我尝试了

hr = pfd->GetOptions(&dwFlags);
            if (SUCCEEDED(hr))
            {

                hr = pfd->SetOptions(dwFlags | FOS_ALLNONSTORAGEITEMS);
                if (SUCCEEDED(hr))
                {
                    // Show the dialog
                    hr = pfd->Show(NULL);
                    if (SUCCEEDED(hr))
                    {
                        // Obtain the result once the user clicks 
                        // the 'Open' button.
                        // The result is an IShellItem object.
                        IShellItem *psiResult;
                        hr = pfd->GetResult(&psiResult);
                        if (SUCCEEDED(hr))
                        {
                            // We are just going to print out the 
                            // name of the file for sample sake.
                            PWSTR pszFilePath = NULL;
                            hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH,
                                &pszFilePath);
                            if (SUCCEEDED(hr))
                            {
                                TaskDialog(NULL,
                                    NULL,
                                    L"CommonFileDialogApp",
                                    pszFilePath,
                                    NULL,
                                    TDCBF_OK_BUTTON,
                                    TD_INFORMATION_ICON,
                                    NULL);
                                CoTaskMemFree(pszFilePath);
                            }
                            psiResult->Release();
                        }
                    }

                }

我也尝试过

hr = pfd->SetOptions(dwFlags);//delete FOS_FORCEFILESYSTEM         

并尝试

DWORD dwFlags = 0;
if (SUCCEEDED(hr))
{
    // overwrite options completely
    hr = pfd->SetOptions(dwFlags | FOS_ALLNONSTORAGEITEMS);

我还尝试用名称空间扩展代码返回全名:

I also tried to return the whole name in namespace extension code:

WCHAR szDisplayName[MAX_PATH];
    if (shgdnFlags & SHGDN_INFOLDER)
    {
        // This form of the display name needs to be handled by ParseDisplayName.
        hr = _GetName(pidl, szDisplayName, ARRAYSIZE(szDisplayName));
    }
    else
    {
        PWSTR pszThisFolder;// = L"Computer\\Jerry";
        hr = SHGetNameFromIDList(m_pidl, (shgdnFlags & SHGDN_FORADDRESSBAR) ? SIGDN_DESKTOPABSOLUTEEDITING : SIGDN_DESKTOPABSOLUTEPARSING, &pszThisFolder);
        if (SUCCEEDED(hr))
        {
            StringCchCopy(szDisplayName, ARRAYSIZE(szDisplayName), pszThisFolder);
            StringCchCat(szDisplayName, ARRAYSIZE(szDisplayName), L"\\");

            WCHAR szName[MAX_PATH];
            hr = _GetName(pidl, szName, ARRAYSIZE(szName));
            if (SUCCEEDED(hr))
            {
                StringCchCat(szDisplayName, ARRAYSIZE(szDisplayName), szName);
            }
            CoTaskMemFree(pszThisFolder);
        }
    }
    if (SUCCEEDED(hr))
    {
        hr = StringToStrRet(szDisplayName, pName);
    }

但是结果是:

更新:

hr = pfd->SetOptions(dwFlags | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE);

这对我有用!

推荐答案

在@RemyLebeau的帮助下,我在代码中进行了一些测试.最后,我找到了适合我的解决方案: 我从MSDN复制了IFileDialog的代码: https ://msdn.microsoft.com/zh-CN/library/windows/desktop/bb776913.aspx#file_types 然后修改一下:

With the help of @RemyLebeau, I did some tests in my code. Finally, I found the working solution for me: I copied the code from MSDN for IFileDialog: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913.aspx#file_types and then revise it a little bit:

HRESULT BasicFileOpen()
{
// CoCreate the File Open Dialog object.
IFileDialog *pfd = NULL;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
    // Create an event handling object, and hook it up to the dialog.
    IFileDialogEvents *pfde = NULL;
    hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));
    if (SUCCEEDED(hr))
    {
        // Hook up the event handler.
        DWORD dwCookie;
        hr = pfd->Advise(pfde, &dwCookie);
        if (SUCCEEDED(hr))
        {
            // Set the options on the dialog.
            DWORD dwFlags = 0;
            if (SUCCEEDED(hr))
            {
                // the default flag is FOS_PATHMUSTEXIST and FOS_FILEMUSTEXIST, so I set overwrite it with these two flags.
                hr = pfd->SetOptions(dwFlags | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE);
                if (SUCCEEDED(hr))
                {
                    // Show the dialog
                    hr = pfd->Show(NULL);
                    if (SUCCEEDED(hr))
                    {
                        // Obtain the result once the user clicks 
                        // the 'Open' button.
                        // The result is an IShellItem object.
                        IShellItem *psiResult;
                        hr = pfd->GetResult(&psiResult);
                        if (SUCCEEDED(hr))
                        {
                            // We are just going to print out the 
                            // name of the file for sample sake.
                            PWSTR pszFilePath = NULL;
                            hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH,
                                &pszFilePath);
                            if (SUCCEEDED(hr))
                            {
                                TaskDialog(NULL,
                                    NULL,
                                    L"CommonFileDialogApp",
                                    pszFilePath,
                                    NULL,
                                    TDCBF_OK_BUTTON,
                                    TD_INFORMATION_ICON,
                                    NULL);
                                CoTaskMemFree(pszFilePath);
                            }
                            psiResult->Release();
                        }
                    }

                }
            }
            // Unhook the event handler.
            pfd->Unadvise(dwCookie);
        }
        pfde->Release();
    }
    pfd->Release();
}
return hr;
}

关键是要用SetOptions覆盖选项.基于MSDN: https://msdn.microsoft.com/zh-CN/library/windows/desktop/dn457282(v=vs.85).aspx

The key point is to overwrite the options by SetOptions. Based on the MSDN:https://msdn.microsoft.com/en-us/library/windows/desktop/dn457282(v=vs.85).aspx ,

FOS_PATHMUSTEXIST
The item returned must be in an existing folder. This is a default value.
FOS_FILEMUSTEXIST
The item returned must exist. This is a default value for the Open dialog.

这些标志是阻止其返回的默认值. 所以我将其设置为:

These flags are default values which prevent it returns. So I set it to :

hr = pfd->SetOptions(dwFlags | FOS_ALLNONSTORAGEITEMS | FOS_NOVALIDATE);

这些标志的意思是:

FOS_NOVALIDATE
Do not check for situations that would prevent an application from opening the selected file, such as sharing violations or access denied errors.

FOS_ALLNONSTORAGEITEMS
Enables the user to choose any item in the Shell namespace, not just those with SFGAO_STREAM or SFAGO_FILESYSTEM attributes. This flag cannot be combined with FOS_FORCEFILESYSTEM.

然后它终于可以工作了!

Then it finally works!

这篇关于无法获取Windows 7 C ++上虚拟文件夹的路径(与外壳名称空间扩展有关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆