C++卸载程序功能示例
注:在程序退出的时候写上自己的卸载代码。
//FileName:Uninstall.h
#pragmaonce
classCUninstall
{
private:
//Exe文件名
CStringm_strExeName;
//Bat文件名
CStringm_strBatName;
public:
//exe的路径
CStringm_strExePath;
//bat的路径
CStringm_strBatPath;
CStringm_unInstallPath;
public:
//是否已经复制到临时文件夹
boolGetState(void);
//初始化
voidInit(void);
//卸载程序
voidUninstall(void);
public:
CUninstall();
~CUninstall();
};
//FileName:Uninstall.cpp
#include"stdafx.h"
#include"Uninstall.h"
#include<atlconv.h>
#include<locale.h>
CUninstall::CUninstall():m_strExeName(_T("XABC01.exe")),m_strBatName(_T("XABC01.bat"))
{
TCHARstrPath[MAX_PATH]={0};
GetTempPath(MAX_PATH,strPath);
m_strExePath=strPath;
m_strExePath+=m_strExeName;
memset(strPath,0,MAX_PATH);
GetTempPath(MAX_PATH,strPath);
m_strBatPath=strPath;
m_strBatPath+=m_strBatName;
}
CUninstall::~CUninstall()
{
}
voidCUninstall::Uninstall(void)
{
//获取exe所在路径
CStringstrExePath; //临时问价下exe文件所在路径
HMODULEhModule=NULL;
TCHARstrPath[MAX_PATH]={0};
HKEYhKey;
::GetModuleFileName(hModule,strPath,MAX_PATH);
strExePath=strPath;
//拷贝到临时文件夹
CopyFile(strExePath,m_strExePath,FALSE);
intnIndex=strExePath.ReverseFind(_T("\\"));
strExePath=strExePath.Left(nIndex);
m_unInstallPath=strExePath;
HANDLEhande=CreateFile(m_strBatPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL);
CloseHandle(hande);
//写卸载批处理文件文件到磁盘
CStringstrBuffer;
strBuffer=_T(":repeat\ndel");
strBuffer+=_T("\"")+m_strExeName+_T("\"\nifexist");
strBuffer+=_T("\"")+m_strExeName+_T("\"gotorepeat\n");
strBuffer+=_T("rd/s/q\"")+strExePath+_T("\"\n");
strBuffer+=_T("del\"")+m_strBatName+_T("\"");
CStdioFilefile;
if(file.Open(m_strBatPath,CFile::modeWrite))
{
char*old_locale=_strdup(setlocale(LC_CTYPE,NULL));
setlocale(LC_CTYPE,"chs");
file.WriteString(strBuffer);
file.Close();
setlocale(LC_CTYPE,old_locale);//还原语言区域的设置
free(old_locale);//还原区域设定
}
else
{
::MessageBox(NULL,TEXT("文件写入磁盘失败!"),TEXT(""),MB_OK|MB_ICONEXCLAMATION);
}
}
boolCUninstall::GetState(void)
{
if(PathFileExists(m_strBatPath))
{
returntrue;
}
else
{
returnfalse;
}
}
voidCUninstall::Init(void)
{
}
相关文章
- C++学习——c++逗号操作符说明(附加全部运算符优先级)
- EasyC++79,虚函数注意事项
- c++语言截取字符串,详解C++ string常用截取字符串方法
- 10min快速回顾C++语法(二)
- C++语法篇之STL库[通俗易懂]
- c++ auto类型_auto C++
- C++基础入门丨4. 程序结构有哪几种?——程序流程结构
- C++11新特性:迭代数组中的元素
- 【c++】【基础】【primer_plus】【第七章】函数指针
- C++stl库_c++库
- c++整人死机程序
- c++的链表-C++链表
- c++的链表-链表入门(C++)
- c++的链表-C++实现简单链表
- c++ primer读书笔记
- C/C++ 实现内存特征码搜索
- C/C++ 打开外部程序
- 软件开发入门教程网之C++ 信号处理
- C++核心编程笔记合集
- 那位用Rust重写数据库的创始人来复盘了:删除27万行C++代码,值吗?
- 【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )
- C/C++/Java 程序计时功能函数详解编程语言
- C++ vector添加(增加)元素
- C++ deque使用、创建及初始化详解
- C++多文件编程是什么
- GCC分步编译C++程序(汇总版)
- 用C和MySQL让事务安全可靠(c++ mysql 事务)
- MicrosoftVisualC++程序的部署方法
- 解决C++中事件不响应的方法详解
- VC++基于Dx实现的截图程序示例代码
- VC++实现通过API来查看程序错误信息的方法