简体   繁体   English

是否可以通过进程名称获取WINAPI进程句柄而无需遍历所有进程

[英]Is it possible to get the WINAPI process handle by the process name without iterating through all processes

Good afternoon, Is it possible to get the WINAPI process handle by its name without walking through all the processes? 下午好,是否可以通过名称获取WINAPI进程句柄而无需遍历所有进程? I know how to WINAPI process handle by its name by iterating through all the processes: 我知道如何通过遍历所有进程按名称来处理WINAPI:

     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);      
     if (Process32First(snapshot, &entry) == TRUE){         
        while (Process32Next(snapshot, &entry) == TRUE)         
        {             
            if (stricmp(entry.szExeFile, ProcessName ) == 0){                   
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);                  
                // measure process memory usage                
                CloseHandle(hProcess);             
            }         
        }     
      }      
      CloseHandle(snapshot);   

However, it seems like it would take a significant amount of time to iterate through the process snapshot. 但是,似乎要花费大量时间来遍历流程快照。 Thank you. 谢谢。

Each process has a unique ID but not unique name. 每个进程都有唯一的ID,但没有唯一的名称。 There could be multiple processes with the same name. 可能有多个具有相同名称的进程。 So it is impossible, as it is impossible, for example, to get an entry from std::map by value w/o iterating trough everything. 因此,这是不可能的,例如,不可能通过值从std::map获取条目,而std::map遍历所有内容。 What you can do, however, is to write a function that gives you a list of IDs by name, that will be reusable, but still will have to iterate. 但是,您可以做的是编写一个函数,该函数按名称为您提供ID列表,该函数可以重用,但仍然必须进行迭代。 Why do you worry about performance here? 你为什么担心这里的表现? I believe it is nothing comparing to opening of the handle and process memory measurement. 我相信这与打开手柄和进行过程内存测量相比无济于事。

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

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