如何获取快捷方式目标 [英] How to get shortcut target

查看:131
本文介绍了如何获取快捷方式目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to be able to read the target of a shortcut (a .lnk file).

我已经对此进行了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
< a href = http://msdn.microsoft.com/zh-CN/library/bb776891%28VS.85%29.aspx rel = nofollow noreferrer> http://msdn.microsoft.com/zh-CN /library/bb776891%28VS.85%29.aspx
http://www.codeproject.com/KB/shell/create_shortcut.aspx

I have Googled this and found numerous results that I have found to be helpful: 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

其中一些网页没有提及哪个标题我需要r个文件,但我不知道如何找到此信息。

Some of these web pages don't mention which header files I need, and I am unaware how to find this information out.

我当前试图使用的代码是:

The code that I am currently trying to get working is this:

#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;
}

您可能会发现这主要来自上述网站之一,他们没有提到他们使用了哪些标头,所以我有一个很好的猜测(似乎正在使用)。

You can probably see that this is mainly from one of the websites above, however they did not mention which headers they used, so I had a good guess (which seems to be working) at which ones to use.

当前我遇到的错误是:

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 ===|

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

I was hoping that someone may be able to give me some advice on this, whether it is just pointing me to some better links or, even better, possibly explaining the above code, how to find out which headers to use and where I am going wrong, or an entirely different solution that achieves the same result.

推荐答案

所有标头都可以,但是您错误地使用了宽字符串(基于wchar_t)和'普通'字符串(基于char):IPersistFile :: Load需要宽字符串而IShellLinkA :: GetPath采用普通字符串。
使用它应该编译:

All headers are fine, but you are using wide (wchar_t based) and 'normal' (char based) strings incorrectly: IPersistFile::Load takes a wide string while IShellLinkA::GetPath takes a normal string. Using this should compile:

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的指针。

Also if you just want the path, you can just pass 0 instead of a pointer to a WIN32_FIND_DATA.

这篇关于如何获取快捷方式目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆