快捷方式 属于 平台SDK 中 Windows Shell 的内容。用到了COM。相当麻烦。
我替你查了一下MSDN。
首先因为用到了COM,
你要在你程序启动的时候 调用 CoInitilize(NULL);,在程序退出时调用CoUninitialize();
然后是准备操作,找到你要的快捷方式的路径。用FindFirstFile 和 FindNextFile 找。如果是MFC,可以用CFileFind比较方便。当然,你也可以在代码中给出快捷方式的路径,但这样会使程序不通用。
需要的头文件:
需要链接的IDL文件:
之后是查找快捷方式的目标。MSDN 里有这段代码。就用MS 的吧。是一个函数的代码。我给你粘在这个帖子的最后面。你可以把它拷进你代码里。
最后当然是运行.exe 文件了。 用CreateProcess 或 WinExec 都行。
那个函数的代码:
HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath, int iPathBufferSize)
{
HRESULT hres;
IShellLink* psl;
char szGotPath[MAX_PATH];
char szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;
*lpszPath = 0; // assume failure
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);
// TODO: Check return value from MultiByteWideChar to ensure
success.
// Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);
if (SUCCEEDED(hres))
{
// Resolve the link.
hres = psl->Resolve(hwnd, 0);
if (SUCCEEDED(hres))
{
// Get the path to the link target.
hres = psl->GetPath(szGotPath,
MAX_PATH,
(WIN32_FIND_DATA*)&wfd,
SLGP_SHORTPATH);
if (SUCCEEDED(hres))
{
// Get the description of the target.
hres = psl->GetDescription(szDescription, MAX_PATH);
if (SUCCEEDED(hres))
{
hres = StringCbCopy(lpszPath, iPathBufferSize,
szGotPath);
if (SUCCEEDED(hres))
{
// Handle success
}
else
// application-defined function
HandleErr(hres);
}
}
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
return hres;
}