// 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;}