vb.net 怎么调用读取窗体句柄和进程PID
请问各位高手帮忙看一下,我用VB.net 编写如下看是没有问题,但运行后确
提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
其中窗体的返回值是 Hwd 5063171889661608672 Long 已经超过32位浮点了,但在C#编写就可以用,在32位浮点以内.vb 6.0 也是这样的,但可以用.
Option Explicit On
Public Class Form1
'得到窗体句柄的函数,FindWindow函数用来返回符合指定的类名( ClassName )和窗口名( WindowTitle )的窗口句柄
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'得到进程标识符的函数
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF '参数决定了对进程的存储权限,使用完全控制
Dim Hwd As Long '存放窗体句柄
Dim pid As Long '存放进程ID
Dim hProcess As Long '存放进程句柄
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Hwd = FindWindow("QElementClient Window", "Element Client")
Hwd = FindWindow((vbNullString), "Windows Media Player")
Me.Text = Hwd
If Hwd = 0 Then
Label2.Text = "程序未运行"
Else
Label2.Text = "程序已运行"
GetWindowThreadProcessId(Hwd, pid)
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
If hProcess = 0 Then
MsgBox("不能读取进程", , "提示")
Else
MsgBox("OK", , "提示!")
End If
End If
End Sub
End Class