开发安装程序
2006-11-28 11:20
113 查看
一实例
1.1 安装程序
1.2 卸载程序
[align=left]二思路[/align]
[align=left]2.1 安装程序[/align]
[align=left]1 如何将目的程序附加在安装程序里?一般安装程序只有一个可执行文件,因而目的程序也放在安装程序里面,通过将目标程序放在资源文件,可以使目标程序像资源一样使用。目标程序在资源的类型是#define RC_BINARYTYPE 256,格式如下:[/align]
[align=left]//#resource.h[/align]
[align=left]#define RC_BINARYTYPE 256[/align]
[align=left]#define ID_MAIN 3000 //ID_MAIN表示主程序的资源ID[/align]
[align=left]//xxx.rc[/align]
[align=left]#include “resource.h”[/align]
[align=left]ID_MAIN RC_BINARYTYPE “XXX.exe” //目标程序[/align]
[align=left]2 如何将资源里的文件解压到目标路径?①用FindResource,LoadResource,LockResource等将资源加载到缓冲区,②然后用CreateFile,WriteFile,CloseFile等将缓冲区的数据写到目标路径。[/align]
[align=left]3 如何对用COMPRESS压缩的文件进行解压?用LZOpenFile,LZCopy,LZClose等解压。[/align]
[align=left]4 如何创建开始菜单和桌面的快捷方式?用SHGetSpecialFolderPath(NULL,StartMenuPath,CSIDL_PROGRAMS,0);获取开始菜单路径,用SHGetSpecialFolderPath(NULL,StartDeskPath,CSIDL_DESKTOP,0);获取桌面路径。用IshellLink创建快捷方式[/align]
2.2 卸载程序
1 删除已安装的所有文件。DeleteFile
2 删除快捷方式。DeleteFile
3 删除卸载程序本身。
参考文献:原著:Alex Tilles 翻译:NorthTibet <<用自删除DLL实现应用程序的安装/卸载代码>>这篇文章是安装程序的核心思想,而且有代码,对开发安装程序非常有帮助。
三代码
/*名称:WriteResourceToFile
功能:将资源中指定的文件写到filename
参数:hInstance句柄,idResource资源ID,filename生成的文件
*/
void WriteResourceToFile(HINSTANCE hInstance,int idResource,char const *filename)
{
HRSRC hResInfo=FindResource(hInstance,MAKEINTRESOURCE(idResource),MAKEINTRESOURCE(RC_BINARYTYPE));
HGLOBAL hgRes=LoadResource(hInstance,hResInfo);
void *pvRes=LockResource(hgRes);
DWORD cbRes=SizeofResource(hInstance,hResInfo);
HANDLE hFile=CreateFile(filename,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
DWORD cbWritten;
WriteFile(hFile,pvRes,cbRes,&cbWritten,0);
CloseHandle(hFile);
}
/*名称:DecompressFile
功能:对COMPRESS压缩文件进行解压
参数:source压缩的文件,解压成的文件
*/
void DecompressFile(char const *source,char const *dest)
{
OFSTRUCT ofs;
ofs.cBytes=sizeof(ofs);
int zhfSource=LZOpenFile(const_cast<char*>(source),&ofs,OF_READ);
int zhfDest=LZOpenFile(const_cast<char*>(dest),&ofs,OF_CREATE|OF_WRITE);
LZCopy(zhfSource,zhfDest);
LZClose(zhfSource);
LZClose(zhfDest);
}
#include <shlobj.h>
#pragma comment(lib,"shell32.lib")
/*名称:CreateProgFolder
功能:创建开始菜单程序组
参数:程序组名称
*/
int CreateProgFolder(LPSTR folder)
{
char foldbuff[MAX_PATH];
SHGetSpecialFolderPath(NULL,foldbuff,CSIDL_PROGRAMS,0); //创建开始
sprintf(foldbuff,"%s//%s",foldbuff,folder);
CreateDirectory(foldbuff,NULL);
return 1;
}
/*名称:CreateProgLink
功能:创建开始菜单的快捷方式
参数:lpszSrcPath源文件路径,linkname快捷方式名称,lpszWorkingPath工作目录,Desc注释
*/
int CreateProgLink(LPSTR lpszSrcPath,LPSTR linkname,LPSTR lpszWorkingPath,LPSTR Desc)
{
char StartMenuPath[MAX_PATH];
SHGetSpecialFolderPath(NULL,StartMenuPath,CSIDL_PROGRAMS,0); //创建开始
sprintf(StartMenuPath,"%s//%s",StartMenuPath,linkname);
CreateLink(lpszSrcPath,StartMenuPath,lpszWorkingPath,Desc);
return 1;
}
/*名称:CreateDeskLink
功能:创建桌面的快捷方式
参数:lpszSrcPath源文件路径,linkname快捷方式名称,lpszWorkingPath工作目录,Desc注释
*/
int CreateDeskLink(LPSTR lpszSrcPath,LPSTR linkname,LPSTR lpszWorkingPath,LPSTR Desc)
{
char StartDeskPath[MAX_PATH];
SHGetSpecialFolderPath(NULL,StartDeskPath,CSIDL_DESKTOP,0); //创建开始
sprintf(StartDeskPath,"%s//%s",StartDeskPath,linkname);
CreateLink(lpszSrcPath,StartDeskPath,lpszWorkingPath,Desc);
return 1;
}
/*名称:CreateLink
功能:创建快捷方式
参数:lpszSrcPath源文件路径,lpszPathLink快捷方式路径,lpszWorkingPath工作目录,Desc注释
*/
HRESULT CreateLink(LPCSTR lpszSrcPath,LPSTR lpszPathLink,LPSTR lpszWorkingPath,LPSTR lpszDesc)
{
HRESULT hres ;
IShellLink * psl ;
IPersistFile * ppf ;
WORD wsz[ MAX_PATH] ;
CoInitialize(NULL);
hres = CoCreateInstance( CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
(void **)&psl) ;
if( FAILED( hres))
return FALSE ;
//设置目标应用程序
psl -> SetPath( lpszSrcPath) ;
psl -> SetDescription(lpszDesc);
psl -> SetWorkingDirectory(lpszWorkingPath);
//从IShellLink获取其IPersistFile接口
//用于保存快捷方式的数据文件 (*.lnk)
hres = psl -> QueryInterface( IID_IPersistFile, (void**)&ppf);
if( FAILED( hres))
{
psl -> Release( ) ;
return FALSE ;
}
// 确保数据文件名为ANSI格式
MultiByteToWideChar( CP_ACP, 0, lpszPathLink, -1,wsz, MAX_PATH) ;
//调用IPersistFile::Save
//保存快捷方式的数据文件 (*.lnk)
hres = ppf -> Save( wsz, STGM_READWRITE) ;
//释放IPersistFile和IShellLink接口
ppf -> Release( ) ;
psl -> Release( ) ;
//记录
WriteFile(hFileLog,lpszPathLink,strlen(lpszPathLink),&bNum,NULL);
WriteFile(hFileLog,"/r/n",2,&bNum,0);
return 1;
}
卸载程序自我删除,需用到一个动态链库,具体应用参考“参考文献”
void SelfDelete(HINSTANCE hInstance)
{
WriteResourceToFile(hInstance,ID_DLL,"instdll.dll");
//生成命令行
//查找rudll32.exe
char commandLine[MAX_PATH*3];
GetWindowsDirectory(commandLine,sizeof(commandLine));
lstrcat(commandLine,"//rundll32.exe");
if(GetFileAttributes(commandLine)==0xFFFFFFFF)
{
GetSystemDirectory(commandLine,sizeof(commandLine));
lstrcat(commandLine,"//rundll32.exe");
}
//添加rundll32.exe参数
lstrcat(commandLine," instdll.dll,_MagicDel@16 ");
char thisName[MAX_PATH];
GetModuleFileName(hInstance,thisName,sizeof(thisName));
lstrcat(commandLine,thisName);
PROCESS_INFORMATION procInfo;
STARTUPINFO startInfo;
memset(&startInfo,0,sizeof(startInfo));
startInfo.dwFlags=STARTF_FORCEOFFFEEDBACK;
CreateProcess(0,commandLine,0,0,FALSE,NORMAL_PRIORITY_CLASS,0,0,&startInfo,&procInfo);
}
相关文章推荐
- Linux系统下安装Bugzilla来追踪程序开发中的bug
- linux上安装eclipse并配置mapreduce程序开发环境
- 开发和部署Pocket Pc 安装程序
- Eclipse开发J2ME程序 第三讲 J2ME插件EclipseMe的安装
- Android开发过程遇到的安装好的APP打开程序崩溃,或者安装后应用列表里没有的问题及解决方案
- 安装 vs2008 feature pack 后,所开发的程序菜单字体丑
- Android开发片段–命令行安装,卸载,启动,程序(AM,PM)
- Android开发基础——真机测试错误,ADB启动不了,程序安装不了,the connection to adb is down,INSTALL_CANCELED_BY_USER...
- 关于如何解锁你的WP7,以便安装自己开发的程序。
- 我们开发的安装程序
- 安装Eclipse开发Mapreduce作业程序
- 使用VS2010开发服务端程序时每次安装服务名都是service1
- redhat enterprise x64安装android 开发环境时 启动eclipse时 找不到adb程序
- Windows服务开发之安装程序的设置
- MyEclipse开发手机程序-安装开发环境(图解)
- (win7)SQL SERVER 2005开发版 安装错误:无法找到产品Microsoft SQL Server Native Client的安装程序包,尝试再次使用的安装包 ' Sqlncli.ms
- [web开发] 如何在Mac OS上安装Apache Tomcat并运行你的第一个Servlet程序
- 微信·小程序开发工具安装指南及注意事项
- s3c2410上搭建QT/Embedded4.8.5开发环境(五)--程序安装后的打包,以及环境变量的设置
- 不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“N