使用com接口可以创建一个应用程序的快捷方式文件在任意目录。
#include <Shlobj.h>
#include <objbase.h>
void CreateLnkFile(char *strAppFile,char *strDstFile)
{
::CoInitialize(NULL);
IShellLink *psl;
HRESULT hres;
WIN32_FIND_DATA wfd;
char szSetPath[MAX_PATH]={0};
// Get pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID *)&psl);
if (SUCCEEDED(hres))
{
// Get pointer to the IPersistFile interface.
//设置文件路径
//GetModuleFileName(NULL,szSetPath,sizeof(szSetPath));
strcpy(szSetPath,strAppFile);
psl->SetPath(szSetPath);
//设置文件绝对路径
(strrchr(szSetPath,'\\'))[1] = 0;
psl->SetRelativePath(szSetPath,0);
psl->SetWorkingDirectory(szSetPath);
//设置参数
//psl->SetArguments("www.sohu.com");
IPersistFile *ppf;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf);
if(SUCCEEDED(hres))
{
WORD wsz[MAX_PATH];
//保存为目标文件
MultiByteToWideChar(CP_ACP, 0, strDstFile, -1, wsz, MAX_PATH);
hres=ppf->Save(wsz, STGM_READWRITE);
ppf->Release();
}
psl->Release();
}
::CoUninitialize();
}
使用:
Set.CreateLnkFile("D:\\testapp.exe","C:\\Documents and Settings\\Administrator\\桌面\\testapp.lnk");