jjzjj

c++ - 我想获取我在 C++ 中创建的每个进程的 PID,并在特定时间后终止每个进程

coder 2024-06-18 原文

我是 C++ 和 Windows api 的新手。我现在所拥有的是我只能打印应用程序第一个进程的 PID。如果我要创建 say 4 进程,我想获得他们的 PID;在控制台中打印它并在特定时间后终止它们中的每一个(使用计时)。

示例概述:
1。对于 process=1 直到 process=5
2。调用notepad.exe
3。获取当前进程的PID,并在控制台打印。
4。从此 processID 执行一些操作
5。增量过程
6。谁执行成功,谁就终止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 pifDWORD dwProcessId 中包含新创建进程的 ID ,例如,您可以通过添加每个 pif.dwProcessId 来创建自己的列表到 std::vector<DWORD> processIds .

要终止创建的进程,只需执行类似的操作

for (auto processId : processIds) {
    TerminateMyProcess(processId, 1);
}

编辑:

我想我刚刚使用 std::remove_if 实现了改进来自 #include <algorithm>std::bind来自 #include <functional> .

而不是 for(auto processId : processIds){...}使用这个:

processIds.erase(std::remove_if(processIds.begin(), processIds.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));

std::remove_if您可以从容器中删除元素,如果传递的函数(在本例中为 TerminateMyProcess )将容器中的元素作为一个参数返回 true。

作为TerminateMyProcess显然有两个参数,DWORDUINT , 我们可以使用 std::bind 作为 TerminateMyProcess 的包装函数需要一个DWORD参数 (placeholders::_1) 而 UINT设置为 1 .

这样,成功终止的 processIds 将从 vector 中移除,只留下无法终止的 processIds。


另一个反射(reflect) IInspctable's comment 的编辑:

在您的应用程序创建了所有这些进程(在您的示例中为 notepad.exe)之后,每个进程都有一个特定的 processId。想象一下,例如,当您的应用程序正在等待终止这些进程的时间时,其中一个进程已经终止。该进程的 ID 现在不再使用,可以由操作系统分配给一个新的完全不同的进程,与您的应用程序或 notepad.exe 无关。通常您不想终止任何进程。 ;)

创建一个 std::vector<HANDLE> processes .
对于每个创建的进程,存储 pif.hProcess在这个 vector 中。此时不要关闭 handle 。 更改 TerminateMyProcess它需要 HANDLE因为它是第一个参数。
使用

processes.erase(std::remove_if(processes.begin(), processes.end(), std::bind(TerminateMyProcess, std::placeholders::_1, 1)));

终止进程并(如果成功终止)从 vector 中删除它们的句柄。其余句柄可用于错误处理等。


工作示例:

#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++ - 我想获取我在 C++ 中创建的每个进程的 PID,并在特定时间后终止每个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50209226/

有关c++ - 我想获取我在 C++ 中创建的每个进程的 PID,并在特定时间后终止每个进程的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  3. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  4. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  5. ruby - 如何在 Ruby 中创建无类 DSL? - 2

    我正在尝试找出如何为我的Ruby项目创建一种“无类DSL”,类似于在Cucumber步骤定义文件中定义步骤定义或在Sinatra应用程序中定义路由。例如,我想要一个文件,其中调用了我的所有DSL函数:#sample.rbwhen_string_matches/hello(.+)/do|name|call_another_method(name)end我认为用我的项目特有的一堆方法污染全局(内核)命名空间是一种不好的做法。因此方法when_string_matches和call_another_method将在我的库中定义,并且sample.rb文件将以某种方式在我的DSL方法的上下文中

  6. ruby-on-rails - 如何在 Rails 3 中创建自定义脚手架生成器? - 2

    有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我

  7. ruby - 为什么在 ruby​​ 中创建 Rational 不需要新方法 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?

  8. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  9. ruby - 在 Ruby 中创建按公共(public)键值分组的新哈希 - 2

    假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解

  10. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

随机推荐