Public Function ShellForWait(sAppName As String, Optional ByVal lShowWindow As VbAppWinStyle = vbMinimizedFocus, Optional ByVal lWaitTime As Long = 0) As Boolean
Dim lID As Long, lHnd As Long, lRet As Long
On Error Resume Next
lID = Shell(sAppName, lShowWindow)
If lID > 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lID)
If lHnd <> 0 Then
Do
lRet = WaitForSingleObject(lHnd, lWaitTime)
DoEvents
Loop While lRet = WAIT_TIMEOUT
CloseHandle lHnd
ShellForWait = True
Else
ShellForWait = False
End If
Else
ShellForWait = False
End If
End Function
Private Sub Command1_Click()
ShellForWait "cmd /c net view >c:\a.txt",,&HFFFF
Open "c:\a.txt" For Input As #1
While Not EOF(1)
Line Input #1, iStr
Debug.Print iStr
Wend
Close #1
End Sub
Shell "net view > c:\a.txt", vbHide --将执行结果输出到c:\a.txt中
Open "c:\a.txt" For Input As #1 --从c:\a.txt文件中读取结果
While Not EOF(1)
Line Input #1, iStr
Debug.Print iStr
Wend
Close #1
End Sub