プロセス終了まで待機          <TOP>


CreateProcess プロセスの起動
CloseHandle オープンされているオブジェクトハンドルをクローズ
OpenProcess 既存のプロセスオブジェクトのハンドルを取得
 

例では、メモ帳を起動し終了を監視しています。

 

'================================================================
'= プロセス終了まで待機
'=    (OpenProcess2.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 Name$, ByVal Cmd$, pAtt As Any, tAtt As Any, ByVal Hand&, ByVal Flg&, Env As Any, ByVal Dir$, sInfo As Any, pInfo As PROCESS_INFORMATION)

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

' 既存のプロセスオブジェクトのハンドルを取得
Declare Function Api_OpenProcess& Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess&, ByVal bInheritHandle&, ByVal dwProcessID&)

#define NORMAL_PRIORITY_CLASS &H20      '通常クラス(一般的なプロセス)
#define PROCESS_TERMINATE &H1           '取得したハンドルをTerminateProcess関数で使用できるようにする

Var Shared ProcessId As Long

Var Shared Timer1 As Object
Var Shared Text1 As Object
Var Shared Button1 As Object

Timer1.Attach GetDlgItem("Timer1")
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Button1.Attach getDlgItem("Button1") : Button1.SetFontSize 14

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

    si.cb = Len(si)
    Ret = Api_CreateProcess(ByVal 0, "notepad.exe", ByVal 0, ByVal 0, 0, NORMAL_PRIORITY_CLASS, ByVal 0, "c:\", si, pi)

    Ret = Api_CloseHandle(pi.hProcess)
    Ret = Api_CloseHandle(pi.hThread)

    ProcessId = pi.dwProcessId

    Timer1.SetInterval 100
    Timer1.Enable -1
End Sub

'================================================================
'= TimerによりhProcHandleの変化を監視
'================================================================
Declare Sub Timer1_Timer edecl ()
Sub Timer1_Timer()
    Var hProcHandle As Long
    Var Ret As Long

    'プロセスオブジェクトのハンドル取得
    hProcHandle = Api_OpenProcess(PROCESS_TERMINATE, 0, ProcessId)

    If hProcHandle = 0 Then
        Text1.SetWindowText "メモ帳は終了しました!"
        Timer1.Enable 0
        Exit Sub
    Else
        Text1.SetWindowText "メモ帳は起動中です!"
    End If

    Ret = Api_CloseHandle(hProcHandle)
End Sub

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