加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

根据进程名获取进程PID以及进程

发布时间:2020-12-17 07:49:03 所属栏目:百科 来源:网络整理
导读:今天,帮人解答问题时,用VB.NET写了段根据进程名称查找PID的方法。 以前用VB6时积累的API发挥了作用,主要在VB.NET处理API声明时注意一下把所有的long改成Integer,同时如果有Any的话也需要改成Object。 VB6的Long对应VB.NET的Integer。如果混了,会报错的。

今天,帮人解答问题时,用VB.NET写了段根据进程名称查找PID的方法。

以前用VB6时积累的API发挥了作用,主要在VB.NET处理API声明时注意一下把所有的long改成Integer,同时如果有Any的话也需要改成Object。

VB6的Long对应VB.NET的Integer。如果混了,会报错的。

既然写了,就记录一下,便于后面使用

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读