感谢您使用微软产品。vb中shell命令调用外部程序后就立即返回原程序,外部程序关闭时原程序无法得到相应的消息。因此,建议您使用CreateProcess启动外部程序并创建进程,再调用WaitForSingleObject获得程序关闭的消息,此时您可以用API函数GetSystemTime或GetLocalTime返回的SYSTEMTIME结构的参数获得系统时间。
如下例:
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePrivate 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 Type Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, 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 String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) 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 Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO ' Initialize the STARTUPINFO structure:
start.cb = Len(start) ' Start the shelled application:
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc) ' Wait for the shelled application to finish:
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0) ' Not necessarily0, you can set a value like 100 as well
DoEvents
Loop Until ReturnValue = 0 Call GetExitCodeProcess(proc.hProcess, ReturnValue)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret& End FunctionPrivate Sub Command1_Click()
MsgBox Text1.Text
End SubPrivate Sub Command2_Click()
Dim s As String
Dim sys As SYSTEMTIMEGetLocalTime sys
s1 = CStr(sys.wHour) + ":" + CStr(sys.wMinute) + ":" + CStr(sys.wSecond) + ":" + CStr(sys.wMilliseconds)
Label1.Caption = s1
Dim retval As Long
retval = ExecCmd("notepad.exe")
GetLocalTime sys
s1 = CStr(sys.wHour) + ":" + CStr(sys.wMinute) + ":" + CStr(sys.wSecond) + ":" + CStr(sys.wMilliseconds)
Label2.Caption = s1
MsgBox "process finished"
End Sub下面文章中包含相似的例子代码,但是原程序在外部程序关闭前被挂起。
Q129796 HOWTO: 32-Bit App Can Determine When a Shelled Process Ends
http://support.microsoft.com/support/kb/articles/q129/7/96.asp详细信息请参考以下链接:
GetSystemTime
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_56p1.asp
GetLocalTime
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_7rj9.asp- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
如下例:
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
Private Declare Sub GetLocalTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePrivate 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 Type Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As String, 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 String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) 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 Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO ' Initialize the STARTUPINFO structure:
start.cb = Len(start) ' Start the shelled application:
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc) ' Wait for the shelled application to finish:
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0) ' Not necessarily0, you can set a value like 100 as well
DoEvents
Loop Until ReturnValue = 0 Call GetExitCodeProcess(proc.hProcess, ReturnValue)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret& End FunctionPrivate Sub Command1_Click()
MsgBox Text1.Text
End SubPrivate Sub Command2_Click()
Dim s As String
Dim sys As SYSTEMTIMEGetLocalTime sys
s1 = CStr(sys.wHour) + ":" + CStr(sys.wMinute) + ":" + CStr(sys.wSecond) + ":" + CStr(sys.wMilliseconds)
Label1.Caption = s1
Dim retval As Long
retval = ExecCmd("notepad.exe")
GetLocalTime sys
s1 = CStr(sys.wHour) + ":" + CStr(sys.wMinute) + ":" + CStr(sys.wSecond) + ":" + CStr(sys.wMilliseconds)
Label2.Caption = s1
MsgBox "process finished"
End Sub下面文章中包含相似的例子代码,但是原程序在外部程序关闭前被挂起。
Q129796 HOWTO: 32-Bit App Can Determine When a Shelled Process Ends
http://support.microsoft.com/support/kb/articles/q129/7/96.asp详细信息请参考以下链接:
GetSystemTime
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_56p1.asp
GetLocalTime
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/time_7rj9.asp- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货