Programming/Visual Basic

실행시킨 프로그램 끝날때까지(종료) 기다렸다 다음 코드 계속 실행하기

bcheul 2008. 6. 30. 11:25

Option Explicit
Private Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   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

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

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, _
                                                        ByVal lpCommandLine As String, _
                                                        lpProcessAttributes As Any, _
                                                        lpThreadAttributes As Any, _
                                                        ByVal bInheritHandles As Long, _
                                                        ByVal dwCreationFlags As Long, _
                                                        ByVal lpEnvironment As Long, _
                                                        ByVal lpCurrentDirectory As Long, _
                                                        lpStartupInfo As Any, _
                                                        lpProcessInformation As Any) As Long


Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Public Sub ExecCmd(cmdline$)
   Dim longRes As Long
   Dim proc As PROCESS_INFORMATION
   Dim start As STARTUPINFO

   ' Initialize the STARTUPINFO structure:
   start.cb = Len(start)

   ' Start the shelled application:
   'Ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
   NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    longRes = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
   NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

   ' Wait for the shelled application to finish:
   longRes = WaitForSingleObject(proc.hProcess, INFINITE)
   longRes = CloseHandle(proc.hProcess)
End Sub

Private Sub Command1_Click()
    ExecCmd "notepad.exe"
   MsgBox "Process Finished"
End Sub