如何获得快捷目标

How to get shortcut target

本文关键字:目标 何获得      更新时间:2023-10-16

我需要能够读取快捷方式(. link文件)的目标。

我在谷歌上搜索了一下,发现了很多有用的结果:http://cboard.cprogramming.com/windows-programming/62962-ishelllink-getpath-dev-cplusplus.htmlhttp://www.go4answers.com/Example/get-shortcut-target-cpp-win64-216615.aspxhttp://msdn.microsoft.com/en-us/library/bb776891%28VS.85%29.aspxhttp://www.codeproject.com/KB/shell/create_shortcut.aspx

有些网页没有提到我需要哪些头文件,我不知道如何找到这些信息。

我目前正在尝试工作的代码是:

#include <windows.h>
#include <string>
#include <objidl.h>   /* For IPersistFile */
#include <shlobj.h>   /* For IShellLink */
using namespace std;
int main(void)
{
IShellLink* psl;
wchar_t* tempStr = new wchar_t[MAX_PATH];
string path = "E:\shortcuts\myshortcut.lnk";
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl);
if (SUCCEEDED(hr))
{
    IPersistFile* ppf;
    hr = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
    if (SUCCEEDED(hr))
    {
        hr = ppf->Load(path.c_str(), STGM_READ);
        if (SUCCEEDED(hr))
        {
            WIN32_FIND_DATA wfd;
            psl->GetPath(tempStr, MAX_PATH, &wfd, SLGP_UNCPRIORITY | SLGP_RAWPATH);
        }
    }
}
    return 0;
}

你可能会看到这主要来自上面的一个网站,但是他们没有提到他们使用了哪些头,所以我有一个很好的猜测(这似乎是有效的)在使用哪些头。

目前我得到的错误是:

In function 'int main()':
24|error: no matching function for call to 'IPersistFile::Load(const char*, int)'
29|error: no matching function for call to 'IShellLinkA::GetPath(wchar_t*&, int, WIN32_FIND_DATA*, int)'
||=== Build finished: 2 errors, 0 warnings ===|

我希望有人能给我一些建议,是否只是指向我一些更好的链接,或者,甚至更好,可能解释上述代码,如何找出哪些头使用和我在哪里出错,或一个完全不同的解决方案,达到相同的结果。

所有标头都很好,但是您使用的是宽(基于wchar_t)和'正常'(基于char)字符串不正确:IPersistFile::Load需要一个宽字符串,而IShellLinkA::GetPath需要一个正常字符串。使用它应该编译:

IShellLinkA* psl; //specify the ansi version explicitely
CoInitialize( 0 ); //you forgot this, needed for all COM calls to work
char* tempStr = new char[ MAX_PATH ];
std::wstring path = L"E:\shortcuts\myshortcut.lnk";

如果你只是想要路径,你可以只传递0而不是指向WIN32_FIND_DATA的指针。