see this, use execcmd( exe file name ) to run the file.Option ExplicitPrivate 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 TypePrivate Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessID As Long dwThreadID As Long End TypePrivate Declare Function dcWaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function dcCreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal _ lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _ lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _ lpStartupInfo As STARTUPINFO, lpProcessInformation As _ PROCESS_INFORMATION) As LongPrivate Declare Function dcCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As LongPrivate Declare Function dcGetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As Long Public Declare Function dcTerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Const NORMAL_PRIORITY_CLASS = &H20& Private Const INFINITE = -1& Const WAIT_TIMEOUT As Long = &H102Public Function ExecCmd(cmdline$) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO Dim ret As Long Dim enAllFail As Long
On Error GoTo errExit
' Initialize the STARTUPINFO structure: start.cb = Len(start)
' Start the shelled application: ret = dcCreateProcess(0&, cmdline$, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish: ret = dcWaitForSingleObject(proc.hProcess, INFINITE) If ret = WAIT_TIMEOUT Then 'After 15 min program may be hung? Call dcTerminateProcess(proc.hProcess, enAllFail) End If Call dcGetExitCodeProcess(proc.hProcess, ret&) Call dcCloseHandle(proc.hProcess) ExecCmd = ret& Exit Function errExit: 'Error handler here
End Function
希望某一 VB 程序利用 Shell 执行某一个外部程序(假设是 notepad.exe)之后, 就一直等到此一程序结束执行时, 才回到 VB 程序继续执行, 该怎么办到呢? 当我们调用 Shell 时, 会传回一个数值, 此一数值称为 Process Id, 利用此 一 Process Id, 我们可以调用 OpenProcess API 取得 Process Handle, 然后 再利用 Process Handle 调用 WaitForSingleObject, 即可等待被 Shell 执行的 程序执行完毕, 才继续向下执行。细节如下: 1. API 的声明: Const SYNCHRONIZE = &H100000 Const INFINITE = &HFFFFFFFF Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long 注:如果以上的声明放在「一般模块」底下, 应将 Declare 之前的 Private 保 留字去掉, 并且在 Const 之前加上 Public 保留字。 2. 程序范例:(以执行 Notepad 程序为例) Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变 数 pId = Shell("Notepad", vbNormalFocus) ' Shell 传回 Process Id pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle If pHnd <> 0 Then Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束 Call CloseHandle(pHnd) End If
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 TypePrivate Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End TypePrivate Declare Function dcWaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function dcCreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As LongPrivate Declare Function dcCloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As LongPrivate Declare Function dcGetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function dcTerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Const WAIT_TIMEOUT As Long = &H102Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
Dim enAllFail As Long
On Error GoTo errExit
' Initialize the STARTUPINFO structure:
start.cb = Len(start)
' Start the shelled application:
ret = dcCreateProcess(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret = dcWaitForSingleObject(proc.hProcess, INFINITE)
If ret = WAIT_TIMEOUT Then
'After 15 min program may be hung?
Call dcTerminateProcess(proc.hProcess, enAllFail)
End If
Call dcGetExitCodeProcess(proc.hProcess, ret&)
Call dcCloseHandle(proc.hProcess)
ExecCmd = ret&
Exit Function
errExit:
'Error handler here
End Function
就一直等到此一程序结束执行时, 才回到 VB 程序继续执行, 该怎么办到呢?
当我们调用 Shell 时, 会传回一个数值, 此一数值称为 Process Id, 利用此
一 Process Id, 我们可以调用 OpenProcess API 取得 Process Handle, 然后
再利用 Process Handle 调用 WaitForSingleObject, 即可等待被 Shell 执行的
程序执行完毕, 才继续向下执行。细节如下:
1. API 的声明:
Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFFFFFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal
dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId
As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal
hHandle As Long, ByVal dwMilliseconds As Long) As Long
注:如果以上的声明放在「一般模块」底下, 应将 Declare 之前的 Private 保
留字去掉, 并且在 Const 之前加上 Public 保留字。
2. 程序范例:(以执行 Notepad 程序为例)
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变
数
pId = Shell("Notepad", vbNormalFocus) ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd)
End If