演示工程下载地址:http://excel.5d6d.com/viewthread.php?tid=45&extra=
1
unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls,TLHelp32,PSAPI, ComCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Panel1: TPanel;
12 Panel2: TPanel;
13 Button1: TButton;
14 ListView1: TListView;
15 procedure Button1Click(Sender: TObject);
16 private
17 { Private declarations }
18 public
19 { Public declarations }
20 end ;
21
22 var
23 Form1: TForm1;
24
25 implementation
26
27 { $R *.dfm }
28
29 procedure TForm1.Button1Click(Sender: TObject);
30 var
31 lpidProcess:Array[ 0 .. 1024 ] of DWORD;
32 cbNeeded:DWORD;
33 i:Integer;
34 item:TListItem;
35 hProcessHandle:DWORD;
36 ModuleFileName:Array[ 0 .. 1024 ] of char;
37 begin
38 ListView1.Clear;
39 { lpidProcess:返回进程ID标识的数组。 }
40 { cb:是进程组数的大小。 }
41 { cbNeeded返回进程数组的大小(单位:字节). }
42 { 返回值:如果函数执行成功,返回True。如果函数执行失败,返回False. }
43 { function EnumProcesses(lpidProcess: LPDWORD; cb: DWORD; var cbNeeded: DWORD): BOOL; }
44 { 此函数在PSAPI模块 }
45 if not EnumProcesses(LPDWORD(@lpidProcess), SizeOf(lpidProcess),cbNeeded) then begin
46 ShowMessage( ' 枚举进程失败! ' );
47 Abort();
48 end ;
49
50 for i: = 0 to cbNeeded div SizeOf(DWORD) - 1 do begin
51 { dwDesiredAccess:访问进程的权限 }
52 { bInheritHandle:句柄是否继承进程属性。 }
53 { dwProcessId:进程ID。 }
54 { 返回值:函数调用成功将返回一个进程句柄值,否则将返回0 }
55 { 注意:在使用完所获得的进程句柄后一定要调用CloseHandle(handle)来关闭进程的句柄。 }
56 { 此函数在Windows模块 }
57 { function OpenProcess(dwDesiredAccess: DWORD; bInheritHandle:
58 BOOL; dwProcessId: DWORD): THandle; stdcall; }
59 { dwDesiredAccess参数详细说明:
60 PROCESS_ALL_ACCESS 所有能获得的权限
61 PROCESS_CREATE_PROCESS 需要创建一个进程
62 PROCESS_CREATE_THREAD 需要创建一个线程
63 PROCESS_DUP_HANDLE 重复使用DuplicateHandle句柄
64 PROCESS_QUERY_INFORMATION 获得进程信息的权限,如它的退出代码、优先级
65 PROCESS_QUERY_LIMITED_INFORMATION /*获得某些信息的权限,如果获得了
66 PROCESS_QUERY_INFORMATION,
67 也拥有PROCESS_QUERY_LIMITED_INFORMATION权限*/
68 PROCESS_SET_INFORMATION 设置某些信息的权限,如进程优先级
69 PROCESS_SET_QUOTA 设置内存限制的权限,使用SetProcessWorkingSetSize
70 PROCESS_SUSPEND_RESUME 暂停或恢复进程的权限
71 PROCESS_TERMINATE 终止一个进程的权限,使用TerminateProcess
72 PROCESS_VM_OPERATION 操作进程内存空间的权限(可用VirtualProtectEx和WriteProcessMemory)
73 PROCESS_VM_READ 读取进程内存空间的权限,可使用ReadProcessMemory
74 PROCESS_VM_WRITE 读取进程内存空间的权限,可使用WriteProcessMemory
75 SYNCHRONIZE 等待进程终止
76 }
77 hProcessHandle: = OpenProcess(PROCESS_ALL_ACCESS,False,lpidProcess[i]);
78 if hProcessHandle = 0 then continue;
79 item: = ListView1.Items.Add();
80 item.Caption: = IntToStr(lpidProcess[i]);
81 { hProcess:用OpenProcesses函数打开进程返回的句柄 }
82 { HMODULE:hModule是目标模块的句柄(当此参数为0时函数返回的是进程可执行文件的路径) }
83 { lpFilename:模块名字字符串缓冲区 }
84 { nSize:模块名字字符串缓冲区大小 }
85 { 返回值:函数调用成功将返回非0,否则将返回0 }
86 { GetModuleFileNameEx函数功能:获得可执行文件的模块路径 }
87 { 此函数在PsAPI模块 }
88 { function GetModuleFileNameEx(hProcess: THandle; hModule: HMODULE;
89 lpFilename: PChar; nSize: DWORD): DWORD; }
90 if GetModuleFileNameEx(hProcessHandle, 0 ,PChar(@ModuleFileName),SizeOf(ModuleFileName)) = 0 then continue;
91 item.SubItems.Text: = ModuleFileName;
92 CloseHandle(hProcessHandle);
93 end ;
94 end ;
95
96 end .
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls,TLHelp32,PSAPI, ComCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Panel1: TPanel;
12 Panel2: TPanel;
13 Button1: TButton;
14 ListView1: TListView;
15 procedure Button1Click(Sender: TObject);
16 private
17 { Private declarations }
18 public
19 { Public declarations }
20 end ;
21
22 var
23 Form1: TForm1;
24
25 implementation
26
27 { $R *.dfm }
28
29 procedure TForm1.Button1Click(Sender: TObject);
30 var
31 lpidProcess:Array[ 0 .. 1024 ] of DWORD;
32 cbNeeded:DWORD;
33 i:Integer;
34 item:TListItem;
35 hProcessHandle:DWORD;
36 ModuleFileName:Array[ 0 .. 1024 ] of char;
37 begin
38 ListView1.Clear;
39 { lpidProcess:返回进程ID标识的数组。 }
40 { cb:是进程组数的大小。 }
41 { cbNeeded返回进程数组的大小(单位:字节). }
42 { 返回值:如果函数执行成功,返回True。如果函数执行失败,返回False. }
43 { function EnumProcesses(lpidProcess: LPDWORD; cb: DWORD; var cbNeeded: DWORD): BOOL; }
44 { 此函数在PSAPI模块 }
45 if not EnumProcesses(LPDWORD(@lpidProcess), SizeOf(lpidProcess),cbNeeded) then begin
46 ShowMessage( ' 枚举进程失败! ' );
47 Abort();
48 end ;
49
50 for i: = 0 to cbNeeded div SizeOf(DWORD) - 1 do begin
51 { dwDesiredAccess:访问进程的权限 }
52 { bInheritHandle:句柄是否继承进程属性。 }
53 { dwProcessId:进程ID。 }
54 { 返回值:函数调用成功将返回一个进程句柄值,否则将返回0 }
55 { 注意:在使用完所获得的进程句柄后一定要调用CloseHandle(handle)来关闭进程的句柄。 }
56 { 此函数在Windows模块 }
57 { function OpenProcess(dwDesiredAccess: DWORD; bInheritHandle:
58 BOOL; dwProcessId: DWORD): THandle; stdcall; }
59 { dwDesiredAccess参数详细说明:
60 PROCESS_ALL_ACCESS 所有能获得的权限
61 PROCESS_CREATE_PROCESS 需要创建一个进程
62 PROCESS_CREATE_THREAD 需要创建一个线程
63 PROCESS_DUP_HANDLE 重复使用DuplicateHandle句柄
64 PROCESS_QUERY_INFORMATION 获得进程信息的权限,如它的退出代码、优先级
65 PROCESS_QUERY_LIMITED_INFORMATION /*获得某些信息的权限,如果获得了
66 PROCESS_QUERY_INFORMATION,
67 也拥有PROCESS_QUERY_LIMITED_INFORMATION权限*/
68 PROCESS_SET_INFORMATION 设置某些信息的权限,如进程优先级
69 PROCESS_SET_QUOTA 设置内存限制的权限,使用SetProcessWorkingSetSize
70 PROCESS_SUSPEND_RESUME 暂停或恢复进程的权限
71 PROCESS_TERMINATE 终止一个进程的权限,使用TerminateProcess
72 PROCESS_VM_OPERATION 操作进程内存空间的权限(可用VirtualProtectEx和WriteProcessMemory)
73 PROCESS_VM_READ 读取进程内存空间的权限,可使用ReadProcessMemory
74 PROCESS_VM_WRITE 读取进程内存空间的权限,可使用WriteProcessMemory
75 SYNCHRONIZE 等待进程终止
76 }
77 hProcessHandle: = OpenProcess(PROCESS_ALL_ACCESS,False,lpidProcess[i]);
78 if hProcessHandle = 0 then continue;
79 item: = ListView1.Items.Add();
80 item.Caption: = IntToStr(lpidProcess[i]);
81 { hProcess:用OpenProcesses函数打开进程返回的句柄 }
82 { HMODULE:hModule是目标模块的句柄(当此参数为0时函数返回的是进程可执行文件的路径) }
83 { lpFilename:模块名字字符串缓冲区 }
84 { nSize:模块名字字符串缓冲区大小 }
85 { 返回值:函数调用成功将返回非0,否则将返回0 }
86 { GetModuleFileNameEx函数功能:获得可执行文件的模块路径 }
87 { 此函数在PsAPI模块 }
88 { function GetModuleFileNameEx(hProcess: THandle; hModule: HMODULE;
89 lpFilename: PChar; nSize: DWORD): DWORD; }
90 if GetModuleFileNameEx(hProcessHandle, 0 ,PChar(@ModuleFileName),SizeOf(ModuleFileName)) = 0 then continue;
91 item.SubItems.Text: = ModuleFileName;
92 CloseHandle(hProcessHandle);
93 end ;
94 end ;
95
96 end .