读取Windows快捷方式信息

最近有用到,但对COM也不熟,所以就不多介绍了。

环境:VS2013;字符集:多字节集。

#include <string>
#include <iostream>
#include <shobjidl.h>
#include <shlguid.h>

int main() {
    IShellLink* plnk;
    CoInitialize(nullptr);
    if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&plnk))) {
        IPersistFile* ppf;
        if(SUCCEEDED(plnk->QueryInterface<IPersistFile>(&ppf))) {
            if(SUCCEEDED(ppf->Load(LR"(C:\Users\Tao\Desktop\QQ影音.lnk)", STGM_READ))) {
                if(SUCCEEDED(plnk->Resolve(GetDesktopWindow(), 0))) {
                    char out[4096]; // 足够了吧?
                    if(SUCCEEDED(plnk->GetPath(&out[0], _countof(out), nullptr, SLGP_RAWPATH)))
                        std::cout << "PATH: " << out << std::endl;
                    if(SUCCEEDED(plnk->GetArguments(&out[0], _countof(out))))
                        std::cout << "ARGS: " << out << std::endl;
                    if(SUCCEEDED(plnk->GetDescription(&out[0], _countof(out))))
                        std::cout << "DESC: " << out << std::endl;

                    int i;
                    if(SUCCEEDED(plnk->GetIconLocation(&out[0], _countof(out), &i)))
                        std::cout << "ICON: " << out << ", INDEX: " << i << std::endl;

                    if(SUCCEEDED(plnk->GetWorkingDirectory(&out[0], _countof(out))))
                        std::cout << "WD: " << out << std::endl;
                }
            }
            ppf->Release();
        }
        plnk->Release();
    }

    CoUninitialize();
    return 0;
}

示例输出:

PATH: F:\Program Files\Tencent\QQPlayer\QQPlayer.exe
ARGS:
DESC: QQ影音,五星级的视听享受
ICON: F:\Program Files\Tencent\QQPlayer\QQPlayer.exe, INDEX: 0
WD: F:\Program Files\Tencent\QQPlayer

参考:IShellLink interface

发表于:2016年01月27日 ,阅读量:453 ,标签:WinAPI

版权声明:若非特别注明,本站所有文章均为作者原创,转载请务必注明原文地址。