VB知道进程名怎么获得进程句柄。。

VB知道进程名怎么获得进程句柄。。
2024-11-07 14:34:51
推荐回答(2个)
回答(1):

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 1024
End Type
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
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

'查找进程的函数
Private Function fun_FindProcess(ByVal ProcessName As String) As Long

Private Sub Command1_Click()
Dim hWnd As Long ' 储存 FindWindow 函数返回的句柄
Dim Pid As Long ' 储存进程标识符
Dim pHandle As Long ' 储存进程句柄
Dim str As String * 20 ' 存储显示文本
' 取得目标窗口的句柄
hWnd = FindWindow(vbNullString, "计算器")
If (hWnd = 0) Then Exit Sub
' 取得进程标识符
GetWindowThreadProcessId hWnd, Pid
' 取得进程句柄
pHandle = OpenProcess(&H1F0FFF, 0, Pid)
Print pHandle
If (pHandle = 0) Then Exit Sub
' 读取内存数据
ReadProcessMemory pHandle, &H40B181, str, 20, 0& 'PROCESS_ALL_ACCESS不知道你定义了没有,我换成直接的值,还有False换成了0
' 在文本框显示
Text2.Text = str
' 关闭进程句柄
CloseHandle hProcess
End Sub

这个例子够你明白的了

回答(2):

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Dim hwndWow As Long
hwndWow = FindWindow(vbNullString, "魔兽世界")
If hwndWow <= 0 Then
MsgBox ("魔兽世界没有运行,请先运行后再开始。")
CommandGo.Enabled = True
Exit Sub
End If