CreateToolhelp32Snapshot
CreateToolhelp32Snapshot函数为指定的进程、进程使用的堆[HEAP]、模块[MODULE]、线程[THREAD])建立一个快照[snapshot]。
HANDLE WINAPI CreateToolhelp32Snapshot(
DWORD dwFlags,
DWORD th32ProcessID
);
参数:
dwFlags
[输入]指定快照中包含的系统内容,这个参数能够使用下列数值(变量)中的一个。
TH32CS_INHERIT - 声明快照句柄是可继承的。
TH32CS_SNAPALL - 在快照中包含系统中所有的进程和线程。
TH32CS_SNAPHEAPLIST - 在快照中包含在th32ProcessID中指定的进程的所有的堆。
TH32CS_SNAPMODULE - 在快照中包含在th32ProcessID中指定的进程的所有的模块。
TH32CS_SNAPPROCESS - 在快照中包含系统中所有的进程。
TH32CS_SNAPTHREAD - 在快照中包含系统中所有的线程。
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
th32ProcessID
[输入]指定将要快照的进程ID。如果该参数为0表示快照当前进程。该参数只有在设置了TH32CS_SNAPHEAPLIST,TH32CS_SNAPMODULE,TH32CS_SNAPALL后才有效,在其他情况下该参数被忽略,所有的进程都会被快照。
返回值:
调用成功,返回快照的句柄,调用失败,返回INVALID_HANDLE_VALUE 。
备注:
使用GetLastError函数查找该函数产生的错误状态码。
要删除快照,使用CloseHandle函数
delphi使用实例:
uses TLHelp32;
procedure TForm1.Button1Click(Sender: TObject);
var
ProcessName: string;
ProcessID: integer;
ListLoop: Boolean;
FsnapShotHandle: Thandle;
FProcessEntry32: TProcessEntry32;
begin
Fsnapshothandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwsize := SizeOF(FProcessEntry32);
Listloop := Process32First(FSnapshotHandle, FProcessEntry32);
while Listloop do begin
ProcessName := FprocessEntry32.szExeFile;
ProcessID := FProcessEntry32.th32ProcessID;
// 我写到列表框中了,你可以根据需要自己改
listbox1.Items.Add('Process NAME:' + ProcessNAme);
ListLoop := Process32Next(FSnapshotHandle, FprocessEntry32);
end;
end;
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
Do
If Left(LCase(ProcessInfo.szExeFile), InStr(ProcessInfo.szExeFile, ".") + 3) = "notepad.exe" Then
'遍历进程,查找notepad.exe,找到后执行操作.......
End If
Loop While Process32Next(MySnapHandle, ProcessInfo) <> 0
End If
CloseHandle MySnapHandle
end sub
C++例子:
#include "StdAfx.h"
#include "windows.h"
#include "tlhelp32.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
PROCESSENTRY32 pe32;
//在使用这个结构前,先设置它的大小
pe32.dwSize = sizeof(pe32);
//给系统内所有的进程拍个快照
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshot 调用失败./n");
return -1;
}
//遍历进程快照,轮流显示每个进程的信息
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
while (bMore)
{
printf("进程名称:%s/n",pe32.szExeFile);
printf("进程ID:%u/n/n",pe32.th32ProcessID);
bMore = ::Process32Next(hProcessSnap,&pe32);
}
//不要忘记清除掉snapshot对象
::CloseHandle(hProcessSnap);
return 0;
}
PROCESSENTRY32
PROCESSENTRY32 Structure
Describes an entry from a list of the processes residing in the system address space when a snapshot was taken.
用来存放快照进程信息的一个结构体。(存放进程信息和调用成员输出进程信息)
用来 Process32First指向第一个进程信息,并将进程信息抽取到PROCESSENTRY32中。用 Process32Next指向下一条进程信息。
Syntax
C++
typedef struct tagPROCESSENTRY32
{
DWORD dwSize;
DWORD cntUsage;
DWORD th32ProcessID;
ULONG_PTR th32DefaultHeapID;
DWORD th32ModuleID;
DWORD cntThreads;
DWORD th32ParentProcessID;
LONG pcPriClassBase;
DWORD dwFlags;
TCHAR szExeFile[MAX_PATH];
} PROCESSENTRY32, *PPROCESSENTRY32;
Members
dwSize (结构的大小)The size of the structure, in bytes. Before calling the Process32First function, set this member to sizeof(PROCESSENTRY32). If you do not initialize dwSize, Process32First fails.
(这个结构的长度,以字节为单位,初始化一个实例以后调用Process32First函数,设置成员的大小sizeof(PROCESSENTRY32).如果你没用PROCESSENTRY32中的成员dwSize初始 化,pricess32First将会失败。)
cntUsage (此进程的引用计数)This member is no longer used and is always set to zero.
(这个成员已经很久没有使用,总是设置为零。)
th32ProcessID 进程ID=process identifier=PIDThe process identifier.
(这个就是任务管理器里面的进程的PID,打开任务管理器--查看---选择列---PID(勾选)就可以显示进程的标示符(PID))
th32DefaultHeapID 进程默认堆IDThis member is no longer used and is always set to zero.
(这个成员已经很久没有使用,总是设置为零。)
th32ModuleID 进程模块IDThis member is no longer used and is always set to zero.
(这个成员已经很久没有使用,总是设置为零。)
cntThreads 此进程开启的线程计数The number of execution threads started by the process.
(这个成员执行线程开始的进程。)
th32ParentProcessID (父进程的ID)The identifier of the process that created this process (its parent process).
pcPriClassBase .(线程优先权)The base priority of any threads created by this process
当前进程创建的任何一个线程的基础优先级,即在当前进程内创建线程的话,其基本优先级的值。
dwFlags This member is no longer used, and is always set to zero.
(这个成员已经很久没有使用,总是设置为零。)
szExeFile (一个数组)进程全名The name of the executable file for the process. To retrieve the full path to the executable file, call the Module32First function and check the szExePath member of the MODULEENTRY32 structure that is returned. However, if the calling process is a 32-bit process, you must call the QueryFullProcessImageName function to retrieve the full path of the executable file for a 64-bit process.
(进程的可执行文件名称。要获得可执行文件的完整路径,应调用Module32First函 数,再检查其返回的MODULEENTRY32结构的szExePath成员。但是,如果被调用进程是一个32位程序,您必须调用 QueryFullProcessImageName函数去获取64位进程的可执行文件完整路径名。)
GetForegroundWindow
函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
函数原型:HWND GetForegroundWindow(VOID)
参数:无。
返回值:函数返回前台窗回的句柄。
速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
GetWindowThreadProcessId
目录[隐藏]
1.1.函数原型
2.2.参数
3.3.返回值
4.4.函数相关信息
1.1、说明
2.2、返回值
3.3、参数表
1.1.函数原型
2.2.参数
3.3.返回值
4.4.函数相关信息
1.1、说明
2.2、返回值
3.3、参数表
GetWindowThreadProcessId
[编辑本段]
一、VC
--------------------------------------------------------------------------------
The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.
意译:
找出某个窗口的创建者(线程或进程),返回创建者的标志符。
哪个线程创建了这个窗口,返回的就是这个线程的id号 (进程只有一个线程的话,那么线程标志符与进程标志符就是指同一个标志符)
可理解为找出某栋房屋的建筑工人的名字。
1.函数原型
DWORD GetWindowThreadProcessId(
HWND hWnd,
LPDWORD lpdwProcessId
);
2.参数
hWnd [in] (向函数提供的)被查找窗口的句柄.
lpdwProcessId [out] 答案的存放地址(变 量地址) Pointer to a variable that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the variable; otherwise, it does not. (如果参数不为NULL,即提供了存放处--变量,那么本函数把进程标志拷贝到存放处,否则不动作。)
3.返回值
The return value is the identifier of the thread that created the window.
返回创建者的标志,注意, lpdwProcessId 是存放创建者标志的变量。即,返回值是答案, lpdwProcessId 是答案存放处。
当然可以把答案再放到其它地方。如
DWORD PID;
PID=GetWindowThreadProcessId( hWnd, lpdwProcessId );
4.函数相关信息
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
[编辑本段]
二、VB声明
Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
1、说明
The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window
这个函数获得指定线程的标识符,此线程创建了指定的窗口,并且随机的产生了这个标识符.
2、返回值
Long,拥有窗口的线程的标识符
3、参数表
参数 类型及说明
lpdwProcessId Long,指定一个变量,用于装载拥有那个窗口的一个进程的标识符
hwnd Long,指定窗口句柄
OpenProcess
目录[隐藏]
[编辑本段]
一.VC
方法名称: OpenProcess
位置: Kernel32.dll
OpenProcess 函数用来打开一个已存在的进程对象,并返回进程的句柄。
1.函数原型
HANDLE OpenProcess(
DWORD dwDesiredAccess, // access flag
BOOL bInheritHandle, // handle inheritance option
DWORD dwProcessId // process identifier
);
2.返回值:
如成功,返回值为指定进程的句柄。
如失败,返回值为空,可调用GetLastError获得错误代码。
-------------------------------------------------------------