小弟刚学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.是不是一定都要加?不加会怎么样?
感谢~