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

Win32控制台获取可执行程序的快捷方式的目标位置、起始位置、快捷键、备注等,示例如下图:

 

 

#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:\\Users\\Public\\Desktop\\腾讯QQ.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;
}

 

posted @ 2021-01-04 17:34  任小七  阅读(880)  评论(0编辑  收藏  举报