進程守護HoldOn

來源:互聯網
上載者:User
// HoldOn.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include <iostream>#include <windows.h> #include <stdio.h> #include <tchar.h> #include "atltime.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){if (argc != 2){printf("please input: holdon xxx.exe");getchar();return 0;}char exefile[32];sprintf(exefile,argv[1]);STARTUPINFO si;PROCESS_INFORMATION pi; //進程資訊: ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); do{// 建立子進程,判斷是否執行成功if(!CreateProcess( NULL,exefile/*"process.exe"*/,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi)){cout << "建立進程失敗.." << GetLastError() << endl;system("pause"); //用於測試return 0; } CTime time = CTime::GetCurrentTime();//進程執行成功,列印進程資訊 cout << "以下是子進程的資訊:" << endl;cout << "建立時間:"<< time.GetMonth()<<"-"<<time.GetDay()<<"  "<<time.GetHour()<<":"<<time.GetMinute()<<":"<<time.GetSecond()<<endl;cout << "進程ID pi.dwProcessID: " << pi.dwProcessId << endl;cout << "線程ID pi.dwThreadID : " << pi.dwThreadId << endl;// 等待知道子進程退出... WaitForSingleObject( pi.hProcess, INFINITE);//檢測進程是否停止 //WaitForSingleObject()函數檢查對象的狀態,如果是未確定的則等待至逾時//子進程退出 cout << "子進程已經退出..." << endl;//關閉進程和控制代碼 CloseHandle(pi.hProcess); CloseHandle(pi.hThread);//system("pause");//執行完畢後等待}while(true);//如果進程推出就再次執行方法 exit(0); return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.