指定された時間スレッドをスリープ          <TOP>


CreateProcess プロセスの起動

GetExitCodeProcess 指定プロセスの終了コードを取得

WaitForSingleObject 指定された時間が経過するまでスレッドをスリープ

CloseHandle オープンされているオブジェクトハンドルをクローズ

 

起動させたメモ帳が終了するまで、スレッドがスリープする状態を表しています。

 

'================================================================
'= 指定された時間スレッドをスリープ
'=    (WaitForSingleObject3.bas)
'================================================================
#include "Windows.bi"

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

Type PROCESS_INFORMATION
    hProcess    As Long
    hThread     As Long
    dwProcessID As Long
    dwThreadID  As Long
End Type

' プロセスの起動
Declare Function Api_CreateProcess& Lib "kernel32" Alias "CreateProcessA" (ByVal lpAppName&, ByVal lpCmdLin$, ByVal lpPrcAttr&, ByVal lpAttr&, ByVal bHand&, ByVal dwFlag&, ByVal lpEnv&, ByVal lpDir&, lpInf As STARTUPINFO, lpPrcInf As PROCESS_INFORMATION)

' 指定プロセスの終了コードを取得
Declare Function Api_GetExitCodeProcess& Lib "Kernel32" Alias "GetExitCodeProcess" (ByVal hProcess&, lpExitCode&)

' 指定されたカーネルオブジェクトがシグナル状態になるか、指定された時間が経過するまでスレッドをスリープ
Declare Function Api_WaitForSingleObject& Lib "Kernel32" Alias "WaitForSingleObject" (ByVal hHandle&, ByVal dwMilliseconds&)

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

#define NORMAL_PRIORITY_CLASS &H20      '通常クラス(一般的なプロセス)
#define INFINITE -1                     '無限に待つ

Var Shared Button1 As Object

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

'================================================================
'=
'================================================================
Declare Function ExecCmd(cmdline As String)
Function ExecCmd(cmdline As String)
    Var si As STARTUPINFO
    Var pi As PROCESS_INFORMATION
    Var Ret As Long

    'STARTUPINFO構造体初期化
    si.cb = Len(si)

    '指定プロセス(メモ帳)を起動
    Ret = Api_CreateProcess(ByVal 0, cmdline, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, ByVal 0, si, pi)

    '指定プロセスの終了を待つ
    Ret = Api_WaitForSingleObject(pi.hProcess, INFINITE)

    '指定プロセスの終了コードを取得
    Ret = Api_GetExitCodeProcess(pi.hProcess, Ret)

    'オブジェクトハンドルをクローズ
    Ret = Api_CloseHandle(pi.hThread)
    Ret = Api_CloseHandle(pi.hProcess)
End Function

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var Ret As Long

    Ret = ExecCmd("notepad.exe")
    A% = MessageBox(GetWindowtext, "プロセス終了", 0, 2)
End Sub

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