Process ID changes when program is visible or minimized

Kevin Duarte

I am trying to get the Process ID of a program (PID) but for some odd reason, the PID changes. When the target program (Alarms & Clock) is visible, it gives me the wrong PID while having the program minimized gives me the right PID.

I am guessing that minimizing the target program suspends it's process, thus allowing it to be read. However, simply reading the PID should not be a restriction even when a process is running.

Does anyone have an idea on what am I doing wrong?

Currently Tried Methods:

  • Ran in Administrative Mode
  • Compiled for 64 bit
  • Compiled for 32 bit

ere is a working, concise piece of code that portrays the problem:

#include <iostream>
#include <Windows.h>
#include <string>

int main()
{
    std::string window_name = "Alarms & Clock"; //Feel free to replace this with another program
    HWND hwnd = FindWindowA(NULL, window_name.c_str());
    if (!hwnd)
    {
        std::cerr << "Error: Could not find window" << std::endl;
        return -1;
    }

    DWORD processID = 0;
    GetWindowThreadProcessId(hwnd, &processID);
    std::cout << "Process ID: " << processID << std::endl;
    std::cin.get();

    return 0;
}
Shadi

I was able to regenerate the problem on my Win/10 with GCC 5.3. I tested it with the "Calculator" app. When the app's window was not minimized I got PID = 14440 which belonged to ApplicationFrameHost.exe, However, I got PID = 1936 correctly when calc's window is minimized.

This is due to the fact that "Calculator" is a tablet app and not a desktop app. Desktop apps give the right PID no matter if the window is minimized or not.

I think this SO post would be useful for you.

It seems that ApplicationFrameHost.exe is an app container that handle many child apps. An extra code is needed to retrieve the exact child app pid you are looking for.

base on that page, I wrote this piece of code and it worked for me, however, you might need to refine it.

typedef struct {
    DWORD ownerpid;
    DWORD childpid;
} windowinfo;

BOOL CALLBACK EnumChildWindowsCallback(HWND hWnd, LPARAM lp) {
    windowinfo* info = (windowinfo*)lp;
    DWORD pid = 0;
    GetWindowThreadProcessId(hWnd, &pid);
    if (pid != info->ownerpid) info->childpid = pid;
    return TRUE;
}

void Show_PID()
{
    Sleep(1000);
    std::string window_name = "Calculator"; 
    HWND hwnd = FindWindowA(NULL, window_name.c_str());
    windowinfo info = { 0 };
    GetWindowThreadProcessId(hwnd, &info.ownerpid);
    info.childpid = info.ownerpid;
    EnumChildWindows(hwnd, EnumChildWindowsCallback, (LPARAM)&info);
    std::cout << "Process ID: " << info.childpid << std::endl;
}

int main()
{
    for (int i = 0; i < 9; ++i)
    {
        Show_PID();
    }

    return 0;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

header overlapping when minimized

分類Dev

WindowStartupLocation ignored when starting minimized

分類Dev

How can I run a program on startup, minimized?

分類Dev

How to find the Process ID (PID) of a running terminal program?

分類Dev

Process id dismiss when ssh connect again

分類Dev

14.04 Desktop Unresponsive When Applications Minimized

分類Dev

Why do my code changes not compile or show up when I run my MonkeyC program from Eclipse?

分類Dev

Certain text on screen is being pushed around when window is resized/minimized

分類Dev

emacs tabbar customisation, making unsaved changes visible

分類Dev

how operating system(linux) kills java program ? What happens to the jvm when I hard kill a java process?

分類Dev

"process launch failed: unknown error" when trying to run Rust program via LLDB

分類Dev

Is there a program that can send me a notification e-mail when a process finishes?

分類Dev

AppWidget ID changes on reboot

分類Dev

Authentication Process is not triggered when using IBM App ID in IBM Cloud Kubernetes Service

分類Dev

Play video on UITableViewCell when it is completely visible

分類Dev

Keyboard is not visible in the second time when reaching

分類Dev

Line in crossplot not visible when using log scale

分類Dev

Disable table column sorting when ($(#addData).is(:"visible"))

分類Dev

directory changes permission when mounted

分類Dev

CSS changes when pushing to Heroku

分類Dev

Collapse Navbar when route changes

分類Dev

change url title if id changes - django

分類Dev

Changing position of data in Input file to a .cpp program Changes output unexpectedly

分類Dev

Close Program when Compiling with gcc

分類Dev

Keep Excel always minimized

分類Dev

Error when starting mariadb - no such process

分類Dev

Error when runing the program, but not when compiling it

分類Dev

How to get process id from child process in node js

分類Dev

How to get process id from SYSTEM_PROCESS_INFORMATION struct?

Related 関連記事

  1. 1

    header overlapping when minimized

  2. 2

    WindowStartupLocation ignored when starting minimized

  3. 3

    How can I run a program on startup, minimized?

  4. 4

    How to find the Process ID (PID) of a running terminal program?

  5. 5

    Process id dismiss when ssh connect again

  6. 6

    14.04 Desktop Unresponsive When Applications Minimized

  7. 7

    Why do my code changes not compile or show up when I run my MonkeyC program from Eclipse?

  8. 8

    Certain text on screen is being pushed around when window is resized/minimized

  9. 9

    emacs tabbar customisation, making unsaved changes visible

  10. 10

    how operating system(linux) kills java program ? What happens to the jvm when I hard kill a java process?

  11. 11

    "process launch failed: unknown error" when trying to run Rust program via LLDB

  12. 12

    Is there a program that can send me a notification e-mail when a process finishes?

  13. 13

    AppWidget ID changes on reboot

  14. 14

    Authentication Process is not triggered when using IBM App ID in IBM Cloud Kubernetes Service

  15. 15

    Play video on UITableViewCell when it is completely visible

  16. 16

    Keyboard is not visible in the second time when reaching

  17. 17

    Line in crossplot not visible when using log scale

  18. 18

    Disable table column sorting when ($(#addData).is(:"visible"))

  19. 19

    directory changes permission when mounted

  20. 20

    CSS changes when pushing to Heroku

  21. 21

    Collapse Navbar when route changes

  22. 22

    change url title if id changes - django

  23. 23

    Changing position of data in Input file to a .cpp program Changes output unexpectedly

  24. 24

    Close Program when Compiling with gcc

  25. 25

    Keep Excel always minimized

  26. 26

    Error when starting mariadb - no such process

  27. 27

    Error when runing the program, but not when compiling it

  28. 28

    How to get process id from child process in node js

  29. 29

    How to get process id from SYSTEM_PROCESS_INFORMATION struct?

ホットタグ

アーカイブ