小弟剛學VBA,有些問題想請高手指教,感謝。
程式碼如下(http://www.programmer-club.com.tw/ShowSameTitleN/vb/28660.html):
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function IsWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const SYNCHRONIZE = &H100000
Const STILL_ALIVE = &H103
Const INFINITE = &HFFFF
Private ExitCode As Long
Private hProcess As Long
Private isDone As Long
Private Sub Command1_Click()
Dim pid As Long
pid = Shell("C:\Project1.exe", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION + SYNCHRONIZE, 0, pid)
isDone = False
Do
Call GetExitCodeProcess(hProcess, ExitCode)
DoEvents
Loop While ExitCode = STILL_ALIVE
Call CloseHandle(hProcess)
isDone = True
Sheet1.Cells(1, 1) = "done"
End Sub
Q1:程式內的isDone是什麼用途??為何一開始宣告它(Private isDone As Long)然後
在之後又isDone = False 和 isDone = True? 用意為何?
Q2:GetExitCodeProcess指的是中止上面執行中(或是執行完)的程式("C:\Project1.exe")嗎??
那定義ExitCode = STILL_ALIVE是什麼意思?為何不是STILL_ACTIVE
(https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx?
Q3:DoEvents是暫停執行,以便讓作業系統可以處理其它的事件。加了這個是為了可以執行
下面的程式Sheet1.Cells(1,1) = "done"嗎??那為何不是加在 Call CloseHandle (hProcess)
之下??
Q4:CloseHandle指的是Closes an open object handle.是不是一定都要加?不加會怎麼樣?
感謝~