SHOpenFolderAndSelectItems的用法

本文演示了如何使用Windows Shell API将文件路径转换为ITEMIDLIST,并通过Shell接口打开指定文件。首先获取桌面文件夹的IShellFolder接口,接着使用ParseDisplayName方法将文件路径转换成ITEMIDLIST,最后调用SHOpenFolderAndSelectItems函数打开文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  LPITEMIDLIST  pidl;
   LPCITEMIDLIST cpidl;
   LPCITEMIDLIST cpidl1;
   LPSHELLFOLDER pDesktopFolder;
   char          szPath[MAX_PATH];
   OLECHAR       olePath[MAX_PATH];
   ULONG         chEaten;
   ULONG         dwAttributes;
   HRESULT       hr;
   char          szPath1[MAX_PATH];

   //
   // Get the path to the file we need to convert.
   //
 //  GetCurrentDirectory(MAX_PATH, szPath);
   sprintf(szPath, "D://code管理//Demo");
   sprintf(szPath1, "D://code管理//Demo//HookManager_src.zip");
   //
   // Get a pointer to the Desktop's IShellFolder interface.
   //
   if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
   {
       //
       // IShellFolder::ParseDisplayName requires the file name be in
       // Unicode.
       //
       MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath, -1,
                           olePath, MAX_PATH);

       //
       // Convert the path to an ITEMIDLIST.
       //
       hr = pDesktopFolder->ParseDisplayName(NULL,0,olePath,&chEaten,&pidl,&dwAttributes);
       if (FAILED(hr))
       {
     AfxMessageBox("ERROR");
           // Handle error.
       }
    cpidl = pidl;

       MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szPath1, -1,
                           olePath, MAX_PATH);

       //
       // Convert the path to an ITEMIDLIST.
       //
       hr = pDesktopFolder->ParseDisplayName(NULL,0,olePath,&chEaten,&pidl,&dwAttributes);
       if (FAILED(hr))
       {
     AfxMessageBox("ERROR");
           // Handle error.
       }
  cpidl1 = pidl;
    HRESULT RE = CoInitialize(NULL);
    int re = SHOpenFolderAndSelectItems(cpidl,1,&cpidl1,NULL);

       //
       // pidl now contains a pointer to an ITEMIDLIST for ./readme.txt.
       // This ITEMIDLIST needs to be freed using the IMalloc allocator
       // returned from SHGetMalloc().
       //

       //release the desktop folder object
         pDesktopFolder->Release();
   }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值