我想获取我在 C++ 中创建的每个进程的 PID,并在特定时间后终止每个进程
I want to get the PID of every process I've created in C++ and terminate each after specific time
我是 C++ 和 Windows api 的新手。我现在所拥有的是我只能打印应用程序第一个进程的 PID。如果我要创建 say 4 进程,我想获得他们的 PID;在控制台中打印它并在特定时间后终止它们中的每一个(使用计时)。
样本概览:
1. for process=1 until process=5
2. Call notepad.exe
3. Get the current PID of this process and print in the console.
4. Perform some execution from this processID
5. increment process
6. 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 运行 并同时终止它吗?
由于 PROCESS_INFORMATION pif
包含 DWORD dwProcessId
中新创建进程的 ID,例如,您可以通过将每个 pif.dwProcessId
添加到 std::vector<DWORD> processIds
来创建您自己的列表。
要终止创建的进程,只需执行类似
的操作
for (auto processId : processIds) {
TerminateMyProcess(processId, 1);
}
编辑:
我想我刚刚使用 #include <algorithm>
的 std::remove_if
和 #include <functional>
的 std::bind
实现了改进。
而不是 for(auto processId : processIds){...}
使用这个:
processIds.erase(std::remove_if(processIds.begin(), processIds.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));
使用 std::remove_if
你可以从容器中删除元素,如果传递的函数(在本例中为 TerminateMyProcess
),它将容器中的元素作为一个参数,returns 是的。
因为 TerminateMyProcess
显然有两个参数,DWORD
和 UINT
,我们可以使用 std::bind 作为 TerminateMyProcess
的包装函数,它需要一个 DWORD
参数 (placeholders::_1) 而 UINT
设置为 1
.
这样,成功终止的 processIds 将从 vector 中删除,只留下无法终止的 processIds。
另一个反映IInspctable's comment的编辑:
在您的应用程序创建了所有这些进程(在您的示例中为 notepad.exe)之后,每个进程都有一个特定的 processId。想象一下,例如,当您的应用程序正在等待终止这些进程的时间时,其中一个进程已经终止。该进程的 ID 现在不再使用,可以由操作系统分配给一个新的完全不同的进程,这与您的应用程序或 notepad.exe 无关。通常您不想终止任何进程。 ;)
创建一个std::vector<HANDLE> processes
。
对于每个创建的进程,将 pif.hProcess
存储在此向量中。此时不要关闭手柄。
将 TerminateMyProcess
更改为它采用 HANDLE
作为第一个参数。
使用
processes.erase(std::remove_if(processes.begin(), processes.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));
终止进程并(如果成功终止)从向量中删除它们的句柄。其余句柄可用于错误处理等
工作示例:
#include <Windows.h>
#include <process.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <chrono>
#include <thread>
std::vector<HANDLE> processes;
void create_process() {
STARTUPINFO si;
SecureZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
SecureZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
if (CreateProcess(L"C:\windows\system32\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hThread);
processes.push_back(pi.hProcess);
}
}
BOOL terminate_process(HANDLE handle, UINT exitCode) {
TerminateProcess(handle, exitCode);
CloseHandle(handle);
return TRUE;
}
// Alternative (simplified), makes `std::bind` unnecessary:
BOOL alt_terminate_process(HANDLE handle) {
TerminateProcess(handle, 1);
CloseHandle(handle);
return TRUE;
}
int main()
{
for (int i = 0; i < 3; ++i) {
create_process();
}
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
// use either
processes.erase(std::remove_if(processes.begin(), processes.end(), std::bind(terminate_process, std::placeholders::_1, 1)));
// or
processes.erase(std::remove_if(processes.begin(), processes.end(), alt_terminate_process));
return 0;
}
我是 C++ 和 Windows api 的新手。我现在所拥有的是我只能打印应用程序第一个进程的 PID。如果我要创建 say 4 进程,我想获得他们的 PID;在控制台中打印它并在特定时间后终止它们中的每一个(使用计时)。
样本概览:
1. for process=1 until process=5
2. Call notepad.exe
3. Get the current PID of this process and print in the console.
4. Perform some execution from this processID
5. increment process
6. 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 运行 并同时终止它吗?
由于 PROCESS_INFORMATION pif
包含 DWORD dwProcessId
中新创建进程的 ID,例如,您可以通过将每个 pif.dwProcessId
添加到 std::vector<DWORD> processIds
来创建您自己的列表。
要终止创建的进程,只需执行类似
的操作for (auto processId : processIds) {
TerminateMyProcess(processId, 1);
}
编辑:
我想我刚刚使用 #include <algorithm>
的 std::remove_if
和 #include <functional>
的 std::bind
实现了改进。
而不是 for(auto processId : processIds){...}
使用这个:
processIds.erase(std::remove_if(processIds.begin(), processIds.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));
使用 std::remove_if
你可以从容器中删除元素,如果传递的函数(在本例中为 TerminateMyProcess
),它将容器中的元素作为一个参数,returns 是的。
因为 TerminateMyProcess
显然有两个参数,DWORD
和 UINT
,我们可以使用 std::bind 作为 TerminateMyProcess
的包装函数,它需要一个 DWORD
参数 (placeholders::_1) 而 UINT
设置为 1
.
这样,成功终止的 processIds 将从 vector 中删除,只留下无法终止的 processIds。
另一个反映IInspctable's comment的编辑:
在您的应用程序创建了所有这些进程(在您的示例中为 notepad.exe)之后,每个进程都有一个特定的 processId。想象一下,例如,当您的应用程序正在等待终止这些进程的时间时,其中一个进程已经终止。该进程的 ID 现在不再使用,可以由操作系统分配给一个新的完全不同的进程,这与您的应用程序或 notepad.exe 无关。通常您不想终止任何进程。 ;)
创建一个std::vector<HANDLE> processes
。
对于每个创建的进程,将 pif.hProcess
存储在此向量中。此时不要关闭手柄。
将 TerminateMyProcess
更改为它采用 HANDLE
作为第一个参数。
使用
processes.erase(std::remove_if(processes.begin(), processes.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));
终止进程并(如果成功终止)从向量中删除它们的句柄。其余句柄可用于错误处理等
工作示例:
#include <Windows.h>
#include <process.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <chrono>
#include <thread>
std::vector<HANDLE> processes;
void create_process() {
STARTUPINFO si;
SecureZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
SecureZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
if (CreateProcess(L"C:\windows\system32\notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
CloseHandle(pi.hThread);
processes.push_back(pi.hProcess);
}
}
BOOL terminate_process(HANDLE handle, UINT exitCode) {
TerminateProcess(handle, exitCode);
CloseHandle(handle);
return TRUE;
}
// Alternative (simplified), makes `std::bind` unnecessary:
BOOL alt_terminate_process(HANDLE handle) {
TerminateProcess(handle, 1);
CloseHandle(handle);
return TRUE;
}
int main()
{
for (int i = 0; i < 3; ++i) {
create_process();
}
std::this_thread::sleep_for(std::chrono::seconds{ 5 });
// use either
processes.erase(std::remove_if(processes.begin(), processes.end(), std::bind(terminate_process, std::placeholders::_1, 1)));
// or
processes.erase(std::remove_if(processes.begin(), processes.end(), alt_terminate_process));
return 0;
}