taotao11994 2015-01-16 11:47 采纳率: 100%
浏览 4303
已采纳

vb.net 通过 进程名 获取 句柄

通过标题的方法我会。不过通过 进程的搜了半天也没有一个例子。

麻烦大家。如果可以,给个简单例子,只要能获取到主窗口句柄就行,因为标题会变。所以不方便

麻烦大家了。谢了

  • 写回答

3条回答 默认 最新

  • danielinbiti 2015-01-16 14:51
    关注

    ProcessPidOnly 是入口,如查找cmd.exe进程,进程命名cmd.exe
    Dim name As String
    name = ProcessPidOnly(Trim("cmd"))

     Module Module1
        Private Const GW_HWNDNEXT = 2
        Private Const GW_CHILD = 5
        Private Declare Function GetDesktopWindow Lib "user32" () As Integer
        Private Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Integer
        Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
        Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
        Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA"  (ByVal hwnd As Integer) As Integer
        '进程名,不带.exe danielinbiti
        Public Function ProcessPidOnly(ByVal ProcessName As String) As String
            Dim myProcess As Process() = Process.GetProcessesByName(ProcessName)
            Dim pid As String = ""
            Dim windowname As String = ""
            If myProcess.Length - 1 = 0 Then
                pid = myProcess(0).Id
            Else
                For i As Short = 0 To myProcess.Length - 1
                    pid = pid & myProcess(i).Id & ";"
                Next
            End If
            If Trim(pid) <> "" Then
                Dim hwnd As Integer
                Dim pidArr = pid.Split(";")
                For i As Short = 0 To pidArr.Length - 1
                    If Trim(pidArr(i)) <> "" Then
                        hwnd = GetWinHandle(CInt(pidArr(i)))
                        windowname = windowname + vbCrLf + getWindowName(hwnd)
                    End If
                Next i
            End If
            ProcessPidOnly = windowname
        End Function
        Private Function GetWinHandle(ByVal hInstance As Integer) As Integer
            Dim tempHwnd As Integer
            Dim test_pid As Integer
            Dim test_thread_id As Integer
            GetWinHandle = 0
            tempHwnd = GetDesktopWindow()
            tempHwnd = GetWindow(tempHwnd, GW_CHILD)
            ' Loop until you find a match or there are no more window handles:
            Do Until tempHwnd = 0
                ' Check for PID match
                ' 取该窗口所属的进程ID
                If GetParent(tempHwnd) = 0 Then
                    test_thread_id = GetWindowThreadProcessId(tempHwnd, test_pid)
                    If test_pid = hInstance Then
                        ' Return found handle
                        GetWinHandle = tempHwnd
                        ' Exit search loop
                        Exit Do
                    End If
                End If
                ' Get the next window handle
                tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
            Loop
        End Function
        Private Function getWindowName(ByVal hwnd As Integer)
            Dim sTitle As String = ""
            Dim nSize As Integer
            nSize = GetWindowTextLength(hwnd)
            If nSize > 0 Then
    
                sTitle = Space$(nSize + 1)
                GetWindowText(hwnd, sTitle, nSize + 1)
            End If
            getWindowName = sTitle
        End Function
    
    
    
    End Module
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179