プロセスの起動と終了          <TOP>


CreateProcess プロセスの起動
WaitForSingleObject 指定されたカーネルオブジェクトがシグナル状態になるか、指定された時間が経過するまでスレッドをスリープ
CloseHandle オープンされているオブジェクトハンドルをクローズ

 

'================================================================
'= プロセスの起動と終了
'=    (CreateProcess2.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

#define NORMAL_PRIORITY_CLASS &H20      '通常クラス(一般的なプロセス)
#define INFINITE &HFFFF                 '無限に中断
#define WAIT_TIMEOUT &H102              'タイムアウト時間が経過したことを示す

' プロセスの起動
Declare Function Api_CreateProcess& Lib "Kernel32" Alias "CreateProcessA" (ByVal App&, ByVal Cmd$, ByVal PAttr&, ByVal TAttr&, ByVal InhHandle&, ByVal Flags&, ByVal lpEnv&, ByVal lpCurDir&, StartupInfo As STARTUPINFO, ProcessInfo As PROCESS_INFORMATION)

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

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

Var Shared Text1 As Object
Var Shared Button1 As Object

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

'================================================================
'=
'================================================================
Declare Function ShellWait(cmdline As String, ByVal bShowApp As Integer) As Integer
Function ShellWait(cmdline As String, ByVal bShowApp As Integer) As Integer
    Var pi As PROCESS_INFORMATION
    Var si As STARTUPINFO
    Var Ret As Long

    si.cb = Len(si)
    si.wShowWindow = Abs(bShowApp)
    si.dwFlags = 1

    Text1.SetWindowText "メモ帳起動"
    
    Ret = Api_CreateProcess(0, cmdline, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, si, pi)

    If Ret = 0 Then
        A% = MessageBox("", "起動に失敗しました!", 0, 2)

        ShellWait = False
        Exit Function
    End If

    Text1.SetWindowText "アプリケーションを閉じてください"

    Do While Api_WaitForSingleObject(pi.hProcess, 10) = WAIT_TIMEOUT
        CallEvent
    Loop

    Text1.SetWindowText "アプリケーションは終了しました"

    Ret = Api_CloseHandle(pi.hProcess)

    ShellWait = (Ret <> 0)
End Function

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

    Ret = ShellWait("notepad.exe", True)

    A% = MessageBox("", "メモ帳は終了しました!", 0, 2)
End Sub

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