How to get shortcut target

家住魔仙堡 提交于 2019-12-02 19:43:12

问题


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

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

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.

Currently the errors I am getting are:

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.


回答1:


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";

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



来源:https://stackoverflow.com/questions/6676779/how-to-get-shortcut-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!