窗户读取快捷方式文件的目标在C ++ [英] windows read the target of shortcut file in c++

查看:135
本文介绍了窗户读取快捷方式文件的目标在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取快捷方式文件的目标上的窗口。使用boost :: read_symlink会抛出异常说的文件或目录不是多分点的消息。试过

  INT主(INT ARGC,_TCHAR *的argv [])
{
    尝试{
    提高::文件系统::路径目标=的boost ::文件系统:: read_symlink(C:\\\\ \\\\ TMP blobstore_2.lnk);
    COUT&所述;&下; target.string();
    }赶上(常量的boost ::文件系统:: filesystem_error&安培;除息)
    {
        COUT<<渔获<< ex.what(); //输出的文件或目录不是多分点
    }    性病:: ifstream的smbConfStream(C:\\\\ \\\\ TMP sym_file_2.lnk);
    字符串SS((的std :: istreambuf_iterator<焦炭>(smbConfStream))
        的std :: istreambuf_iterator<焦炭>());
    COUT<<&ENDL LT;<SS:<< SS; //从SS它看起来像目标信息的输出里面SS present与其他二进制数据一起。如何干净地获得目标的。    INT I;
    CIN>>我;    返回0;
}


解决方案

一个视窗.lnk文件不是一个符号链接。这是一个快捷方式文件。您使用<$c$c>IShellLink接口来操纵它。

借助文档包含下面的例子演示了如何解决一个快捷方式文件

  // ResolveIt  - 使用壳牌和的IShellLink接口IPersistFile
//检索现有的快捷方式路径和描述。
//
//返回调用接口的成员函数的结果。
//
//参数:
// HWND - 一个句柄父窗口。壳牌使用此窗口
//显示一个对话框,如果它需要提示用户输入更多
//在解析链接信息。
// lpszLinkFile - 包含链路的路径的缓冲器的地址,
//包括文件名。
// lpszPath - 缓冲的接收链路的路径地址
                  靶向于,包括文件名。
// lpszDesc - 缓冲区接收的说明地址
//外壳链接,存储在链接的注释字段
//属性。的#includestdafx.h中
#包括WINDOWS.H
#包括shobjidl.h
#包括shlguid.h
#包括strsafe.hHRESULT ResolveIt(HWND HWND,LPCSTR lpszLinkFile,LPWSTR lpszPath,诠释iPathBufferSize)
{
    HRESULT HRES;
    *的IShellLink PSL;
    WCHAR szGotPath [MAX_PATH];
    WCHAR szDescription [MAX_PATH];
    WIN32_FIND_DATA WFD;    * lpszPath = 0; //假设失败    //获取指向接口​​的IShellLink。假定CoInitialize的
    //已经被调用。
    HRES = CoCreateInstance的(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(LPVOID *)及PSL);
    如果(成功(HRES))
    {
        IPersistFile * PPF;        //获取一个指针IPersistFile接口。
        HRES = psl-&GT;的QueryInterface(IID_IPersistFile,(无效**)及PPF);        如果(成功(HRES))
        {
            WCHAR WSZ [MAX_PATH];            //确保该字符串是单向code。
            的MultiByteToWideChar(CP_ACP,0,lpszLinkFile,-1,WSZ,MAX_PATH);            //添加code此处查看从MultiByteWideChar返回值
            //成功。            //加载的快捷方式。
            HRES = ppf-&GT;负载(WSZ,STGM_READ);            如果(成功(HRES))
            {
                //解决的链接。
                HRES = psl-&GT;解决(HWND,0);                如果(成功(HRES))
                {
                    //获取路径链接目标。
                    HRES = psl-&GT;的getPath(szGotPath,MAX_PATH,(WIN32_FIND_DATA *)及WFD,SLGP_SHORTPATH​​);                    如果(成功(HRES))
                    {
                        //获取目标的描述。
                        HRES = psl-&GT; GetDescription(szDescription,MAX_PATH);                        如果(成功(HRES))
                        {
                            HRES = StringCbCopy(lpszPath,iPathBufferSize,szGotPath);
                            如果(成功(HRES))
                            {
                                //处理成功
                            }
                            其他
                            {
                                //处理错误
                            }
                        }
                    }
                }
            }            //指针释放到IPersistFile接口。
            ppf-&GT;发行();
        }        //指针释放到接口的IShellLink。
        psl-&GT;发行();
    }
    返回HRES;
}

How to read the target of shortcut file on windows. Tried using boost::read_symlink which throws exception saying "the file or directory is not a reparse point" message.

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;
}

解决方案

A Windows .lnk file is not a symbolic link. It is a shortcut file. You use the IShellLink interface to manipulate it.

The documentation contains the following example demonstrating how to resolve a shortcut file.

// 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; 
}

这篇关于窗户读取快捷方式文件的目标在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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