程式異常退出後自啟

2021-09-21 07:41:25 字數 1900 閱讀 3453

在windows上,許多服務或者展示類的客戶端往往都會附帶開機自啟/程式異常退出後自啟這一功能。功能很簡單,實現起來也不複雜,只需要建立乙個監控程式來一直檢測其是否正在執行,沒執行則啟動它;開機自啟只需將程式路徑新增到登錄檔中即可。

#include "stdafx.h"

#include #include #include #include #include #include #include #include #include #pragma comment(lib,"shlwapi.lib")

using namespace std;

//隱藏dos視窗

#pragma comment(linker,"/subsystem:\"windows\" /entry:\"maincrtstartup\"")

//定義路徑最大程度

#define max_path_num 4096

//定義守護程序名稱

#define proccess_name "test.exe"

//定義寫入的登錄檔路徑

#define selfstart_regedit_path "software\\microsoft\\windows\\currentversion\\run\\"

bool setselfstart()

; 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)

lret = regsetvalueexa(hkey, "testmonitor", 0, reg_sz, (const unsigned char*)pname, strlen(pname) + sizeof(char));

if (lret != error_success)

regclosekey(hkey);

return true;

}int main()

//設定程式開機自啟動

if (!setselfstart())

startupinfoa si;

process_information pi;

zeromemory(&si, sizeof(si));

si.cb = sizeof(si);

zeromemory(&pi, sizeof(pi));

char ppath[max_path_num] = ;

getmodulefilenamea(null, ppath, max_path);

char *ptemp = strrchr(ppath, '\\');

*ptemp = 0x00;

strcat(ppath, "\\");

strcat(ppath, proccess_name);

char pcmd[max_path_num] = ;

strcat(pcmd, ppath);

do cout<<"守護程序成功,id:" << pi.dwprocessid << endl;

waitforsingleobject(pi.hprocess, infinite);

cout << "守護程序退出了。。。" << endl;

closehandle(pi.hprocess);

closehandle(pi.hthread);

} else

sleep(1000);

}while (true);

return 0;

}

讓程式異常退出後自動重啟

程式 freeeim.exe 遇到問題異常退出,是否重啟?類似的情況我們似乎碰見過,很多程式都有這個功能 這是怎麼實現的呢?經 過一番努力,在msdn找到了setunhandledexceptionfilter函式,利用它,可以實現這個功能。其實這個過程叫做seh structured except...

自啟程式為何自啟失敗?

理論上,通過寫登錄檔項,將自啟程式路徑寫入登錄檔run項裡,可以使程式在電腦重啟的時候自動執行。總而言之,其實就是 程式工作目錄的設定問題。自啟程式一般由系統程序啟動,所以自啟程式的工作目錄如果沒有特別設定,將會是系統目錄。如果此時有建立檔案 但是檔案路徑只寫了檔名,沒有包含全路徑 的操作什麼的,就...

Ubuntu 桌面程式開機自啟

只是方法之一,使用.desktop檔案。desktop entry檔案是 linux 桌面系統中用於描述程式啟動配置資訊的檔案,它以.desktop為字尾名,相當於windows系統下的桌面快捷方式。通常乙個二進位制可執行程式是乙個沒有字尾沒有圖示的檔案,不可以隨意移動。以kgt.desktop為例...