【问题标题】:How to get shortcut target如何获取快捷方式目标
【发布时间】:2011-07-13 09:25:30
【问题描述】:

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

我在 Google 上搜索了这个并发现了许多我认为有帮助的结果: http://cboard.cprogramming.com/windows-programming/62962-ishelllink-getpath-dev-cplusplus.html http://www.go4answers.com/Example/get-shortcut-target-cpp-win64-216615.aspx http://msdn.microsoft.com/en-us/library/bb776891%28VS.85%29.aspx http://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 ===|

我希望有人可以就此给我一些建议,无论是只是为我指出一些更好的链接,或者甚至更好地解释上面的代码,如何找出要使用的标头以及在哪里使用我错了,或者完全不同的解决方案达到了相同的结果。

【问题讨论】:

    标签: c++ gcc codeblocks shortcut


    【解决方案1】:

    所有标题都很好,但是您错误地使用了宽(基于 wchar_t)和“普通”(基于字符)字符串: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 的指针。

    【讨论】:

    • 感谢您的回复。这似乎已经对一些错误进行了排序,但是现在我得到了一个未定义的引用:“CoInitialize@4”、“CoCreateInstance@20”和“IID_IPersistFile”。你能告诉我如何解决这些错误吗?谢谢
    • 你需要链接到ole32.lib
    • 我之前没有做过链接,所以你能告诉我这看起来是否正确吗? stackoverflow.com/questions/5862757/… 非常感谢
    • 这似乎成功了(尽管我链接了 ole32.a(这是同一件事吗?))我还有一个错误:“22|undefined reference to `IID_IPersistFile'”对如何解决这个问题有任何建议吗?谢谢
    • 好的,现在没问题,只需要链接uuid.lib,目前没有更多错误:)
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    相关资源
    最近更新 更多