プロセスの強制終了

example(VBA)

' OpenProcess 関数
Private Declare Function OpenProcess Lib "KERNEL32.DLL" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle  As Long, _
    ByVal dwProcessId     As Long) As Long

' CloseHandle 関数
Private Declare Function CloseHandle Lib "KERNEL32.DLL" ( _
    ByVal hObject As Long) As Long

' TerminateProcess 関数
Private Declare Function TerminateProcess Lib "KERNEL32.DLL" ( _
    ByVal hProcess  As Long, _
    ByVal uExitCode As Long) As Long

' 定数の定義
Private Const SYNCHRONIZE       As Long = &H100000
Private Const PROCESS_TERMINATE As Long = &H1

'メモ帳を起動して終了させるまでの流れです。
Sub Main()
    Dim pId As Long, pHand As Long

    pId = CLng(Shell("Notepad", vbNormalFocus))

    ' プロセスのハンドルを取得する
    pHand = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0&, pId)

    ' メモ帳を強制終了する
    Call TerminateProcess(pHand, 0&)

    ' プロセスのハンドルを閉じる
    Call CloseHandle(pHand)

End Sub

プロセスを強制終了させるにはKERNEL32.DLLのTerminateProcessおよびCloseHandle関数を使用します。


すでに発行済みのプロセスを強制終了する

example(VBA)

Option Explicit

Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" ( _
            ByVal Flags As Long, _
            ByVal ProcessID As Long) As Long
            
Private Declare Function Process32First Lib "kernel32.dll" ( _
            ByVal lngHandleshot As Long, _
            ByRef ProcessEntry As PROCESSENTRY32) As Long
            
Private Declare Function Process32Next Lib "kernel32.dll" ( _
            ByVal lngHandleshot As Long, _
            ByRef ProcessEntry As PROCESSENTRY32) As Long

Private Declare Function OpenProcess Lib "kernel32.dll" ( _
            ByVal dwDesiredAccess As Long, _
            ByVal blnherithandle As Long, _
            ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32.dll" ( _
            ByVal hObject As Long) As Long
            
Private Declare Function TerminateProcess Lib "kernel32.dll" ( _
            ByVal hProcess As Long, _
            ByVal uExitCode As Long) As Long
            
Private Const SYNCHRONIZE As Long = &H100000
Private Const PROCESS_TERMINATE As Long = &H1
Private Const TH32CS_SNAPPROCESS As Long = &H2
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
    ParentPriority As Long
    BasePriority As Long
    Flags As Long
    FileName As String * 256
End Type

Sub Main()
    Dim lHand As Long, lRet As Long
    Dim procEntry As PROCESSENTRY32
    
    lHand = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, &O0)
    procEntry.Size = SIZEOF_PROCESSENTRY32
    lRet = Process32First(lHand, procEntry)
    Do While lRet
        Dim FileName As String
        Dim lPId As Long
        Dim flag As Boolean
        Dim i As Long
        
        FileName = Left(procEntry.FileName, InStr(procEntry.FileName, vbNullChar) - 1)
        If FileName = "notepad.exe" Then
            i = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0&, procEntry.ProcessID)
            Call TerminateProcess(i, 0)
            Call CloseHandle(i)
        End If
        lRet = Process32Next(lHand, procEntry)
    Loop
End Sub

すでに起動済みのメモ帳を停止させるサンプルです。

もちろんプロセスを停止数前には必要に応じて保存する処理を追加する必要があります。