首页 > 解决方案 > When looping through all processes, the process name stays the same but the process ID changes

问题描述

So I want to get the list of all processes running on the system (Windows) and print out the process ID with the name in this format:

Process ID - Process Name

The code works and it prints out the process ID and process name but the process name stays the same all the time even though the process ID beside the name is changing, for instance (example output):

Process ID - Process Name
13044           007FF61C
13220           007FF61C
11752           007FF61C

My code which gets the processes and prints the ID and name looks like this:

void misc::get_processes(void) {

    HANDLE process_snapshot;
    PROCESSENTRY32 pe32;

    process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if (process_snapshot == INVALID_HANDLE_VALUE)
        cout << "CreateToolhelp32Snapshot() returned INVALID_HANDLE_VALUE" << endl;

    pe32.dwSize = sizeof(PROCESSENTRY32);

    if (!Process32First(process_snapshot, &pe32))
        cout << "Process32First() returned an error" << endl;

    cout << "Process ID - Process Name" << endl;

    do 
        cout << pe32.th32ProcessID << "\t\t" << pe32.szExeFile << endl;
    while (Process32Next(process_snapshot, &pe32));
}

Also I wanted to include that the process name changes when I restart the program, for instance from:

006FF74C

to:

0019FA6C

标签: c++process

解决方案


推荐阅读