Resolving a Windows Shell link to find the target path
See the question and my original answer on StackOverflowAs explained in the comment, it behaves like a Shell Link. Here is one way to resolve the target path, it uses the IShellItem2 COM interface and the System.Link.TargetParsingPath property.
C++ version:
#include <windows.h>
#include <stdio.h>
#include <shobjidl_core.h>
#include <Propkey.h>
int main()
{
CoInitialize(NULL);
{
IShellItem2* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\mypath\\link", NULL, IID_PPV_ARGS(&item))))
{
LPWSTR path = NULL;
if (SUCCEEDED(item->GetString(PKEY_Link_TargetParsingPath, &path)))
{
wprintf(L"Target: %s\n", path); // x:\path\to\dir
CoTaskMemFree(name);
}
item->Release();
}
}
CoUninitialize();
return 0;
}
C version:
#include <windows.h>
#include <stdio.h>
#include <shobjidl_core.h>
#include <Propkey.h>
int main()
{
CoInitialize(NULL);
{
IShellItem2* item;
if (SUCCEEDED(SHCreateItemFromParsingName(L"c:\\mypath\\link", NULL, &IID_IShellItem2, &item)))
{
LPWSTR path = NULL;
if (SUCCEEDED(item->lpVtbl->GetString(item, &PKEY_Link_TargetParsingPath, &path)))
{
wprintf(L"Target: %s\n", path); // x:\path\to\dir
CoTaskMemFree(path);
}
item->lpVtbl->Release(item);
}
}
CoUninitialize();
return 0;
}