用BCB做完了安装/卸载程序,把几个我觉得还可以的函数源码公布,希望对大家有点用
在做安装/卸载程序的过程中,我写了几个函数,把比较难用的WIN32 API封装了一
下,希望能对大家有点用吧!
1、快捷方式生成函数
所需头文件: #include <shlobj.h>
函数声明: void __fastcall CreateShortCut( AnsiString SetupPath,
AnsiString StartFolderName,
AnsiString ShortCutName,
AnsiString ExecuteName,
bool IsDesktop=true,
bool IsStart=true,
bool IsAutoRun=false );
函数实现:
//创建快捷方式
void __fastcall TfrmBeginSetup::CreateShortCut( AnsiString SetupPath,
AnsiString StartFolderName,
AnsiString ShortCutName,
AnsiString ExecuteName,
bool IsDesktop, bool IsStart,
bool IsAutoRun )
{
IShellLink* pLink;
IPersistFile* pPersistFile;
LPMALLOC ShellMalloc;
LPITEMIDLIST DesktopPidl;
char DesktopDir[MAX_PATH];
AnsiString Name=ShortCutName, ExeName=ExecuteName, StartName="",
SetupDir=SetupPath;
StartName = StartFolderName;
if( SetupDir.SubString( (SetupDir.Length()-1),1 ) != "\\" )
SetupDir += "\\";
if( IsDesktop == true )
{
//创建桌面快捷方式
if(FAILED(SHGetMalloc(&ShellMalloc)))
return;
if(FAILED(SHGetSpecialFolderLocation(NULL,
CSIDL_DESKTOPDIRECTORY,
&DesktopPidl)))
return;
if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return;
}
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetShowCmd(SW_SHOW);
pLink->SetDescription( Name.c_str() );
AnsiString newpath = SetupDir + ExeName;
pLink->SetPath(newpath.c_str());
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **) &pPersistFile)))
{
WideString strShortCutLocation(DesktopDir);
strShortCutLocation += "\\" + Name + ".lnk";
Desktop = strShortCutLocation;
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}
CoUninitialize();
}
} //end IsDesktop if
if( IsStart == true )
{
//在程序组中创建快捷方式
if(FAILED(SHGetMalloc(&ShellMalloc)))
return;
if(FAILED(SHGetSpecialFolderLocation(NULL,
CSIDL_PROGRAMS,
&DesktopPidl)))
return;
if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return;
}
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetShowCmd(SW_SHOW);
pLink->SetDescription( Name.c_str() );
AnsiString newpath = SetupDir + ExeName;
pLink->SetPath(newpath.c_str());
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **) &pPersistFile)))
{
WideString strShortCutLocation(DesktopDir);
Start = strShortCutLocation;
//如果StartFolderName != NULL,则在程序组下创建子菜单
if( StartName != "" )
{
if( StartName.SubString(0,1) != "\\" )
strShortCutLocation += "\\";
strShortCutLocation += StartName;
ForceDirectories( strShortCutLocation );
}
strShortCutLocation += "\\" + Name +".lnk";
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
}
pLink->Release();
CoUninitialize();
}
} //end IsStart if
if( IsAutoRun == true )
{
//程序自动启动
if(FAILED(SHGetMalloc(&ShellMalloc)))
return;
if(FAILED(SHGetSpecialFolderLocation(NULL,
CSIDL_STARTUP,
&DesktopPidl)))
return;
if(!SHGetPathFromIDList(DesktopPidl, DesktopDir))
{
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
return;
}
ShellMalloc->Free(DesktopPidl);
ShellMalloc->Release();
if(SUCCEEDED(CoInitialize(NULL)))
{
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **) &pLink)))
{
pLink->SetShowCmd(SW_SHOW);
pLink->SetDescription( Name.c_str() );
AnsiString newpath = SetupDir + ExeName;
pLink->SetPath(newpath.c_str());
if(SUCCEEDED(pLink->QueryInterface(IID_IPersistFile,
(void **) &pPersistFile)))
{
WideString strShortCutLocation(DesktopDir);
strShortCutLocation += "\\" + Name +".lnk";
pPersistFile->Save(strShortCutLocation.c_bstr(), TRUE);
pPersistFile->Release();
}
pLink->Release();
}
CoUninitialize();
}
} //end IsAutoRun if
}
说明一下,第一个参数是安装文件的所在目录;第二个是创建“开始-程序”快捷方
式的文件目录,也可以设置为NULL,则不创建目录;第三个是快捷方式的名称;第
四个是和快捷方式相关联的执行文件;后面三个预设了值的BOOL参数,分别表示
是否需要创建桌面、开始-程序组快捷,以及是否自动启动。