简体   繁体   English

Win32进程创建顺序

[英]Win32 process creation order

I would like to enumerate all the instances of an application and determine which instance was created first (oldest). 我想枚举应用程序的所有实例,并确定哪个实例最先创建(最旧)。 Given a list of HWNDs that belong to the process instances, can I sort the list to determine the order of creation? 给定属于流程实例的HWND列表,我可以对列表进行排序以确定创建顺序吗? If not, is there another way? 如果没有,还有其他方法吗?

EDIT 1 : the windows being enumerated are not created by my process, they were created long before my process started execution. 编辑1:正在枚举的窗口不是由我的进程创建的,它们是在我的进程开始执行之前创建的。

EDIT 2: As mentioned in the comments, I am interested in the creation time of the processed. 编辑2:如评论中所述,我对处理的创建时间感兴趣。 I need the HWND of he main window of the oldest instance of the application. 我需要该应用程序最旧实例的主窗口的HWND。 Not sure how to get the HWND from the process ID. 不确定如何从进程ID获取HWND。

There are two different ways you can approach this: 有两种不同的方法可以解决此问题:

  1. Start with the windows 从窗户开始

    • Use EnumWindows() , or a FindWindow/Ex() loop, to find the candidate app windows you are interested in. In the case of EnumWindows() , you can use things like GetClassName() and GetWindowText() in the callback. 使用EnumWindows()FindWindow/Ex()循环查找您感兴趣的候选应用程序窗口。对于EnumWindows() ,您可以在回调中使用GetClassName()GetWindowText()类的东西。

    • Use GetWindowThreadProcessId() to get each window's PID. 使用GetWindowThreadProcessId()获取每个窗口的PID。

    • Use OpenProcess() to open each PID, and GetProcessTimes() to get its creation time. 使用OpenProcess()打开每个PID,然后使用GetProcessTimes()获取其创建时间。

    • Now you can sort the times to get the oldest, and you will know the window(s) that go with the corresponding process. 现在,您可以对时间进行排序以获取最旧的时间,并且您将知道与相应过程相对应的窗口。

  2. Start with the processes 从流程开始

    • Use EnumProcesses() , or a Process32(First|Next)() loop, to find the PIDs of each instance of the app path+filename you are interested in. 使用EnumProcesses()Process32(First|Next)()循环来查找您感兴趣的应用程序路径和文件名的每个实例的PID。

    • Use OpenProcess() and GetProcessTimes() to get their creation times, and then sort them. 使用OpenProcess()GetProcessTimes()来获取它们的创建时间,然后对其进行排序。

    • Then, with the oldest PID, you can enumerate windows looking for the one(s) that belong to that PID. 然后,使用最早的PID,您可以枚举窗口以查找属于该PID的窗口。 You can either: 您可以:

      1. enumerate all windows as above, using GetWindowThreadProcessId() to look for the PID. 使用GetWindowThreadProcessId()枚举上述所有窗口,以查找PID。

      2. use EnumThreadWindows() on each thread of the process. 在进程的每个线程上使用EnumThreadWindows() To get the process's thread IDs, you can use a Thread32(First|Next)() loop. 要获取进程的线程ID,可以使用Thread32(First|Next)()循环。

        Optionally, assuming the process's main thread is the one creating the window(s) you want, you can limit the window enumeration to just that thread. (可选)假设进程的主线程是创建所需窗口的主线程,则可以将窗口枚举限制为仅该线程。 Enumerate the pricess's threads, using OpenThread() and GetThreadTimes() to find the oldest thread ID, which will be the main thread. 枚举价格线程,使用OpenThread()GetThreadTimes()查找最早的线程ID,它将成为主线程。

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

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