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:若有不对,敬请指出,谢谢~安全