Delphi7 获取所有进程和名称
时间: 2023-06-19 18:05:47 浏览: 246
可以使用 Windows API 函数 EnumProcesses 和 GetModuleBaseName 来获取系统中所有进程的信息,下面是一个 Delphi7 的示例代码:
```delphi
const
MAX_PATH = 260;
PROCESS_QUERY_INFORMATION = $0400;
PROCESS_VM_READ = $0010;
type
DWORD_PTR = Cardinal;
function EnumProcesses(lpidProcess: PDWORD; cb: DWORD; var cbNeeded: DWORD): BOOL; stdcall; external 'psapi.dll';
function GetModuleBaseNameA(hProcess: THandle; hModule: HMODULE; lpBaseName: PAnsiChar; nSize: DWORD): DWORD; stdcall; external 'psapi.dll';
function OpenProcess(dwDesiredAccess: DWORD; bInheritHandle: BOOL; dwProcessId: DWORD): THandle; stdcall; external 'kernel32.dll';
procedure GetProcessList;
var
lpidProcess: array[0..1023] of DWORD;
cbNeeded, cProcesses: DWORD;
i: Integer;
hProcess: THandle;
szProcessName: array[0..MAX_PATH - 1] of AnsiChar;
begin
if not EnumProcesses(@lpidProcess, SizeOf(lpidProcess), cbNeeded) then
Exit;
cProcesses := cbNeeded div SizeOf(DWORD);
for i := 0 to cProcesses - 1 do
begin
if lpidProcess[i] <> 0 then
begin
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, lpidProcess[i]);
if hProcess <> 0 then
begin
if GetModuleBaseNameA(hProcess, 0, szProcessName, MAX_PATH) > 0 then
writeln(lpidProcess[i], ' - ', szProcessName);
CloseHandle(hProcess);
end;
end;
end;
end;
```
这个示例会遍历系统中所有进程,并输出进程 ID 和进程名称。你可以将输出改为存储到一个 TStringList 中,以便进一步处理。
阅读全文