【語言-C++】C++、C 建立掛起程序
阿新 • • 發佈:2019-02-17
#include <windows.h> #include <stdio.h> struct ProcessType { DWORD value[30]; }; void Init(ProcessType &pt) { pt.value[0] = DEBUG_PROCESS; pt.value[1] = DEBUG_ONLY_THIS_PROCESS; pt.value[2] = CREATE_SUSPENDED;//新建程序的主執行緒掛起,呼叫ResumeThread(pis.hThread);啟用新程序的主執行緒 pt.value[3] = DETACHED_PROCESS; pt.value[4] = CREATE_NEW_CONSOLE; pt.value[5] = NORMAL_PRIORITY_CLASS; pt.value[6] = IDLE_PRIORITY_CLASS; pt.value[7] = HIGH_PRIORITY_CLASS; pt.value[8] = REALTIME_PRIORITY_CLASS; pt.value[9] = CREATE_NEW_PROCESS_GROUP; pt.value[10] = CREATE_UNICODE_ENVIRONMENT; pt.value[11] = CREATE_SEPARATE_WOW_VDM; pt.value[12] = CREATE_SHARED_WOW_VDM; pt.value[13] = CREATE_FORCEDOS; pt.value[14] = BELOW_NORMAL_PRIORITY_CLASS; pt.value[15] = ABOVE_NORMAL_PRIORITY_CLASS; pt.value[16] = INHERIT_PARENT_AFFINITY; pt.value[17] = INHERIT_CALLER_PRIORITY; pt.value[18] = CREATE_PROTECTED_PROCESS; pt.value[19] = EXTENDED_STARTUPINFO_PRESENT; pt.value[20] = PROCESS_MODE_BACKGROUND_BEGIN; pt.value[21] = PROCESS_MODE_BACKGROUND_END; pt.value[22] = CREATE_BREAKAWAY_FROM_JOB; pt.value[23] = CREATE_PRESERVE_CODE_AUTHZ_LEVEL; pt.value[24] = CREATE_DEFAULT_ERROR_MODE; pt.value[25] = CREATE_NO_WINDOW; pt.value[26] = PROFILE_USER; pt.value[27] = PROFILE_KERNEL; pt.value[28] = PROFILE_SERVER; pt.value[29] = CREATE_IGNORE_SYSTEM_DEFAULT; } BOOL CreateChildProcess(LPWSTR lpszExecFile,PROCESS_INFORMATION& proinfo,DWORD TYPE) { STARTUPINFO si = {sizeof(si)}; //啟動資訊 PROCESS_INFORMATION pi ; //返回資訊結構體 BOOL bStatus = CreateProcess(lpszExecFile,NULL,NULL,NULL,FALSE,TYPE,NULL,NULL,&si,&pi);//建立程序 if(!bStatus) { return FALSE; } proinfo = pi; return TRUE; } int main(int argc, char const *argv[]) { ProcessType PT; Init(PT); printf("開始建立程序\n"); PROCESS_INFORMATION pis;//用於儲存函式返回值 LPWSTR P(L"C:\\Windows\\system32\\calc.exe"); //建立程序 if(!CreateChildProcess(P,pis,PT.value[2])) { printf("建立程序失敗!\n"); return -1; } printf("建立程序成功!程序控制代碼為:%d\n",pis.hThread); printf("按任意鍵將掛起程序恢復!\n"); system("pause"); //掛起程序恢復 ResumeThread(pis.hThread); printf("演示完畢!\n"); system("pause"); return 0; }