我现在写了一个程序,不用InstallShield打包,直接用WINRAR制作成自解压包,解压自运行.这样没有创建快捷方式,可是客户要求创建快捷方式,不知道怎么样在程序中创建快捷方式?
bool CreateShortcut(LPCSTR ExeName, LPCSTR CmdLine,
LPCSTR Desc)
{
int len;
char *Ext;
const char *Name;
char buf[MAX_PATH];
HRESULT hres;
IShellLink* psl;
LPITEMIDLIST ppid;
//下面的代码,取得开始菜单目录,以及生成开始菜单下
//的快捷方式的文件名
memset(buf, 0, sizeof(buf));
Name = ExeName ? strrchr(ExeName,\\) : NULL;
if (Name==NULL) return false;
SHGetSpecialFolderLocation(0, CSIDL_STARTMENU, &ppid);
SHGetPathFromIDList(ppid, buf); len = strlen(buf);
if (len>0 && buf[len-1]==\\) len -= 1;
strcpy(buf+len, Name); Ext = strrchr(buf+len,.);
strcpy(Ext?Ext:buf+strlen(buf), ".lnk");
//下面的代码,生成快捷方式
if (CoInitialize(NULL)!=S_OK) return false;
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
psl->SetPath(ExeName);
psl->SetDescription(Desc);
if (CmdLine && CmdLine[0]!=\0)
psl->SetArguments(CmdLine);
hres = psl->QueryInterface(IID_IPersistFile,
(void**)&ppf);
if (SUCCEEDED(hres)) {
wchar_t wsz[MAX_PATH];
// Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, buf,
-1, wsz, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
CoUninitialize(); return hres==S_OK;
}
#define NO_WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <shlobj.h>
#include <vcl.h>
将它们加到最前面。
WINRAR 也可以指定创建快捷方式的,在配置生成 自解压 的压缩包时。