IShellLink – how to get the original target path

  com, winapi, windows

Here is the scenario:
I created a shortcut in a windows PC with target C:UsersbDesktopNew Text Document.txt

https://www.mediafire.com/file/gp58qf6y1m3es36/shortcut/file

Then I copied the shortcut to another PC with different user name, and I want to retrieve the original target path.

If you open the shortcut file with text editor you can see the original path is preserved, so the goal is definitely possible.

The following code does not work despite the presence of SLGP_RAWPATH. It outputs C:UsersaDesktopNew Text Document.txt, changing user folder name to the one associated with the running program.
I understand that the problem is not about environment variable, because no environment variable name can be seen in the file. But I can’t find any documentation about this autorelocation behavior.

IShellLinkW*lnk;
if(CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLinkW,(LPVOID*)&lnk)==0){
    IPersistFile*file;
    if(lnk->QueryInterface(IID_IPersistFile,(void**)&file)==0){
        if(file->Load(L"shortcut",0)==0){
            wchar_t path[MAX_PATH];
            if(lnk->GetPath(path,_countof(path),0,SLGP_RAWPATH)==0){
                _putws(path);
            }
            IShellLinkDataList*sdl;
            if(lnk->QueryInterface(IID_IShellLinkDataList,(void**)&sdl)==0){
                EXP_SZ_LINK*lnkData;
                if(sdl->CopyDataBlock(EXP_SZ_LINK_SIG,(void**)&lnkData)==0){
                    _putws(lnkData->swzTarget);
                    LocalFree(lnkData);
                }
                sdl->Release();
            }
        }
        file->Release();
    }
    lnk->Release();
}

Source: Windows Questions

LEAVE A COMMENT