Win32控制台获取可执行程序的快捷方式的目标位置、起始位置

由于在开发中,使用Qt中的QFileInfo::symLinkTarget不能获取到正确的文件路径。

C:/Windows/Installer/{8F158BE6-D2F5-40CF-A51F-5C658A2A7CC5}/NewShortcut1_409C464E55B64E9C8A394B22BC19BCFB.exe

所以找到一种在window下查找快捷方式的属性的方法。

编译环境vs2019 C++

转载:https://www.cnblogs.com/rcg714786690/p/14231103.htmlWin32控制台获取可执行程序的快捷方式的目标位置、起始位置、快捷键、备注等 - 任小七 - 博客园 (cnblogs.com)

#include <iostream>
#include <atlstr.h>
#include <ShObjIdl.h>
#include <assert.h>
#include <ShlGuid.h>
using namespace std;

struct ShortcutAttribute
{
    CString Path_;//目标位置
    CString WorkingDirectory_;//起始位置
    CString Description_;//备注
    WORD HotKey_;//快捷键
    ShortcutAttribute()
    {
        Path_ = _T("");
        WorkingDirectory_ = _T("");
        Description_ = _T("");
    }
};


bool GetShortcutAttribute(IN CString shortcutPath, OUT ShortcutAttribute& shortattr)
{
    assert(shortcutPath != _T(""));
    LPTSTR lpsz = shortcutPath.GetBuffer(MAX_PATH);
    IShellLink* psl = NULL;
    // Initialize the COM library.
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        printf("ERROR - Could not initialize COM library");
        return false;
    }
    HRESULT hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf = NULL;
        hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
        if (SUCCEEDED(hres))
        {
#ifndef _UNICODE
            wchar_t wsz[MAX_PATH];
            ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, wsz, MAX_PATH);
            hres = ppf->Load(wsz, STGM_READ);
#else
            hres = ppf->Load(lpsz, STGM_READ);
#endif
            if (SUCCEEDED(hres))
            {
                //1.目标位置
                WIN32_FIND_DATA wfd;
                hres = psl->GetPath(shortattr.Path_.GetBuffer(MAX_PATH), MAX_PATH, &wfd, SLGP_UNCPRIORITY);

                //2.起始位置
                hres = psl->GetWorkingDirectory(shortattr.WorkingDirectory_.GetBuffer(MAX_PATH), MAX_PATH);

                //3.备注
                hres = psl->GetDescription(shortattr.Description_.GetBuffer(MAX_PATH), MAX_PATH);

                //4.快捷键
                hres = psl->GetHotkey(&shortattr.HotKey_);

                shortattr.Path_.ReleaseBuffer();
                shortattr.WorkingDirectory_.ReleaseBuffer();
                shortattr.Description_.ReleaseBuffer();
            }
            ppf->Release();
        }
        psl->Release();
    }
    shortcutPath.ReleaseBuffer();
    CoUninitialize();
    return true;
}

int main(int argc, char* argv[])
{
    ShortcutAttribute sa;
    bool flag = GetShortcutAttribute(CString(L"C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Git/Git GUI.lnk"), sa);
    if (flag)
    {
        //cout << CT2A(sa.Path_.GetBuffer()) << endl;
        cout << CT2A(sa.WorkingDirectory_.GetBuffer()) << endl;
        //cout << CT2A(sa.Description_.GetBuffer()) << endl;
        //cout << sa.HotKey_ << endl;
    }
    //getchar();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值