Comment obtenir une liste des processus en cours d'exécution (avec des détails sur PID, propriétaire, etc.) sur ma machine à l'aide de DELPHI? P>
EDIT: strong> Aucune des solutions proposées ne me donne l'utilisateur qui possède le processus, uniquement des informations telles que PID, EXENAME, etc. P>
6 Réponses :
Vous devez utiliser: p>
Structure de processentry32 contiendra tout le informations que vous pourriez avoir besoin. p>
La documentation est de MDSN, pour C ++, mais c'est le même em> dans Delphi. P>
Une solution consiste à utiliser le bibliothèque d'aide à outils a> (voir tlhelp32 unité), ou Enumprocesses a > Sous Windows NT (voir unité PSAPI). Jetez un coup d'oeil à Voici un exemple rapide de la manière d'obtenir le nom d'utilisateur d'un processus: p> jclsysinfo.runningprocesseslist code> dans le JCL a> Pour un exemple.
Malheureusement, cela ne répond pas à ma nécessité de trouver le propriétaire d'un processus
À l'aide de la bibliothèque d'aide à outils, il y a le champ Th32ParentProcessid dans les entrées retournées. JClsysInfo elle-même ne l'utilise pas, mais vous pouvez l'utiliser comme point de départ.
Tondrej - Je pensais en fait à l'utilisateur qui possède / exécute le processus
Je vois. J'ai supposé que par "propriétaire", vous vouliez dire le processus parent. J'ai ajouté du code pour obtenir le nom d'utilisateur du compte sous lequel le processus est en cours d'exécution.
Ceci est la fonction que nous utilisons pour vérifier si un processus existe, le FProCessentry32 détient toutes les informations sur le processus, de sorte que vous devriez pouvoir l'étendre à ce dont vous avez besoin.
Il a été pris de tagPROCESSENTRY32 = packed record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD; // this process
th32DefaultHeapID: DWORD;
th32ModuleID: DWORD; // associated exe
cntThreads: DWORD;
th32ParentProcessID: DWORD; // this process's parent process
pcPriClassBase: Longint; // Base priority of process's threads
dwFlags: DWORD;
szExeFile: array[0..MAX_PATH - 1] of Char;// Path
end;
Merci pour le code, cela fonctionne bien. BTW, vous pouvez remplacer cette clause si la clause avec si strutils.endstext (nom de fichier EXE, processentry32.szexefile) alors code>.
Indice: Nous pouvons Quitter Code> après le résultat : = true code>. Aucun point dans itération du tout quand nous avons déjà le résultat. Et en utilisant Sametext code> au lieu de majuscule code> pourrait également aider.
Cette classe vous donnera une liste de tous Ouvrez Windows (répertorié ci-dessous) avec pid, légende, dimensions, etc. Ce n'est pas exactement d'information sur les processus, mais je l'ai utilisé pour trouver des applications via elle.
// Window List Component 1.5 by Jerry Ryle
//
// Aaugh! I accidentally uploaded the wrong source
// which had a nasty bug in the refresh procedure!
// Thanks to Serge, who found my mistake and suggested
// a few other improvements!
//
// This component will enumerate windows and return
// information about them in the Windows property.
// The component currently returns a handle, caption text,
// associated ProcessID, visibility, and dimensions.
// For documentation, please read the accompanying
// WindowList.txt
//
// This component is completely free of course. If you find
// it useful, and are compelled to send me cash, beer, or
// dead things in envelopes, please feel free to do so.
//
// email me if you make it better: gryle@calpoly.edu
unit WindowList;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TWindowObject = record
WinHandle : HWnd; // Window Handle
WinCaption : String; // Window Caption Text (If any)
ProcessID : Integer; // Process the window belongs to
IsVisible : Boolean; // Is the window visible?
IsEnabled : Boolean; // Is the window enabled for mouse/keyboard input?
IsIconic : Boolean; // Is the window minimized?
WindowRect : TRect; // Window Dimensions
// Add more properties here if you like,
// then fill them in at the WindowCallback
// function.
end;
PTWindowObject = ^TWindowObject;
TWindowList = class(TComponent)
private
WindowLst : TList;
FCount : Integer;
protected
Function GetAWindow(Index : Integer) : TWindowObject;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
Procedure Refresh;
Property Windows[Index : Integer]: TWindowObject read GetAWindow;
Property Count : Integer read FCount;
published
// Published declarations
end;
procedure Register;
implementation
// Note that this function is not a member of WindowList.
// Therefore, the list to be filled needs to be passed
// as a pointer. Note that this is passed as a VAR. if you
// don't do this, bad things happen in memory.
Function WindowCallback(WHandle : HWnd; Var Parm : Pointer) : Boolean; stdcall;
// This function is called once for each window
Var MyString : PChar;
MyInt : Integer;
MyWindowPtr : ^TWindowObject;
begin
New(MyWindowPtr);
// Window Handle (Passed by the enumeration)
MyWindowPtr.WinHandle := WHandle;
// Window text
MyString := Allocmem(255);
GetWindowText(WHandle,MyString,255);
MyWindowPtr.WinCaption := String(MyString);
FreeMem(MyString,255);
// Process ID
MyInt := 0;
MyWindowPtr.ProcessID := GetWindowThreadProcessId(WHandle,@MyInt);
// Visiblity
MyWindowPtr.IsVisible := IsWindowVisible(WHandle);
// Enabled
MyWindowPtr.IsEnabled := IsWindowEnabled(WHandle);
// Iconic
MyWindowPtr.IsIconic := IsIconic(WHandle);
// Window Dimensions
MyWindowPtr.WindowRect := Rect(0,0,0,0);
GetWindowRect(WHandle,MyWindowPtr.WindowRect);
// Add the structure to the list. Do not dereference Parm...
// once again, bad things happen.
TList(Parm).Add(MyWindowPtr);
Result := True; // Everything's okay. Continue to enumerate windows
end;
constructor TWindowList.Create(AOwner: TComponent);
var MyWindowPtr : PTWindowObject;
begin
inherited;
WindowLst := TList.Create;
// Thanks Serge, I should've done this from the start :)
// Sloppy me.
If Not ( csDesigning in ComponentState ) Then
Begin
EnumWindows(@WindowCallback,Longint(@WindowLst));
FCount := WindowLst.Count;
End
Else
FCount := 0;
end;
destructor TWindowList.Destroy;
var I : Integer;
begin
If WindowLst.Count > 0 Then
Begin
For I := 0 To (WindowLst.Count - 1) Do
Dispose(PTWindowObject(WindowLst[I]));
End;
WindowLst.Free;
inherited;
end;
procedure TWindowList.Refresh;
begin
WindowLst.Clear; {Clear the list!}
EnumWindows(@WindowCallback,Longint(@WindowLst));
FCount := WindowLst.Count;
end;
function TWindowList.GetAWindow(Index : Integer) : TWindowObject;
begin
Result := PTWindowObject(WindowLst[Index])^;
end;
procedure Register;
begin
RegisterComponents('System', [TWindowList]);
end;
end.
Vous pouvez regarder à l'aide de composants WMISET (69 $ Licence unique, 199 $ pour Licence de site, version d'essai disponible). Le TwmiprocessControl Composant semble encapsuler des appels à Win32_Process. Ils ont également un exemple de Obtenir un propriétaire de processus . P>
Vous devez utiliser JCL proposé ci-dessous. L'utilisation directe avec les fonctions ToolHelP ou EnumProcesses est assez tracas.