如何获得快捷目标
How to get shortcut target
我需要能够读取快捷方式(. 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的指针。
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何从C++中的依赖类型中获得它所依赖的类型
- 欧拉项目#8答案是大以获得有效答案
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何使用C/C++在MacOSX中获得键盘布局
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在不产生任何垃圾的情况下获得C中的像素
- C++A*算法并不总是在路径中具有目标节点
- 有没有一种方法可以在编译时获得作用域类名
- 如何在C++中获得"静态纯虚拟"功能?
- 基于树莓pi的tensorflow lite量化ssd目标检测
- Python中的for循环与C++有何不同
- 为测试目标创建具有不同源文件夹的文件
- 如何设置一个范围来提取我想要获得的信息
- 在多次运行中获得一致的callgrind输出
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 我可以获得未处理(目标)C++异常的堆栈跟踪吗
- 我可以获得IloNumVarArray的值吗?关于目标函数的定义
- 使用依赖项交叉编译:如何在主机上获得目标依赖项
- 如何获得快捷目标