wey97
January 27th, 2004, 06:31 PM
You need a text file named test.txt on your c:\ drive and a shortcut to the text file named test on c:\
(right click test.txt and choose Create Shortcut and rename the shortcut test)
The call to GetPath() gets the correct path from the shortcut to the actual file but GetIconLocation() is empty.
#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <iostream>
using namespace std;
HRESULT GetIconPath(string link)
{
HRESULT hRes;
IShellLink* pISL;
IPersistFile* pIPF;
WCHAR wszText[MAX_PATH];
TCHAR szText[MAX_PATH];
INT nIndex = -1;
INT *pnIndex = &nIndex;
hRes = CoInitialize(NULL);
if(hRes == S_OK)
{
hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&pISL);
if(hRes == S_OK)
{
hRes = pISL->QueryInterface(IID_IPersistFile, (LPVOID*)&pIPF);
if(hRes == S_OK)
{
MultiByteToWideChar(0, 0, link.c_str(), -1, wszText, MAX_PATH);
pIPF->Load(wszText, STGM_READ);
hRes = pISL->GetPath(szText, MAX_PATH, NULL, SLGP_UNCPRIORITY);
if(hRes == S_OK)
{
cout << "Path = " << szText << endl;
}
hRes = pISL->GetIconLocation(szText, MAX_PATH, pnIndex);
if(hRes == S_OK)
{
cout << "Icon Loc = " << szText << " Index = " << nIndex << endl;
}
}
pIPF->Release();
}
pISL->Release();
}
CoUninitialize();
return hRes;
}
int main()
{
GetIconPath("c:\\test.lnk");
system("pause");
}
Any ideas?
(right click test.txt and choose Create Shortcut and rename the shortcut test)
The call to GetPath() gets the correct path from the shortcut to the actual file but GetIconLocation() is empty.
#include <windows.h>
#include <shobjidl.h>
#include <shlguid.h>
#include <iostream>
using namespace std;
HRESULT GetIconPath(string link)
{
HRESULT hRes;
IShellLink* pISL;
IPersistFile* pIPF;
WCHAR wszText[MAX_PATH];
TCHAR szText[MAX_PATH];
INT nIndex = -1;
INT *pnIndex = &nIndex;
hRes = CoInitialize(NULL);
if(hRes == S_OK)
{
hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&pISL);
if(hRes == S_OK)
{
hRes = pISL->QueryInterface(IID_IPersistFile, (LPVOID*)&pIPF);
if(hRes == S_OK)
{
MultiByteToWideChar(0, 0, link.c_str(), -1, wszText, MAX_PATH);
pIPF->Load(wszText, STGM_READ);
hRes = pISL->GetPath(szText, MAX_PATH, NULL, SLGP_UNCPRIORITY);
if(hRes == S_OK)
{
cout << "Path = " << szText << endl;
}
hRes = pISL->GetIconLocation(szText, MAX_PATH, pnIndex);
if(hRes == S_OK)
{
cout << "Icon Loc = " << szText << " Index = " << nIndex << endl;
}
}
pIPF->Release();
}
pISL->Release();
}
CoUninitialize();
return hRes;
}
int main()
{
GetIconPath("c:\\test.lnk");
system("pause");
}
Any ideas?