VB 通过进程PID获取该程序的窗口句柄函数

Option Explicit
 
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_VM_READ = &H10
 
Sub main()
    If FindWindow(vbNullString, "计算器") = 0 Then
        Shell "calc.exe"
    End If
    Debug.Print GetModuleFileNameByHwnd(FindWindow(vbNullString, "计算器"))
End Sub
 
'<>
'********************************************************************************
' 函数: GetModuleFileNameByHwnd
' 功能: 通过窗口句柄得到模块名称
'********************************************************************************
'<>
Public Function GetModuleFileNameByHwnd(ByVal hWindow As Long) As String
 
    Dim dwProcId        As Long
    Dim hProcess        As Long
    Dim hModule         As Long
    Dim nRet            As Long
    Dim szBuf           As String
    Const MAX_SIZE      As Long = 256
    
    If hWindow <= 0 Then Exit Function
    
    '' 得到进程ID
    Call GetWindowThreadProcessId(hWindow, dwProcId)
    If dwProcId = 0 Then Exit Function
    
    '' 根据进程ID,取得进程的句柄
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, dwProcId)
    If hProcess = 0 Then Exit Function
    
    '' 枚举进程中的各个模块
    nRet = EnumProcessModules(hProcess, hModule, 4&, 0&)
    If nRet = 0 Then Exit Function
    
    '' 最后用下面这个函数得到可执行文件的名称
    szBuf = String$(MAX_SIZE, vbNullChar)
    GetModuleFileNameEx hProcess, hModule, szBuf, Len(szBuf)
    GetModuleFileNameByHwnd = StripNulls(szBuf)
    
End Function
'
'-----------------------------------------------------------------------------
'
'***********************************************************
' 函数: StripNulls
' 功能: 清除字符串中多余的Null
'***********************************************************
Public Function StripNulls(ByRef szOriginal As String) As String
Dim i As Long
    i = InStr(szOriginal, vbNullChar)
    If i > 0 Then
        szOriginal = Left$(szOriginal, i - 1)
    End If
    StripNulls = szOriginal
End Function
 

### 回答1: 在Visual Basic中,可以通过使用Windows API函数获取指定进程名称的窗口句柄。下面是一个实现该功能的示例代码: ```vb Imports System.Runtime.InteropServices Public Class Form1 ' 导入Windows API函数 <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer End Function ' 定义方法来获取进程窗口句柄 Private Function GetWindowHandleByProcessName(ByVal processName As String) As IntPtr Dim processes As Process() = Process.GetProcessesByName(processName) If processes.Length > 0 Then ' 获取第一个匹配进程窗口句柄 Return processes(0).MainWindowHandle End If Return IntPtr.Zero End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim processName As String = "notepad" Dim windowHandle As IntPtr = GetWindowHandleByProcessName(processName) If windowHandle <> IntPtr.Zero Then Dim windowTitle As New StringBuilder(256) GetWindowText(windowHandle, windowTitle, windowTitle.Capacity) MessageBox.Show("找到进程名为 '" + processName + "' 的窗口句柄:" + windowHandle.ToString() + vbCrLf + "窗口标题:" + windowTitle.ToString()) Else MessageBox.Show("未找到进程名为 '" + processName + "' 的窗口句柄") End If End Sub End Class ``` 在上面的示例中,`FindWindow`用于查找带有指定类名和窗口名称的顶层窗口句柄,`GetWindowText`用于获取指定窗口的标题文本。通过使用`Process.GetProcessesByName()`方法获取指定进程名称的进程列表,然后从中获取第一个进程的主窗口句柄,即可获得所需的窗口句柄。 ### 回答2: 在VB中,我们可以通过进程名获得窗口句柄,具体步骤如下: 1. 首先,我们需要引用Windows API函数,以便使用相关的函数和结构体。在VB中,可以使用Declare语句引用API函数。 ```vb Declare Function FindWindowExA Lib "user32" (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long ``` 2. 接下来,我们可以通过进程名遍历系统中的进程,找到目标进程窗口句柄。我们使用FindWindowExA函数,该函数通过父窗口句柄、子窗口句柄、类名和窗口标题来查找窗口。 ```vb Dim hWnd as Long Dim hWndProcess as Long Dim procID as Long hWnd = FindWindowExA(0, 0, vbNullString, "目标进程窗口标题") ``` 3. 一旦我们找到窗口句柄,我们可以使用GetWindowThreadProcessId函数获取进程ID。 ```vb GetWindowThreadProcessId hWnd, procID ``` 4. 最后,我们可以使用进程ID和窗口句柄进行进一步的操作,例如向窗口发送消息、操纵窗口等。 需要注意的是,通过进程名获得窗口句柄可能存在一些限制。因为一个进程可以创建多个窗口,而窗口标题也可能不唯一,所以在使用FindWindowExA函数时,我们需要确保目标窗口的标题是唯一的,以免出现误判的情况。 总之,通过以上步骤,我们可以在VB中通过进程名获得窗口句柄,并且可以进一步使用该句柄进行相关操作。 ### 回答3: 在VB中,我们可以通过以下步骤来通过进程获取窗口句柄。 首先,我们需要使用System.Diagnostics命名空间中的Process类来获取已经运行的所有进程的详细信息。我们可以使用Process.GetProcessesByName方法来获取指定进程名的进程对象数组。 接下来,我们可以遍历这个进程对象数组,通过使用Process.MainWindowTitle属性来获取进程的主窗口标题,并与我们想要的进程名进行比较。 一旦找到匹配的进程,我们可以使用Process.MainWindowHandle属性来获取窗口句柄。这个句柄可以用于后续的窗口操作。 下面是一个通过进程获取窗口句柄的示例代码: ``` Imports System.Diagnostics Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll", SetLastError:=True)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim procName As String = "Notepad" ' 要查找的进程名 Dim processes() As Process = Process.GetProcessesByName(procName) If processes.Length > 0 Then Dim mainWindowHandle As IntPtr = processes(0).MainWindowHandle ' 使用窗口句柄进行后续操作 MessageBox.Show("窗口句柄:" & mainWindowHandle.ToString()) Else MessageBox.Show("未找到指定进程") End If End Sub End Class ``` 以上是一个基本的示例,你可以根据实际需求进行扩展和修改,例如通过进程名模糊匹配等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值