api函数OpenProcess可以根据进程PID获得进程句柄。
OpenProcess函数的C++语法如下:
HANDLE WINAPI OpenProcess( _In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId );
其中参数dwDesiredAccess表示对进程的访问权限,参数bInheritHandle表示是否可以继承该进程句柄,dwProcessId参数表示进程的标识PID。
如下代码的作用是获取当前进程句柄:
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long '定义一个进程的访问权限常量,表示可以具有所有权限 Public Const PROCESS_ALL_ACCESS = &H1F0FFF Sub QQ1722187970() Dim pid As Long pid = GetCurrentProcessId '进程句柄变量 Dim hProcess As Long hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid) Debug.Print hProcess '关闭进程句柄 CloseHandle hProcess End Sub
发表评论