|
|||||
<このサンプルの概要> プロセス一覧を表示して子プロセスの起動/終了をするサンプルソースを作ってみました。 VB6のフォームにListBoxを1つ、CommandButtonを3つ、Timerを1つ追加してお試しくださ い。本サンプルのキーワードは以下のAPIです。 (1)CreateToolhelp32Snapshot (2)Process32First (3)Process32Next (4)Shell (5)OpenProcess (6)TerminateProcess (7)TaskKill ★標準モジュール(Module1.bas) ' プロセス一覧API Public Const TH32CS_SNAPHEAPLIST = 1 Public Const TH32CS_SNAPPROCESS = 2 Public Const TH32CS_SNAPTHREAD = 4 Public Const TH32CS_SNAPMODULE = 8 Public Const TH32CS_SNAPALL = 15 Public Const SIZEOF_PROCESSENTRY32 As Long = 296 Public Type PROCESSENTRY32 Size As Long RefCount As Long ProcessID As Long HeapID As Long ModuleID As Long ThreadCount As Long ParentProcessID As Long BasePriority As Long Flags As Long FileName As String * 260 End Type Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _ ByVal Flags As Long, ByVal ProcessID As Long) As Long Public Declare Function Process32First Lib "kernel32" ( _ ByVal lngHandleshot As Long, _ ByRef ProcessEntry As PROCESSENTRY32) As Long Public Declare Function Process32Next Lib "kernel32" ( _ ByVal lngHandle As Long, _ ByRef ProcessEntry As PROCESSENTRY32) As Long ' プロセスの起動/終了API Public Declare Function OpenProcess Lib "kernel32" ( _ ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Public Declare Function CloseHandle Lib "kernel32" ( _ ByVal hObject As Long) As Long Public Declare Function TerminateProcess Lib "kernel32" ( _ ByVal hProcess As Long, _ ByVal uExitCode As Long) As Long Public Const SYNCHRONIZE As Long = &H100000 Public Const PROCESS_TERMINATE As Long = &H1 ★フォームモジュール(Form1.frm) Private Sub Form_Load() ' PIDとプロセス名の一覧表示 Timer1_Timer End Sub Private Sub Timer1_Timer() On Error Resume Next ' プロセス情報取得開始 Dim lngHandle As Long lngHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ' プロセス情報取得 Dim lngResult As Long Dim procEntry As PROCESSENTRY32 procEntry.Size = SIZEOF_PROCESSENTRY32 lngResult = Process32First(lngHandle, procEntry) Do While lngResult ' PIDとプロセス名の一覧表示 Dim strFname As String Dim lngPid As Long Dim blnFlag As Boolean Dim i As Long strFname = Left(procEntry.FileName, InStr(procEntry.FileName, vbNullChar) - 1) lngPid = procEntry.ProcessID blnFlag = False For i = 0 To List1.ListCount - 1 If List1.ItemData(i) = lngPid Then blnFlag = True Exit For End If Next i If blnFlag = False Then List1.AddItem procEntry.ProcessID & _ String(8 - Len(CStr(procEntry.ProcessID)), " ") & _ strFname List1.ItemData(List1.ListCount - 1) = -1 * procEntry.ProcessID List1.ListIndex = List1.ListCount - 1 Else List1.ItemData(i) = -1 * procEntry.ProcessID End If ' 次プロセス情報取得 lngResult = Process32Next(lngHandle, procEntry) Loop For i = List1.ListCount - 1 To 0 Step -1 If List1.ItemData(i) > 0 Then List1.RemoveItem i Else List1.ItemData(i) = -1 * List1.ItemData(i) End If Next i ' プロセス情報取得終了 CloseHandle lngHandle End Sub Private Sub Command1_Click() ' 子プロセス起動 Dim lngPid As Long lngPid = CLng(Shell(App.EXEName & ".exe", vbHide)) End Sub Private Sub Command2_Click() ' プロセス終了確認 If MsgBox(List1.List(List1.ListIndex) & " を終了しますか?", vbYesNo) = vbNo Then Exit Sub End If ' プロセスのPID取得 Dim lngPid As Long lngPid = List1.ItemData(List1.ListIndex) ' プロセスのハンドル取得 Dim lngHandle As Long lngHandle = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0&, lngPid) ' プロセス終了 Call TerminateProcess(lngHandle, 0&) End Sub Private Sub Command3_Click() ' プロセス終了確認 If MsgBox(Mid(List1.List(List1.ListIndex), 9) & " を終了しますか?", vbYesNo) = vbNo Then Exit Sub End If ' プロセス終了 Call Shell("TaskKill.exe /IM " & Mid(List1.List(List1.ListIndex), 9), vbHide) End Sub |