【发布时间】:2018-05-07 07:36:37
【问题描述】:
我是 C++ 和 Windows api 的新手。我现在拥有的是我只能打印应用程序第一个进程的 PID。如果我要创建 4 个进程,我想获得他们的 PID;在控制台中打印它并在特定时间后终止它们中的每一个(使用计时)。
样品概览:1. for process=1 until process=52. Call notepad.exe3. Get the current PID of this process and print in the console.4. Perform some execution from this processID5. increment process6. Whoever successfully executed, terminate the PID.
这是我目前的代码。
#include <iostream>
#include <string>
#include <tchar.h>
#include <process.h>
#include <windows.h>
#include <tlhelp32.h>
#include <stdlib.h>
using namespace std;
// Forward declarations:
BOOL GetProcessList();
BOOL TerminateMyProcess(DWORD dwProcessId, UINT uExitCode);
int main(void)
{
// Gives info on the thread and process for the new process
PROCESS_INFORMATION pif;
// Defines how to start the program
STARTUPINFO si;
// Zero the STARTUPINFO struct
ZeroMemory(&si, sizeof(si));
// Must set size of structure
si.cb = sizeof(si);
for (int i = 0; i < 3; i++) {
BOOL bRet = CreateProcess(L"C:\\Users\\notepad.exe", // Path to executable file
NULL, // Command string - not needed here
NULL, // Process handle not inherited
NULL, // Thread handle not inherited
FALSE, // No inheritance of handles
0, // No special flags
NULL, // Same environment block as this prog
NULL, // Current directory - no separate path
&si, // Pointer to STARTUPINFO
&pif); // Pointer to PROCESS_INFORMATION
if (FALSE == bRet)
{
MessageBox(HWND_DESKTOP, L"Unable to start program", L"", MB_OK);
return EXIT_FAILURE;
}
}
// Close handle to process
CloseHandle(pif.hProcess);
// Close handle to thread
CloseHandle(pif.hThread);
//return EXIT_SUCCESS;
//system("pause");
GetProcessList();
system("pause");
return 0;
}
BOOL GetProcessList()
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return(FALSE);
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof(PROCESSENTRY32);
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
CloseHandle(hProcessSnap); // clean the snapshot object
return(FALSE);
}
// Now walk the snapshot of processes
do
{
//string str = pe32.szExeFile;
wchar_t *sample = pe32.szExeFile;
wstring ws(sample);
string str(ws.begin(), ws.end());
//cout << "Haha" << endl;
if (str == "notepad.exe") // put the name of your process you want to kill
{
//print the PID
cout << "\n" <<pe32.th32ProcessID << endl;
system("pause");
TerminateMyProcess(pe32.th32ProcessID, 1);
}
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return(TRUE);
}
BOOL TerminateMyProcess(DWORD dwProcessId, UINT uExitCode)
{
DWORD dwDesiredAccess = PROCESS_TERMINATE;
BOOL bInheritHandle = FALSE;
HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
if (hProcess == NULL)
return FALSE;
BOOL result = TerminateProcess(hProcess, uExitCode);
CloseHandle(hProcess);
return result;
}
知道如何让同一应用程序的每个 PID 运行并同时终止它吗?
【问题讨论】:
-
您根本不需要任何进程 ID。成功创建后你得到了进程的句柄。如果您想稍后终止此过程 - 保存此句柄并使用它调用
TerminateProcess。之后关闭句柄 -
您的所有代码 - 完全无效。如果您想对您创建的进程执行某些操作 - 将进程句柄保存在某个对象中。当您需要对进程执行某些操作(查询状态、读取内存、终止等)时,请使用此句柄(对象)。当您不再需要此过程时 - 删除对象并在对象析构函数中关闭进程句柄。您根本不需要的进程ID。任何
CreateToolhelp32Snapshot都不需要。 -
@RbMm 我已经修改了我的概述,请检查。
-
那又怎样?意义和目标不明确。如果您需要对您创建的流程进行处理 - 保存并管理它。全部。如果你需要别的东西 - 你需要问另一个问题
标签: c++ windows winapi process pid