|
|||||
<このサンプルの概要> 以下のWin32Apiを使用したサンプルソースです。 (1)CreateToolhelp32Snapshot (2)Process32First (3)Process32Next (4)OpenProcess (5)CloseHandle (6)TerminateProcess VB.NETでApiを宣言(Declare)する場合はVB.NET用に変換する必要があります。 変換方法のサンプルはこちらです。VB6とVB.NETのAPI宣言(Declare)の違いを比較したサンプル C#.NETでApiを宣言(Declare)する場合はC#.NET用に変換する必要があります。 変換方法のサンプルはこちらです。VB6とC#.NETのAPI宣言(Declare)の違いを比較したサンプル その他のサンプルソースはこちらです。 Win32Api定義のサンプル サンプルソース Private Const SYNCHRONIZE As Long = &H100000 Private Const PROCESS_TERMINATE As Long = &H1 Private Const TH32CS_SNAPHEAPLIST = 1 Private Const TH32CS_SNAPPROCESS = 2 Private Const TH32CS_SNAPTHREAD = 4 Private Const TH32CS_SNAPMODULE = 8 Private Const TH32CS_SNAPALL = 15 Private Const SIZEOF_PROCESSENTRY32 As Long = 296 Private 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 Private Declare Function OpenProcess Lib "kernel32" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Declare Function TerminateProcess Lib "kernel32" _ (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _ (ByVal Flags As Long, ByVal ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" _ (ByVal lngHandleshot As Long, ByRef ProcessEntry As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" _ (ByVal lngHandle As Long, ByRef ProcessEntry As PROCESSENTRY32) As Long 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 |