process32First 是一个进程获取函数,当我们利用函数CreateToolhelp32Snapshot()获得当前运行进程的快照后,我们可以利用process32First函数来获得第一个进程的句柄.其原型为(用的是vfp)
DECLARE INTEGER Process32First IN WIN32API ;
INTEGER hSnapshot,STRING @ lppe
在C语言中如下
1
2
3
4
|
BOOL
WINAPI Process32First(
HANDLE
hSnapshot,
//_in
LPPROCESSENTRY32 lppe
//_out
);
|
其中PROCESSENTRY32结构为:
PROCESSENTRY32 结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
typedef
struct
tagPROCESSENTRY32 {
DWORD
dwSize;
// 结构大小;
DWORD
cntUsage;
// 此进程的引用计数;
DWORD
th32ProcessID;
// 进程ID;
DWORD
th32DefaultHeapID;
// 进程默认堆ID;
DWORD
th32ModuleID;
// 进程模块ID;
DWORD
cntThreads;
// 此进程开启的线程计数;
DWORD
th32ParentProcessID;
// 父进程ID;
LONG
pcPriClassBase;
// 线程优先权;
DWORD
dwFlags;
// 保留;
char
szExeFile[MAX_PATH];
// 进程全名;
} PROCESSENTRY32;
|
此函数往往和
Process32Next(
Handle hsnapShot,
LPPROCESSENTRY32 lppe)
搭配使用,用来枚举当前系统快照相关的所有进程。
函数返回值:成功返回true,否则返回false.
vb实例
Private Type
PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
Private Sub demo()
Dim MySnapHandle As Long
Dim ProcessInfo As
PROCESSENTRY32
MySnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
ProcessInfo.dwSize = Len(ProcessInfo)
If Process32First(MySnapHandle, ProcessInfo) <> 0 Then
'用来判断快照MySnapHandle 成功获得进程信息
end if
end sub
===============================================================================
C/C++
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
PROCESSENTRY32 p;
PROCESSENTRY32 *info = &p;
// 在使用这个结构之前,先设置它的大小
info->dwSize =
sizeof
(PROCESSENTRY32 );
char
proName[] = {
"devenv.exe"
};
HANDLE
handlePro = NULL;
//结束进程句柄
// 给系统内的所有进程拍一个快照
HANDLE
hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if
(hProcessSnap == INVALID_HANDLE_VALUE)
{
printf
(
" CreateToolhelp32Snapshot调用失败! \n"
);
return
-1;
}
// 遍历进程快照,轮流显示每个进程的信息
BOOL
bMore = ::Process32First(hProcessSnap, info);
BOOL
terminate = FALSE;
while
( bMore != FALSE)
{
if
(
strcmp
(proName,info->szExeFile) == 0)
{
handlePro=OpenProcess(PROCESS_TERMINATE, FALSE, info->th32ProcessID);
if
(NULL == handlePro)
{
break
;
}
//结束进程
terminate = TerminateProcess(handlePro, 0);
}
bMore = Process32Next(hProcessSnap, info);
}
::CloseHandle(hProcessSnap);
|
===============================================================================