Today, to help people solve the problem, with VB.net wrote a paragraph based on the process name to find the PID method.
Previously used in the VB6 accumulation of APIs to play a role, mainly in the VB.net processing API declaration Notice to change all long to integer, and if any, you need to change to object.
The VB6 long corresponds to the vb.net integer. If you mix it up, you'll get an error.
Now that you've written it, just record it and make it easy to use later.
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 Priva Te Declare Function GetWindow Lib "user32" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer Private Declare Fu Nction 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 lpst Ring as String, ByVal CCH As Integer) As Integer Private Declare Function getwindowtextlength Lib "user32" Alias "Getwi Ndowtextlengtha "(ByVal hwnd As Integer) As Integer ' process name, without the. exe Danielinbiti public Function processpidonly (ByVal P Rocessname as String) as String Dim myprocess As Process () = Process.getprocessesbyname (ProcessName) Dim pid As String = "" D Im 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 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 = GETWI Nhandle (CInt (Pidarr (i)) Windowname = windowname + vbCrLf + getwindowname (hwnd) End If Next I end If processpidonly = windowname End function Private Function getwinhandle (ByVal H Instance 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 Y ou find a match or there is no more window handles:do Until temphwnd = 0 ' Check for PID match ' Take the process ID that the window belongs to If GetParent (temphwnd) = 0 Then test_thread_id = GetWindowThreadProcessId (temp Hwnd, test_pid) If test_pid = HInstance Then ' Return found handle Ge Twinhandle = Temphwnd ' exit search loop Exit do End If En D If ' Get the next window handle temphwnd = GetWindow (Temphwnd, Gw_hwndnext) Loop End Fun ction Private Function Getwindowname (ByVal hwnd as Integer) Dim stitle As String = "" Dim nSize as Intege R nSize = Getwindowtextlength (hwnd) If nSize > 0 Then stitle = space$ (nSize + 1) Ge Twindowtext (hwnd, Stitle, NSize + 1) End If Getwindowname = Stitle End FunctionEnd Module
Get process PID and process based on process name