Skip to content

Instantly share code, notes, and snippets.

@moehuster
Created November 29, 2019 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moehuster/dd19983514899805c5d1c800d7f48c68 to your computer and use it in GitHub Desktop.
Save moehuster/dd19983514899805c5d1c800d7f48c68 to your computer and use it in GitHub Desktop.
#include <tchar.h>
#include <Windows.h>
#include <tlhelp32.h>
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
bool findProcess(const TCHAR* processName)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcess = INVALID_HANDLE_VALUE;
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
while (Process32Next(hProcessSnap, &pe32)) {
if (!_tcscmp(processName, pe32.szExeFile)) {
CloseHandle(hProcessSnap);
return true;
}
}
CloseHandle(hProcessSnap);
return false;
}
int main(int argc, char* argv[])
{
TCHAR appPath[MAX_PATH];
GetModuleFileName(NULL, appPath, MAX_PATH);
_tcscpy(_tcsrchr(appPath, TEXT('\\')), TEXT("\\mcsca_backend.exe"));
for (;;) {
if (!findProcess(TEXT("mcsca_backend.exe"))) {
// 重启程序
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcess(NULL, appPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
WaitForSingleObject(pi.hProcess, -1);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment