简体   繁体   English

C ++流程监控(GetExitCodeProcess)

[英]C++ Process Monitoring (GetExitCodeProcess)

I want to monitor a process in c++, so I'm using: 我想监视c ++中的进程,所以我在使用:

std::wstring windowName = TEXT("needle");
HWND windowHandle = FindWindowW(NULL, windowName.c_str());

The FindWindow function, as I understand it, checks the title for all windows (Why did Microsoft name their OS after a core part of it, checking windows in Windows, madness). 据我所知,FindWindow函数会检查所有窗口的标题(为什么Microsoft会以其核心部分的名称来命名其操作系统,因此会疯狂地检查Windows中的窗口)。 If a title matches "needle" then it gives me the... 如果标题匹配“ needle”,那么它给了我...

HWND windowHandle

Next I am using: 接下来我正在使用:

DWORD* PID;
GetWindowThreadProcessId(windowHandle, PID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, *PID);

This gives me the processID or PID as I've named it. 这就是我命名的processID或PID。 I can then use that to... 然后我可以用它来...

HWND p;
DWORD state;
GetExitCodeProcess(p, &state);

... get the state of the process, I'm going to check for it being "STILL_ACTIVE", like so: ...获取进程状态,我将检查它是否为“ STILL_ACTIVE”,如下所示:

        if (state != STILL_ACTIVE) {
        std::cout << "excessive profanity\n";
    }
    else {
        std::cout << "sigh of relief\n";
    }

Except this doesn't work, "cout-ing" (new verb) the value of state gives me some kind of hexadecimal code. 除非这行不通,否则“ cout-ing”(新动词)state的值会给我某种十六进制代码。 It's never "STILL_ACTIVE" despite having multiple windows with "needle" as the title. 尽管有多个以“ needle”为标题的窗口,但它从来都不是“ STILL_ACTIVE”。 The code compiles fine, it's just something to do with conversion, pointers, LPCWSTR's or something I've never come across. 该代码可以很好地编译,这与转换,指针,LPCWSTR或我从未遇到过的事情有关。 Help would be appreciated. 帮助将不胜感激。 Thanks 谢谢

One error (and probably not the only error) is that there is no way this can work correctly: 一个错误(可能不是唯一的错误)是无法正常工作:

DWORD* PID;
GetWindowThreadProcessId(windowHandle, PID);

You are giving GetWindowThreadProcessId an uninitialized pointer, PID . 您正在给GetWindowThreadProcessId一个未初始化的指针PID There is nothing that the function can do with it except dereference it (causing undefined behavior), or at best, check if the value is NULL. 除了取消引用(导致未定义的行为)外,该函数无法执行任何其他操作,或者最多检查该值是否为NULL。

When a function asks for a pointer, it doesn't mean you literally declare a pointer and pass it to the function. 当一个函数要求一个指针时,并不意味着您从字面上声明一个指针并将其传递给该函数。 The function wants the address of an existing entity: 该函数需要一个现有实体的地址

DWORD PID;
GetWindowThreadProcessId(windowHandle, &PID);

You have two problems: 您有两个问题:

1) As PaulMcKenzie points out in his answer, PID points to nothing, and will cause problems. 1)正如PaulMcKenzie在其答案中指出的那样, PID指向无内容,并且会引起问题。 Instead you should declare a DWORD and pass a pointer to it to GetWindowThreadProcessId : 相反,您应该声明一个DWORD并将其指向的指针传递给GetWindowThreadProcessId

DWORD PID;
// note: &PID instead of just PID
GetWindowThreadProcessId(windowHandle, &PID);
// note: Just PID instead of *PID
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);

2) GetExitCodeProcess needs a handle to a process, not an uninitialized HWND . 2) GetExitCodeProcess需要处理的句柄,而不是未初始化的HWND Instead you should give it the handle returned from OpenProcess : 相反,您应该给它从OpenProcess返回的句柄:

DWORD state;
// note: this is the hProcess returned from OpenProcess
GetExitCodeProcess(hProcess, &state);

Note that this will still only work for one process. 请注意,这仍然仅适用于一个进程。 If multiple processes have windows with the title "needle" then the result of your FindWindow call will be unpredictable. 如果多个进程具有标题为“ needle”的窗口,则FindWindow调用的结果将不可预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM