[VBA ] 請問VBA API相關問題...
小弟剛學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.是不是一定都要加?不加會怎麼樣?
感謝~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.141.129.209
※ 文章網址: https://www.ptt.cc/bbs/Visual_Basic/M.1425226333.A.BCE.html
→
03/02 02:40, , 1F
03/02 02:40, 1F
→
03/02 09:13, , 2F
03/02 09:13, 2F
推
03/02 14:37, , 3F
03/02 14:37, 3F
→
03/02 14:38, , 4F
03/02 14:38, 4F
→
03/02 14:40, , 5F
03/02 14:40, 5F
→
03/02 14:41, , 6F
03/02 14:41, 6F
→
03/02 14:41, , 7F
03/02 14:41, 7F
→
03/02 14:42, , 8F
03/02 14:42, 8F
→
03/02 14:43, , 9F
03/02 14:43, 9F
→
03/02 15:04, , 10F
03/02 15:04, 10F
→
03/02 15:05, , 11F
03/02 15:05, 11F
→
03/02 15:06, , 12F
03/02 15:06, 12F
→
03/02 21:44, , 13F
03/02 21:44, 13F
→
03/02 21:45, , 14F
03/02 21:45, 14F
→
03/02 21:46, , 15F
03/02 21:46, 15F
→
03/02 21:51, , 16F
03/02 21:51, 16F
→
03/02 21:52, , 17F
03/02 21:52, 17F
→
03/02 21:54, , 18F
03/02 21:54, 18F
→
03/02 21:54, , 19F
03/02 21:54, 19F
→
03/02 22:03, , 20F
03/02 22:03, 20F
→
03/02 22:05, , 21F
03/02 22:05, 21F
→
03/02 22:05, , 22F
03/02 22:05, 22F
→
03/02 22:06, , 23F
03/02 22:06, 23F
→
03/02 22:07, , 24F
03/02 22:07, 24F
推
03/02 22:19, , 25F
03/02 22:19, 25F
→
03/02 22:20, , 26F
03/02 22:20, 26F
→
03/02 22:22, , 27F
03/02 22:22, 27F
推
03/02 22:24, , 28F
03/02 22:24, 28F
→
03/03 00:14, , 29F
03/03 00:14, 29F
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章