资源简介

功能:对指定的Exe程序进行守护,防止程序因为一些意外情况挂死。程序启动后每隔2s检测指定程序是否处于正常运行状态,如不是,则重启

资源截图

代码片段和文件信息

#include “CheckThread.h“

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

BOOL IsExistProcess(const char*  szProcessName)
{
    PROCESSENTRY32 processEntry32;
    HANDLE toolHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS 0);
    if (((int)toolHelp32Snapshot) != -1)
    {
        processEntry32.dwSize = sizeof(processEntry32);
        if (Process32First(toolHelp32Snapshot &processEntry32))
        {
            do
            {
                int iLen = 2 * wcslen(processEntry32.szExeFile);
                char* chRtn = new char[iLen + 1];
                //转换成功返回为非负值
                wcstombs(chRtn processEntry32.szExeFile iLen + 1);
                if (strcmp(szProcessName chRtn) == 0)
                {
                    return TRUE;
                }
            } while (Process32Next(toolHelp32Snapshot &processEntry32));
        }
        CloseHandle(toolHelp32Snapshot);
    }
    //
    return FALSE;
}

CheckThread::CheckThread(Qobject *parent)
    : QThread(parent)
     _state(true)
{

}

CheckThread::~CheckThread()
{
    quit();
    wait();
}

void CheckThread::runStateChgSlot(bool state)
{
    qDebug() << __FUNCTION__ << “ : state chg state = “ << state;
    _state = state;
}

void CheckThread::run()
{
    QSettings sets(“sys.ini“ QSettings::IniFormat);
    QString exeAbsPath = sets.value(KEY_EXE_PATH).toString();
    QString exeName = exeAbsPath.split(‘/‘).last();
    // 检测程序是否还在运行
     while (1) {
         if (_state && !IsExistProcess(exeName.toStdString().c_str())) {
             QString loginfo;
             loginfo += QDateTime::currentDateTime().toString(“yyyy-MM-dd hh:mm:ss“);
             loginfo += “|“;
             loginfo += exeName;
             loginfo += “|“;
             loginfo += “restart\n“;
             QFile file(“restart_recored.txt“);
             file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
             QTextStream out(&file);
             out << loginfo;
             file.close();
             system(exeAbsPath.toStdString().c_str());
         } else {

         }

         Sleep(2000);
     }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-01-17 09:53  VerSysProtector\
     目录           0  2019-01-17 09:56  VerSysProtector\build-VerSysProtector-msvc-Release\
     文件        1169  2019-01-11 15:34  VerSysProtector\build-VerSysProtector-msvc-Release\.qmake.stash
     目录           0  2019-01-11 15:34  VerSysProtector\build-VerSysProtector-msvc-Release\debug\
     文件       31073  2019-01-17 09:56  VerSysProtector\build-VerSysProtector-msvc-Release\Makefile
     文件       53022  2019-01-17 09:56  VerSysProtector\build-VerSysProtector-msvc-Release\Makefile.Debug
     文件       53028  2019-01-17 09:56  VerSysProtector\build-VerSysProtector-msvc-Release\Makefile.Release
     目录           0  2019-01-17 09:56  VerSysProtector\build-VerSysProtector-msvc-Release\release\
     文件     3466856  2014-03-11 18:54  VerSysProtector\build-VerSysProtector-msvc-Release\release\D3Dcompiler_47.dll
     目录           0  2019-01-16 19:40  VerSysProtector\build-VerSysProtector-msvc-Release\release\Icon\
     目录           0  2019-01-11 15:37  VerSysProtector\build-VerSysProtector-msvc-Release\release\iconengines\
     文件       31232  2017-12-01 00:43  VerSysProtector\build-VerSysProtector-msvc-Release\release\iconengines\qsvgicon.dll
     文件       10654  2019-01-16 19:39  VerSysProtector\build-VerSysProtector-msvc-Release\release\Icon\run.jpg
     文件       14779  2019-01-16 19:39  VerSysProtector\build-VerSysProtector-msvc-Release\release\Icon\stop.jpg
     目录           0  2019-01-11 15:37  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\
     文件       26112  2017-11-30 22:34  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qgif.dll
     文件       33280  2017-12-01 00:34  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qicns.dll
     文件       26624  2017-11-30 22:34  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qico.dll
     文件      297472  2017-11-30 22:35  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qjpeg.dll
     文件       21504  2017-12-01 00:42  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qsvg.dll
     文件       20992  2017-12-01 00:33  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qtga.dll
     文件      327680  2017-12-01 00:34  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qtiff.dll
     文件       19968  2017-12-01 00:34  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qwbmp.dll
     文件      401408  2017-12-01 00:35  VerSysProtector\build-VerSysProtector-msvc-Release\release\imageformats\qwebp.dll
     文件       13312  2017-11-30 22:11  VerSysProtector\build-VerSysProtector-msvc-Release\release\libEGL.dll
     文件     1950720  2017-11-30 22:11  VerSysProtector\build-VerSysProtector-msvc-Release\release\libGLESV2.dll
     文件    15995904  2016-06-14 21:08  VerSysProtector\build-VerSysProtector-msvc-Release\release\opengl32sw.dll
     目录           0  2019-01-11 15:37  VerSysProtector\build-VerSysProtector-msvc-Release\release\platforms\
     文件     1155072  2017-11-30 22:37  VerSysProtector\build-VerSysProtector-msvc-Release\release\platforms\qwindows.dll
     文件     4833792  2019-01-11 15:37  VerSysProtector\build-VerSysProtector-msvc-Release\release\Qt5Core.dll
     文件     5146624  2017-11-30 22:21  VerSysProtector\build-VerSysProtector-msvc-Release\release\Qt5Gui.dll
............此处省略41个文件信息

评论

共有 条评论