指定のプロセスを強制終了させる          <TOP>


電卓を起動し、強制終了させています。

CreateProcess プロセスの起動

TerminateProcess 指定のプロセスを強制終了させる

CloseHandle オブジェクトハンドルをクローズ

 

電卓起動後、すぐ終了するので確認のためメッセージを表示させ一旦停止させています。

 

'================================================================
'= 指定のプロセスを強制終了させる
'=    (TerminateProcess.bas)
'================================================================
#include "Windows.bi"

Type PROCESS_INFORMATION
  hProcess    As Long
  hThread     As Long
  dwProcessId As Long
  dwThreadId  As Long
End Type

Type STARTUPINFO
  cb As Long
  lpReserved      As Long
  lpDesktop       As Long
  lpTitle         As LOng
  dwX             As Long
  dwY             As Long
  dwXSize         As Long
  dwYSize         As Long
  dwXCountChars   As Long
  dwYCountChars   As Long
  dwFillAttribute As Long
  dwFlags         As Long
  wShowWindow     As Integer
  cbReserved2     As Integer
  lpReserved2     As Long
  hStdInput       As Long
  hStdOutput      As Long
  hStdError       As Long
End Type

' プロセスの起動
Declare Function Api_CreateProcess& Lib "kernel32" Alias "CreateProcessA" (ByVal lpName&, ByVal lpComLine$, lpPAttr As Any, lpTAttr As Any, ByVal bHndl&, ByVal dwCFlg&, lpEnv As Any, ByVal lpCDir$, lpSInfo As STARTUPINFO, lpProc As PROCESS_INFORMATION)

' 指定のプロセスを強制終了させる
Declare Function Api_TerminateProcess& Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess&, ByVal uExitCode&)

' オープンされているオブジェクトハンドルをクローズ
Declare Function Api_CloseHandle& Lib "Kernel32" Alias "CloseHandle" (ByVal hObject&)

#define SYNCHRONIZE &H100000
#define PROCESS_TERMINATE &H1
#define NORMAL_PRIORITY_CLASS &H20

Var Shared Button1 As Object

Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    ShowWindow -1
    Cls
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var pi As PROCESS_INFORMATION
    Var StartInfo As STARTUPINFO
    Var ProcHandle As Long
    Var Ret As Long

  StartInfo.cb = Len(StartInfo)
    Ret = Api_CreateProcess(0, "Calc.exe", ByVal 0, ByVal 0, 1, NORMAL_PRIORITY_CLASS, ByVal 0, ByVal 0, StartInfo, pi)

    '確認のため一旦制御を止める
    A% = MessageBox("", "電卓を終了します", 0, 2)

    '電卓を終了
    Ret = Api_TerminateProcess(pi.hProcess, 0)

    'ハンドルの解放
    Ret = Api_CloseHandle(pi.hThread)
    Ret = Api_CloseHandle(pi.hProcess)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End