VB.netでWin32APIのCreateProcess関数を呼び出す
VB.netでWin32APIのCreateProcess関数を呼び出したのでメモ。 CreateProcess関数でメモ帳を実行する。 WaitForSingleObject関数で、lngWaitForObjectTimeOut に設定した時間が経過するか、メモ帳が閉じられない限り、フォームの操作を受け付けなくする。
Form1.vb
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim strExeFileName As String Dim strCommandLine As String Dim udtProcessAttributes As SECURITY_ATTRIBUTES Dim udtThreadAttributes As SECURITY_ATTRIBUTES Dim strCurrentDriectory As String Dim lngWaitForObjectTimeOut As Integer Dim lngWaitForObjectEvent As Integer Dim lngWin32apiResultCode As Integer Dim si As STARTUPINFO Dim pi As PROCESS_INFORMATION ' 実行可能ファイル名を指定 strExeFileName = "C:\Windows\System32\notepad.exe" ' コマンドラインを指定 strCommandLine = vbNullString ' セキュリティ構造体を初期化 udtProcessAttributes.nLength = Len(udtProcessAttributes) udtThreadAttributes.nLength = Len(udtThreadAttributes) ' カレントディレクトリを指定 strCurrentDriectory = vbNullString ' 新しいプロセスのメインウィンドウの表示状態を指定 si.cb = Len(si) ' 新しいプロセスを作成 lngWin32apiResultCode = CreateProcess(strExeFileName, strCommandLine, udtProcessAttributes, udtThreadAttributes, False, 0, 0, strCurrentDriectory, si, pi) ' 待機するタイムアウト時間を指定 lngWaitForObjectTimeOut = 6000 ' 新しいプロセスがシグナル状態になるまで待機 lngWaitForObjectEvent = WaitForSingleObject(pi.hProcess, lngWaitForObjectTimeOut) ' 待機結果を表示 Select Case lngWaitForObjectEvent Case STATUS_WAIT_0 Label4.Text = "プロセスは終了しました。" Case WAIT_TIMEOUT Label4.Text = "待機はタイムアウトしました。" Case WAIT_FAILED Label4.Text = "待機は失敗しました。" End Select End Sub End Class
Module1.vb
Option Explicit On Module Module1 ' セキュリティ属性に関する情報を定義する構造体 Structure SECURITY_ATTRIBUTES Dim nLength As Integer Dim lpSecurityDescriptor As Integer Dim bInheritHandle As Integer End Structure ' 新しいプロセスのメインウィンドウの表示状態を定義する構造体 Structure STARTUPINFO Dim cb As Integer Dim lpReserved As Integer Dim lpDesktop As Integer Dim lpTitle As Integer Dim dwX As Integer Dim dwY As Integer Dim dwXSize As Integer Dim dwYSize As Integer Dim dwXCountChars As Integer Dim dwYCountChars As Integer Dim dwFillAttribute As Integer Dim dwFlags As Integer Dim wShowWindow As Short Dim cbReserved2 As Short Dim lpReserved2 As Byte Dim hStdInput As Integer Dim hStdOutput As Integer Dim hStdError As Integer End Structure Structure PROCESS_INFORMATION Public hProcess As Integer Public hThread As Integer Public dwProcessId As Integer Public dwThreadId As Integer End Structure Declare Function CreateProcess Lib "kernel32.dll" _ Alias "CreateProcessA" ( ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByRef lpProcessAttributes As SECURITY_ATTRIBUTES, ByRef lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Integer, ByVal dwCreationFlags As Integer, ByVal lpEnvironment As Byte, ByVal lpCurrentDriectory As String, ByRef lpStartupInfo As STARTUPINFO, ByRef lpProcessInformation As PROCESS_INFORMATION) As Integer ' オブジェクトの状態がシグナル状態あるいはタイムアウト時間が ' 経過するまで待機する関数の宣言 Declare Function WaitForSingleObject Lib "kernel32.dll" _ (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer Public Const STATUS_WAIT_0 = &H0& 'ublic Const STATUS_ABANDONED_WAIT_0 = &H80& Public Const STATUS_TIMEOUT = &H102& 'ublic Const WAIT_OBJECT_0 = ((STATUS_WAIT_0) + 0) 'ublic Const WAIT_ABANDONED = ((STATUS_ABANDONED_WAIT_0) + 0) Public Const WAIT_TIMEOUT = STATUS_TIMEOUT Public Const WAIT_FAILED = &HFFFFFFFF 'ublic Const INFINITE = &HFFFFFFFF End Module