Windows系統中,進程是一個很重要的部分,進程與進程內核對象對應,同時也擁有許多屬性,咱們必須一一地去熟悉他們,從而來感知系統。windows
//設置當前進程優先級爲Real-time if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) cout << "Failed to set priority of current process!" << endl; DWORD parent_exp_PID = -1; string parent_name = ""; HANDLE snap_handle = NULL; snap_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL); if (snap_handle == INVALID_HANDLE_VALUE) cout << "Create process snapshot unsuccessfully!" << "\t Error Code: " << GetLastError() << endl; PROCESSENTRY32 process_info = { 0 }; MODULEENTRY32 module_info = { 0 }; process_info.dwSize = sizeof(PROCESSENTRY32); module_info.dwSize = sizeof(MODULEENTRY32); cout << "Process Name" << "\t\t" << "Process ID" << "Parent PID" << endl; if (Process32First(snap_handle, &process_info)) { //經過進程快照句柄,遍歷枚舉進程 do { cout << process_info.szExeFile << "\t\t" << process_info.th32ProcessID << "\t" << process_info.th32ParentProcessID << endl; //經過進程句柄,遍歷枚舉每一個進程有關的模塊 //當調用進程是一個32-bit程序,而快照進程是一個64-bit程序時候,CreateToolHelp32Snashot將會失敗,GetLastError獲得ERROR_PARTIAL_COPY (299) HANDLE mod_snap_handle = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, process_info.th32ProcessID); if (INVALID_HANDLE_VALUE==mod_snap_handle) { DWORD error_code = GetLastError(); continue; } if (Module32First(mod_snap_handle, &module_info)) { do { cout <<"\t"<< module_info.szModule << endl; } while (Module32Next(mod_snap_handle,&module_info)); } CloseHandle(mod_snap_handle); if (0==strcmp(process_info.szExeFile,TEXT("explorer.exe"))) parent_exp_PID = process_info.th32ParentProcessID; if (parent_exp_PID != -1 && process_info.th32ProcessID == parent_exp_PID) parent_name = process_info.szExeFile; } while (Process32Next(snap_handle, &process_info)); CloseHandle(snap_handle); } else cout << "Failed to get process information in the beginning." << "\t Error Code: " << GetLastError() << endl; cout << "Explorer's father process: " << parent_name << "\t" << "PID: " << parent_exp_PID << endl;
string file_name = TEXT("Fucker.txt"); HANDLE output_file_handle=NULL, input_file_handle=NULL; SECURITY_ATTRIBUTES handle_sec_attributes = { { 0 } }; handle_sec_attributes.nLength = sizeof(SECURITY_ATTRIBUTES); handle_sec_attributes.bInheritHandle = TRUE; //指定這個輸出文件句柄是可繼承的 handle_sec_attributes.lpSecurityDescriptor = NULL; output_file_handle = CreateFile(file_name.c_str(), GENERIC_WRITE, FILE_SHARE_READ, &handle_sec_attributes, OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE == output_file_handle) { cout << "Failed to create file !" << endl; return -1; } TCHAR exe_name[] = TEXT("4s.exe"); STARTUPINFO start_info = { 0 }; start_info.cb = sizeof(STARTUPINFO); start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); start_info.hStdOutput = output_file_handle; start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); start_info.dwFlags = STARTF_USESTDHANDLES; PROCESS_INFORMATION process_info = { 0 }; //isInheritable要設置成TRUE,這樣子進程才能繼承到I/O文件句柄,進而使用它們 if (!CreateProcess(exe_name, NULL, NULL, NULL, TRUE, REALTIME_PRIORITY_CLASS|CREATE_NEW_CONSOLE, NULL, NULL, &start_info, &process_info)) cout << "Failed to create process !" << endl; CloseHandle(output_file_handle); CloseHandle(process_info.hThread); CloseHandle(process_info.hProcess);
PS:若有不對,敬請指出,謝謝~安全