vb 如何获取正确的进程名
如果用窗口标题获取的话有时候2个窗口标题一样就可能会获取错误..进程也有一样名字的....如何获取正确的进程名,最好是判断进程名的路径的.总的来说一句话,怎么正确的获取正...
如果用窗口标题获取的话有时候2个窗口标题一样就可能会获取错误..
进程也有一样名字的....
如何获取正确的进程名,
最好是判断进程名的路径的.
总的来说一句话,怎么正确的获取正确的进程名.在多一样进程名的情况下,在多标题(一样)的情况下.. 展开
进程也有一样名字的....
如何获取正确的进程名,
最好是判断进程名的路径的.
总的来说一句话,怎么正确的获取正确的进程名.在多一样进程名的情况下,在多标题(一样)的情况下.. 展开
1个回答
展开全部
我帮你改了一段代码,可以获取进程的PID和路径:
在窗体设计器里放一个listbox,命名为process;
再放2个txtbox,一个名为ProcessID,用来显示PID;
另一个名为Path,用来显示进程路径;
最后再放一个按钮,命名为cmdRefresh,用于刷新列表;
完成后即可运行代码。
Option Explicit
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Dim lngHand As Long
Dim strName As String * 255
Dim lngProcID As Long
Dim lngDeskTopHandle As Long
Dim lngWindowCount As Long
Public Function GetOpenWindowNames() As Long
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
GetWindowText lngHand, strName, Len(strName)
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
If Left$(strName, 1) <> vbNullChar Then
form1.process.AddItem Left$(strName, InStr(1, strName, vbNullChar))
End If
Loop
End Function
Public Function GetProcessPathByProcessID(PID As Long) As String
On Error GoTo Z
Dim cbNeeded As Long
Dim szBuf(1 To 250) As Long
Dim Ret As Long
Dim szPathName As String
Dim nSize As Long
Dim hProcess As Long
hProcess = OpenProcess(&H400 Or &H10, 0, PID)
If hProcess <> 0 Then
Ret = EnumProcessModules(hProcess, szBuf(1), 250, cbNeeded)
If Ret <> 0 Then
szPathName = Space(260)
nSize = 500
Ret = GetModuleFileNameExA(hProcess, szBuf(1), szPathName, nSize)
GetProcessPathByProcessID = Left(szPathName, Ret)
End If
End If
Ret = CloseHandle(hProcess)
If GetProcessPathByProcessID = "" Then
GetProcessPathByProcessID = "SYSTEM"
End If
Exit Function
Z:
End Function
Private Sub cmdRefresh_Click()
GetOpenWindowNames
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
Private Sub Form_Load()
cmdRefresh.Caption = "刷新"
GetOpenWindowNames
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
Private Sub process_Click()
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
在窗体设计器里放一个listbox,命名为process;
再放2个txtbox,一个名为ProcessID,用来显示PID;
另一个名为Path,用来显示进程路径;
最后再放一个按钮,命名为cmdRefresh,用于刷新列表;
完成后即可运行代码。
Option Explicit
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Dim lngHand As Long
Dim strName As String * 255
Dim lngProcID As Long
Dim lngDeskTopHandle As Long
Dim lngWindowCount As Long
Public Function GetOpenWindowNames() As Long
lngDeskTopHandle = GetDesktopWindow()
lngHand = GetWindow(lngDeskTopHandle, GW_CHILD)
Do While lngHand <> 0
GetWindowText lngHand, strName, Len(strName)
lngHand = GetWindow(lngHand, GW_HWNDNEXT)
If Left$(strName, 1) <> vbNullChar Then
form1.process.AddItem Left$(strName, InStr(1, strName, vbNullChar))
End If
Loop
End Function
Public Function GetProcessPathByProcessID(PID As Long) As String
On Error GoTo Z
Dim cbNeeded As Long
Dim szBuf(1 To 250) As Long
Dim Ret As Long
Dim szPathName As String
Dim nSize As Long
Dim hProcess As Long
hProcess = OpenProcess(&H400 Or &H10, 0, PID)
If hProcess <> 0 Then
Ret = EnumProcessModules(hProcess, szBuf(1), 250, cbNeeded)
If Ret <> 0 Then
szPathName = Space(260)
nSize = 500
Ret = GetModuleFileNameExA(hProcess, szBuf(1), szPathName, nSize)
GetProcessPathByProcessID = Left(szPathName, Ret)
End If
End If
Ret = CloseHandle(hProcess)
If GetProcessPathByProcessID = "" Then
GetProcessPathByProcessID = "SYSTEM"
End If
Exit Function
Z:
End Function
Private Sub cmdRefresh_Click()
GetOpenWindowNames
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
Private Sub Form_Load()
cmdRefresh.Caption = "刷新"
GetOpenWindowNames
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
Private Sub process_Click()
lngHand = FindWindow(vbNullString, process.Text)
GetWindowThreadProcessId lngHand, lngProcID
ProcessID = lngProcID
Path = GetProcessPathByProcessID(lngProcID)
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询