作者jacky33 (我可能不会爱你)
看板Visual_Basic
标题[VBA ] 请问VBA API相关问题...
时间Mon Mar 2 00:12:09 2015
小弟刚学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://webptt.com/cn.aspx?n=bbs/Visual_Basic/M.1425226333.A.BCE.html
1F:→ MOONRAKER: 你不是刚学 为什麽要看这种变态的code。 03/02 02:40
2F:→ jacky33: 工作需要 03/02 09:13
3F:推 tsongs: 推变态的code VBA很熟API不懂正常 VB一般不会用到这些 03/02 14:37
4F:→ tsongs: 我用api一般都是把变数换成我定义的就完工 03/02 14:38
5F:→ tsongs: 这CASE你有3个地方要改成你的定义 03/02 14:40
6F:→ tsongs: 1. Private Sub Command1_Click() 改成你的执行模式 03/02 14:41
7F:→ tsongs: 2. pid = Shell("C:\Project1.exe", vbNormalFocus) 03/02 14:41
8F:→ tsongs: 改执行档案路径与程式名称 03/02 14:42
9F:→ tsongs: 3. Sheet1.Cells(1, 1) = "done" 这行可以改成呼叫你後续 03/02 14:43
10F:→ tsongs: 我在使用API时会习惯建一个模组叫API 然後COPY过去 03/02 15:04
11F:→ tsongs: 不过只COPY到1.以前 1.以後是要引用到自己写的程序当中 03/02 15:05
12F:→ tsongs: 第一次用API 重点在怎用他 03/02 15:06
13F:→ wenyonba: 这段程式应该是启动project1.exe,并等他跑完,然後在 03/02 21:44
14F:→ wenyonba: Cell里显示已跑完吧??老实说我看不出isDone有啥用途 03/02 21:45
15F:→ wenyonba: DoEvents并不能说是"暂停",而是让程式去处理其他的讯息 03/02 21:46
16F:→ wenyonba: 因为没有看到其他的Code无法知道isDone有何用处,不过宣 03/02 21:51
17F:→ wenyonba: 告成全域或模组级,可能是当控制权由DoEvents转移到其他 03/02 21:52
18F:→ wenyonba: 地方时,那个副程式可以藉由isDone知道project1.exe跑完 03/02 21:54
19F:→ wenyonba: 了没 03/02 21:54
20F:→ wenyonba: 至於为啥是STILL_ALIVE不是STILL_ACTIVE??因为如果是C, 03/02 22:03
21F:→ wenyonba: 直接include正确的header就可以得到STILL_ACTIVE=0x103 03/02 22:05
22F:→ wenyonba: 但是这是VBA,常数你要自己宣告好,所以名称也由你自己定, 03/02 22:05
23F:→ wenyonba: 跟C的header里不一样名字也没差 03/02 22:06
24F:→ wenyonba: 我回答的好像好乱啊XD 03/02 22:07
25F:推 johnpage: Q1:isDone=确定 03/02 22:19
26F:→ johnpage: Q2: STILL_ALIVE=仍然活着 03/02 22:20
27F:→ johnpage: Q3:执行do loop时,没有加 DoEvents程式会显示无回应 03/02 22:22
28F:推 johnpage: Q4:hook 03/02 22:24
29F:→ jacky33: 感谢~ 03/03 00:14