zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++卸载程序功能示例

C++程序 功能 示例 卸载
2023-06-13 09:15:12 时间

注:在程序退出的时候写上自己的卸载代码。

复制代码代码如下:


//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)
{

}