C++卸載程式功能樣本_C 語言

來源:互聯網
上載者:User

註:在程式退出的時候寫上 自己的卸載代碼。

複製代碼 代碼如下:

// FileName: Uninstall.h
#pragma once

class CUninstall
{
private:

    // Exe檔案名稱
    CString m_strExeName;

    // Bat檔案名稱
    CString m_strBatName;

public:

    // exe的路徑
    CString m_strExePath;

    // bat的路徑
    CString m_strBatPath;

    CString m_unInstallPath;

public:

    // 是否已經複製到臨時檔案夾
    bool GetState (void);

    // 初始化
    void Init (void);

    // 卸載程式
    void Uninstall (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"))
{
    TCHAR strPath[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()
{

}

void CUninstall::Uninstall (void)
{
    // 擷取exe所在路徑
    CString strExePath;        // 臨時問價下exe檔案所在路徑
    HMODULE hModule = NULL;
    TCHAR strPath[MAX_PATH] = {0};
    HKEY hKey;

    ::GetModuleFileName(hModule, strPath, MAX_PATH);
    strExePath = strPath;

    // 拷貝到臨時檔案夾
    CopyFile(strExePath, m_strExePath, FALSE);

    int nIndex = strExePath.ReverseFind(_T('\\'));
    strExePath = strExePath.Left(nIndex);
    m_unInstallPath = strExePath;
    HANDLE hande = CreateFile (m_strBatPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, NULL);
    CloseHandle(hande);

    // 寫卸載批次檔檔案到磁碟
    CString strBuffer;
    strBuffer = _T(":repeat\ndel ");
    strBuffer += _T("\"") + m_strExeName + _T("\"\nif exist ");
    strBuffer += _T("\"") + m_strExeName + _T("\" goto repeat\n");
    strBuffer += _T("rd /s /q \"") + strExePath + _T("\"\n");
    strBuffer += _T("del \"") + m_strBatName + _T("\"");

    CStdioFile file;
    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);
    }
}

bool CUninstall::GetState (void)
{
    if (PathFileExists(m_strBatPath))
    {
        return true;
    }
    else
    {
        return false;
    }
}

void CUninstall::Init (void)
{

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.