Windows在C ++中读取快捷方式文件的目标

Talespin_Kit

如何在Windows上读取快捷方式文件的目标。使用boost :: read_symlink进行了尝试,该异常引发异常,提示“文件或目录不是重新解析点”消息。

int main(int argc, _TCHAR* argv[])
{           
    try {
    boost::filesystem::path target = boost::filesystem::read_symlink("c:\\tmp\\blobstore_2.lnk");
    cout<<target.string();
    } catch(const boost::filesystem::filesystem_error& ex)
    {
        cout<<"in catch"<<ex.what(); // prints "the file or directory is not a reparse point"
    }

    std::ifstream smbConfStream("c:\\tmp\\sym_file_2.lnk");
    string ss((std::istreambuf_iterator<char>(smbConfStream)),
        std::istreambuf_iterator<char>());
    cout <<endl<<" ss: "<<ss; // From the output of the "ss" it looks like the information of the target is present inside ss along with other binary data. How to cleanly get the target out.

    int i;
    cin>>i;

    return 0;
}
大卫·赫弗南(David Heffernan)

Windows .lnk文件不是符号链接。这是一个快捷方式文件。您可以使用该IShellLink界面进行操作。

文档包含以下示例,演示了如何解析快捷方式文件。

// ResolveIt - Uses the Shell's IShellLink and IPersistFile interfaces 
//             to retrieve the path and description from an existing shortcut. 
//
// Returns the result of calling the member functions of the interfaces. 
//
// Parameters:
// hwnd         - A handle to the parent window. The Shell uses this window to 
//                display a dialog box if it needs to prompt the user for more 
//                information while resolving the link.
// lpszLinkFile - Address of a buffer that contains the path of the link,
//                including the file name.
// lpszPath     - Address of a buffer that receives the path of the link
                  target, including the file name.
// lpszDesc     - Address of a buffer that receives the description of the 
//                Shell link, stored in the Comment field of the link
//                properties.

#include "stdafx.h"
#include "windows.h"
#include "shobjidl.h"
#include "shlguid.h"
#include "strsafe.h"

HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPWSTR lpszPath, int iPathBufferSize) 
{ 
    HRESULT hres; 
    IShellLink* psl; 
    WCHAR szGotPath[MAX_PATH]; 
    WCHAR szDescription[MAX_PATH]; 
    WIN32_FIND_DATA wfd; 

    *lpszPath = 0; // Assume failure 

    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called. 
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); 
    if (SUCCEEDED(hres)) 
    { 
        IPersistFile* ppf; 

        // Get a pointer to the IPersistFile interface. 
        hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 

        if (SUCCEEDED(hres)) 
        { 
            WCHAR wsz[MAX_PATH]; 

            // Ensure that the string is Unicode. 
            MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH); 

            // Add code here to check return value from MultiByteWideChar 
            // for success.

            // Load the shortcut. 
            hres = ppf->Load(wsz, STGM_READ); 

            if (SUCCEEDED(hres)) 
            { 
                // Resolve the link. 
                hres = psl->Resolve(hwnd, 0); 

                if (SUCCEEDED(hres)) 
                { 
                    // Get the path to the link target. 
                    hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); 

                    if (SUCCEEDED(hres)) 
                    { 
                        // Get the description of the target. 
                        hres = psl->GetDescription(szDescription, MAX_PATH); 

                        if (SUCCEEDED(hres)) 
                        {
                            hres = StringCbCopy(lpszPath, iPathBufferSize, szGotPath);
                            if (SUCCEEDED(hres))
                            {
                                // Handle success
                            }
                            else
                            {
                                // Handle the error
                            }
                        }
                    }
                } 
            } 

            // Release the pointer to the IPersistFile interface. 
            ppf->Release(); 
        } 

        // Release the pointer to the IShellLink interface. 
        psl->Release(); 
    } 
    return hres; 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Windows中查找错误的快捷方式文件

来自分类Dev

键盘快捷方式不适用于USER / Links文件夹中的快捷方式-Windows 8.1

来自分类Dev

Windows中“我的电脑”的地址目标是什么?如何制作自定义快捷方式?

来自分类Dev

Windows:在快捷方式目标字符串中包含端口号

来自分类Java

如何使用Java创建(和读取)Windows快捷方式(.lnk文件)?

来自分类Dev

Windows 快捷方式上的目标字段被禁用

来自分类Dev

Windows OS中的Shell快捷方式

来自分类Dev

Windows快捷方式中的重定向

来自分类Dev

在Linux中创建Windows快捷方式

来自分类Dev

在Windows中禁用快捷方式

来自分类Dev

在 Windows 10 中阻止快捷方式

来自分类Dev

以编程方式为Windows中的文件夹创建快捷方式

来自分类Dev

c中的pragma omp快捷方式

来自分类Dev

在Windows 10上的JuliaPro中运行Julia文件的快捷方式(或如何将Ctrl-Enter设置为运行文件的快捷方式)?

来自分类Dev

如何创建桌面快捷方式(链接),但是具有自己的快捷方式文件扩展名(例如.appfolder),而不是在C#中以编程方式创建.lnk?

来自分类Dev

Ubuntu 16.04-烦人的.ink文件->导致Windows中的快捷方式:如何修复?

来自分类Dev

如何通过拖放将文件名传递到Windows中的快捷方式

来自分类Dev

Windows 10拒绝在“开始”菜单文件夹中创建快捷方式

来自分类Dev

如何阻止Windows在SendTo文件夹中添加Bluetooth快捷方式?

来自分类Dev

文件夹的 Inno Setup 快捷方式未在 Windows 10 中打开

来自分类Dev

使用文件路径 C# 在收藏夹中创建快捷方式

来自分类Dev

Windows XP新文件夹的快捷方式

来自分类Dev

Windows OS窗口文件路径的快捷方式

来自分类Dev

Cygwin执行Windows快捷方式文件(.LNK)

来自分类Dev

如何将Windows快捷方式文件转换为常规文件,但保留快捷方式名称?

来自分类Dev

如何从R中的Windows快捷方式中提取路径?

来自分类Dev

在Windows 10中重新映射Win + arrow快捷方式

来自分类Dev

无法在安装CIFS的Windows共享中遵循快捷方式

来自分类Dev

如何在Power Shell中遵循Windows快捷方式?

Related 相关文章

  1. 1

    在Windows中查找错误的快捷方式文件

  2. 2

    键盘快捷方式不适用于USER / Links文件夹中的快捷方式-Windows 8.1

  3. 3

    Windows中“我的电脑”的地址目标是什么?如何制作自定义快捷方式?

  4. 4

    Windows:在快捷方式目标字符串中包含端口号

  5. 5

    如何使用Java创建(和读取)Windows快捷方式(.lnk文件)?

  6. 6

    Windows 快捷方式上的目标字段被禁用

  7. 7

    Windows OS中的Shell快捷方式

  8. 8

    Windows快捷方式中的重定向

  9. 9

    在Linux中创建Windows快捷方式

  10. 10

    在Windows中禁用快捷方式

  11. 11

    在 Windows 10 中阻止快捷方式

  12. 12

    以编程方式为Windows中的文件夹创建快捷方式

  13. 13

    c中的pragma omp快捷方式

  14. 14

    在Windows 10上的JuliaPro中运行Julia文件的快捷方式(或如何将Ctrl-Enter设置为运行文件的快捷方式)?

  15. 15

    如何创建桌面快捷方式(链接),但是具有自己的快捷方式文件扩展名(例如.appfolder),而不是在C#中以编程方式创建.lnk?

  16. 16

    Ubuntu 16.04-烦人的.ink文件->导致Windows中的快捷方式:如何修复?

  17. 17

    如何通过拖放将文件名传递到Windows中的快捷方式

  18. 18

    Windows 10拒绝在“开始”菜单文件夹中创建快捷方式

  19. 19

    如何阻止Windows在SendTo文件夹中添加Bluetooth快捷方式?

  20. 20

    文件夹的 Inno Setup 快捷方式未在 Windows 10 中打开

  21. 21

    使用文件路径 C# 在收藏夹中创建快捷方式

  22. 22

    Windows XP新文件夹的快捷方式

  23. 23

    Windows OS窗口文件路径的快捷方式

  24. 24

    Cygwin执行Windows快捷方式文件(.LNK)

  25. 25

    如何将Windows快捷方式文件转换为常规文件,但保留快捷方式名称?

  26. 26

    如何从R中的Windows快捷方式中提取路径?

  27. 27

    在Windows 10中重新映射Win + arrow快捷方式

  28. 28

    无法在安装CIFS的Windows共享中遵循快捷方式

  29. 29

    如何在Power Shell中遵循Windows快捷方式?

热门标签

归档