用VB获得所有后台运行程序

先用VB获得后台运行程序的名称路径分别显示在第一个列表框与第二个列表框里求怎么获得高手指点... 先用VB获得后台运行程序 的名称路径分别显示在第一个列表框与第二个列表框里 求怎么获得 高手指点 展开
 我来答
么是中1276
2014-07-19 · 超过74用户采纳过TA的回答
知道答主
回答量:186
采纳率:50%
帮助的人:134万
展开全部
我帮你改了一段代码,可以获取进程的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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式