windows下的守护进程C++
生活随笔
收集整理的這篇文章主要介紹了
windows下的守护进程C++
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 守護進程
1.1 需求分析
我有三個程序需要不斷運行,有可能出現某些未知的原因而宕掉,需要本守護程序來進行守護,發現它運行不管,死掉就重啟它,并且服務器開機就啟動。
1.2 使用方法
將該程序與需要守護的程序放在同一目錄。修改以下幾處
- 守護的程序的絕對路徑 twkWindowName_DownloadWget
- 定義守護進程名稱 PROCCESS_NAME_DOWMLOADWGET
- 需要守護的程序所在的路徑 PROGRAMEPATH
開發環境:
- Windows10專業版
- Visual Studio 2017 Enterprise
1.3 實現代碼
/***************************************************************************** Author: 唐維康 Date: 2020年10月05日 Code: UNICODE description: 守護進程:查看下載+轉換+維護三個程序是否因為某種原因死掉,如果死掉就重啟它 開發環境: Visual Studio 2017 Enterprise 和 Windows10專業版使用說明: PROCCESS_NAME_DOWMLOADWGET守護進程名稱根據需要守護的進程修改;twkWindowName_DownloadWget 是需要守護的程序的絕對路徑PROCCESS_NAME_MAINTENANCEPROCEDURES是需要守護的程序所在的路徑 一般情況和本程序在同一目錄 ************************************************************************/#include <stdio.h> #include <stdlib.h> #include <Windows.h> #include <io.h> #include <iostream> #include <tchar.h>//參考網站 //https://blog.csdn.net/jelly_chen_zo/article/details/21546245?utm_medium=distribute.pc_relevant.none-task-blog-title-2&spm=1001.2101.3001.4242 //https://blog.csdn.net/u013408061/article/details/53314327//使用這個 TEXT 宏就可以使得程序都可以正常使用 //TEXT使路徑成為Unicode字符串 必須為LPCTSTR格式,強制類型轉換不行 //下面三個定義為是需要守護的程序的絕對路徑 LPCTSTR twkWindowName_DownloadWget = TEXT("D:\\Desktop\\Monitor_TangWeiKang\\UpgradedMonitoringMachine_TangWeiKang\\RecordingSeparation\\code\\MP3Conversion\\Daemon\\Test01.exe"); LPCTSTR twkWindowName_MP3Conversion = TEXT("D:\\Desktop\\Monitor_TangWeiKang\\UpgradedMonitoringMachine_TangWeiKang\\RecordingSeparation\\code\\MP3Conversion\\Daemon\\Test02.exe"); LPCTSTR twkWindowName_MaintenanceProcedures = TEXT("D:\\Desktop\\Monitor_TangWeiKang\\UpgradedMonitoringMachine_TangWeiKang\\RecordingSeparation\\code\\MP3Conversion\\Daemon\\Test03.exe");//LPCTSTR szAppWindowName = L"D:\\Desktop\\Monitor_TangWeiKang\\UpgradedMonitoringMachine_TangWeiKang\\RecordingSeparation\\code\\MP3Conversion\\Daemon\\Test01.exe"; using namespace std;//隱藏DOS黑窗口 //#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )//定義路徑最大程度 #define MAX_PATH_NUM 4096//定義守護進程名稱 #define PROCCESS_NAME_DOWMLOADWGET "Test01.exe" #define PROCCESS_NAME_MP3CONVERSION "Test02.exe" #define PROCCESS_NAME_MAINTENANCEPROCEDURES "Test03.exe" //PROGRAMEPATH 是需要守護的程序所在的路徑 一般情況和本程序在同一目錄 #define PROGRAMEPATH "D:\\Desktop\\Monitor_TangWeiKang\\UpgradedMonitoringMachine_TangWeiKang\\RecordingSeparation\\code\\MP3Conversion\\Daemon"//定義寫入的注冊表路徑 #define SELFSTART_REGEDIT_PATH "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\"BOOL SetSelfStart();int main() {Notepad獲得守護的進程的句柄HWND handle = FindWindow(NULL, szAppWindowName);HWND handle = FindWindow(NULL, (LPCTSTR)"Notepad");//HWND handle = FindWindow(NULL, szAppWindowName);//if (handle != NULL)//{// /*MessageBox(NULL, TEXT("Application is already running"), szAppClassName, MB_ICONERROR);// ExitProcess(1);*/// cout << "該程序已經在運行了!" << endl;// continue;//}//else//{// cout << "沒有存在" << endl;//}//獲取程序完整名稱char pName[MAX_PATH_NUM] = { 0 };GetModuleFileNameA(NULL, pName, MAX_PATH_NUM);//twk測試/*cout << "***************" << endl;cout << pName << endl;cout << "***************" << endl;*///設置程序開機自啟動if (!SetSelfStart()){cout << "守護進程開機自啟動失敗" << endl;return -1;}else{cout << "守護進程開機自啟動成功" << endl;}STARTUPINFOA si;// 該結構用于指定新進程的主窗口特性//進程對象PROCESS_INFORMATION pi[3];//初始化ZeroMemory(&si, sizeof(si));si.cb = sizeof(si);ZeroMemory(&pi, sizeof(pi));//進程相關信息(句柄,標識)//獲取當前程序的路徑 不用GetCurrentDirectoryA函數獲取是因為開機讀取了其它路徑//開機讀取的路徑是 pPath=C:\Windows\system32\test02.exechar pPath_DownloadWget[MAX_PATH_NUM] = { 0 };//GetCurrentDirectoryA(MAX_PATH_NUM, pPath_DownloadWget);strcat(pPath_DownloadWget, PROGRAMEPATH);char pPath_MP3Conversion[MAX_PATH_NUM] = { 0 };//GetCurrentDirectoryA(MAX_PATH_NUM, pPath_MP3Conversion);strcat(pPath_MP3Conversion, PROGRAMEPATH);char pPath_MaintenanceProcedures[MAX_PATH_NUM] = { 0 };//GetCurrentDirectoryA(MAX_PATH_NUM, pPath_MaintenanceProcedures);strcat(pPath_MaintenanceProcedures, PROGRAMEPATH);//拼接需要守護的程序strcat(pPath_DownloadWget, "\\");strcat(pPath_DownloadWget, PROCCESS_NAME_DOWMLOADWGET);strcat(pPath_MP3Conversion, "\\");strcat(pPath_MP3Conversion, PROCCESS_NAME_MP3CONVERSION);strcat(pPath_MaintenanceProcedures, "\\");strcat(pPath_MaintenanceProcedures, PROCCESS_NAME_MAINTENANCEPROCEDURES);//cout << " pPath_MaintenanceProcedures="<<pPath_MaintenanceProcedures << endl;//構造cmd執行守護進程的字符串char pCmd_DownloadWget[MAX_PATH_NUM] = { 0 };strcat(pCmd_DownloadWget,"cmd /c ");strcat(pCmd_DownloadWget, pPath_DownloadWget);char pCmd_MP3Conversion[MAX_PATH_NUM] = { 0 };strcat(pCmd_MP3Conversion, "cmd /c ");strcat(pCmd_MP3Conversion, pPath_MP3Conversion);char pCmd_MaintenanceProcedures[MAX_PATH_NUM] = { 0 };strcat(pCmd_MaintenanceProcedures, "cmd /c ");strcat(pCmd_MaintenanceProcedures, pPath_MaintenanceProcedures);//twk測試//cout << "pPath_DownloadWget=" << pPath_DownloadWget << endl;//char twkWindowName[MAX_PATH_NUM] = { 0 };//strcat(twkWindowName, pPath_DownloadWget);//LPCTSTR *twkWindowName = (LPCTSTR *)malloc(4096 * sizeof(LPCTSTR));//sprintf(twkWindowName,"%s", (LPCTSTR)pPath_DownloadWget);//cout << "twkWindowName=" << twkWindowName.c_str() << endl;//cout << "szAppWindowName"<<szAppWindowName << endl;//無限循環,監視守護進程do {//檢查守護程序是否存在if ((_access(pPath_DownloadWget, 0) != -1)|| (_access(pPath_MP3Conversion, 0) != -1)|| (_access(pPath_MaintenanceProcedures, 0) != -1)){//獲得守護的進程的句柄//HWND handle = FindWindow(NULL, szAppWindowName);//HWND handle = FindWindow(NULL, (LPCTSTR)"Notepad");HWND handle_DownloadWget = FindWindow(NULL, twkWindowName_DownloadWget);//HWND handle = FindWindow(NULL, (LPCWSTR)pPath_DownloadWget);if (handle_DownloadWget != NULL){/*MessageBox(NULL, TEXT("Application is already running"), szAppClassName, MB_ICONERROR);ExitProcess(1);*/cout << "守護的程序DownloadWget正在運行!" << endl;}else{if (!CreateProcessA(NULL, pCmd_DownloadWget, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[0])){cout << "守護進程DownloadWget啟動失敗,程序即將退出" << endl;return -1;}}HWND handle_MP3Conversion = FindWindow(NULL, twkWindowName_MP3Conversion);//HWND handle = FindWindow(NULL, (LPCWSTR)pPath_DownloadWget);if (handle_MP3Conversion != NULL){/*MessageBox(NULL, TEXT("Application is already running"), szAppClassName, MB_ICONERROR);ExitProcess(1);*/cout << "守護的程序MP3Conversion正在運行!" << endl;}else{if (!CreateProcessA(NULL, pCmd_MP3Conversion, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[0])){cout << "守護進程MP3Conversion啟動失敗,程序即將退出" << endl;return -1;}}HWND handle_MaintenanceProcedures = FindWindow(NULL, twkWindowName_MaintenanceProcedures);//HWND handle = FindWindow(NULL, (LPCWSTR)pPath_DownloadWget);if (handle_MaintenanceProcedures != NULL){/*MessageBox(NULL, TEXT("Application is already running"), szAppClassName, MB_ICONERROR);ExitProcess(1);*/cout << "守護的程序MaintenanceProcedures正在運行!" << endl;}else{if (!CreateProcessA(NULL, pCmd_MaintenanceProcedures, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[0])){cout << "守護進程MaintenanceProcedures啟動失敗,程序即將退出" << endl;//continue;return -1;}}for (int i = 0; i < 3; i++){//啟動成功,獲取進程的IDcout << "守護進程成功,ID:" << pi[i].dwProcessId << endl;//無限等待子進程退出WaitForSingleObject(pi[i].hProcess, INFINITE);cout << "守護進程退出了。。。" << endl;//關閉進程和句柄CloseHandle(pi[i].hProcess);CloseHandle(pi[i].hThread);}}else{cout << "守護程序不存在" << endl;}//睡一下,重啟Sleep(2000);//int a;//a = CreateProcessA(NULL, (LPSTR)"Test01.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[0]);//a = CreateProcessA(NULL, (LPSTR)"Test02.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[1]);//a = CreateProcessA(NULL, (LPSTR)"Test03.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi[2]);// //終止線程for (int i = 0; i < 3; i++){OpenProcess(NULL, true, pi[i].dwProcessId);DWORD fuExitCode; // 進程的退出碼GetExitCodeProcess(pi[i].hProcess, &fuExitCode); // 獲得進程的退出碼TerminateProcess(pi[i].hProcess, fuExitCode); // 終止進程CloseHandle(pi[i].hThread);CloseHandle(pi[i].hProcess);}} while (true);return 0; }//設置本身開機自啟動 BOOL SetSelfStart() {//獲取程序完整名稱char pName[MAX_PATH_NUM] = { 0 };GetModuleFileNameA(NULL, pName, MAX_PATH_NUM);//在注冊表中寫入啟動信息HKEY hKey = NULL;LONG lRet = 0;lRet = RegOpenKeyExA(HKEY_CURRENT_USER, SELFSTART_REGEDIT_PATH, 0, KEY_ALL_ACCESS, &hKey);//判斷是否成功if (lRet != ERROR_SUCCESS){return FALSE;}lRet = RegSetValueExA(hKey, "Daemon", 0, REG_SZ, (const unsigned char*)pName, strlen(pName) + sizeof(char));//判斷是否成功if (lRet != ERROR_SUCCESS){return FALSE;}//關閉注冊表RegCloseKey(hKey);return TRUE; }// 取消開機自動啟動 void cancelAutoStart() {HKEY hKey;string strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//1、找到系統的啟動項 if (RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS){//2、刪除值RegDeleteValue(hKey, (LPCTSTR)"GISRestart");//3、關閉注冊表RegCloseKey(hKey);} }1.4 結果顯示
我用了三個程序來測試,Test01.exe Test02.exe Test03.exe 都是循環打印該進程PID,并且與守護程序放在了同一目錄。
開機啟動測試
界面顯示系統開機程序也跟隨開機。
重啟程序測試
當守護程序宕掉,2秒后重啟。
存在檢查
如果有一個守護的程序已經運行了,就不需要管。
我們手動運行Test01.exe。界面顯示Test01.exe跳過,它已經存在了。
總結
以上是生活随笔為你收集整理的windows下的守护进程C++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派实现AD转换(pcf8591模块)
- 下一篇: windows下程序开机自启动(任务计划