【发布时间】: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