How to get the process ID's of all running programs (Views: 710)
Problem/Question/Abstract: I am trying to build a component to get info about the programs executing in my NT4 environment. I managed to get the names for all the windows running (title text). Now, how can I get the process ID for each of them? Which function should I use? Is there any function that tells me for how long my process is running? Answer: It is better to use the EnumWindows function instead of FindWindow/GetWindow, because the latter isn't sensitive to windows being destroyed halfway through the loop and you can end up getting invalid handles. Here's an example: function EnumWindowsProc(hWnd: LongWord; Form: TForm1): WordBool; stdcall; begin Form.FoundAWindow(hWnd); Result := True end; procedure TForm1.FoundAWindow(hWnd: LongWord); var ProcessID: LongWord; Title: array[0..255] of Char; begin GetWindowThreadProcessID(hWnd, @ProcessID); GetWindowText(hWnd, Title, 256); {Now, do whatever you want with Title and ProcessID, e.g.:} Memo1.Lines.Add(Title + ' [' + IntToStr(ProcessID) + ']') end; procedure TForm1.Timer1Timer(Sender: TObject); begin EnumWindows(@EnumWindowsProc, Integer(Self)) end; |