调用cmd的命令行程序也是小概率事件,遇到了就变得很重要,本来以为和启动扑通的windows应用程序一样有个简单的命令(例如:Call Win32API.WinExec("notepad.exe", SW_SHOWNORMAL) )就能搞定,结果发现比我想象的复杂多了。这段代码不难,说老实话也没必要去琢磨啥意思,直接调用函数就好了。
'执行cmd的命令,函数调用了readTillChar
'调用方法举例:
'strCmd=ProjectSuite.Path & "\testapp.exe PARAM1"
‘strCmd="dir"
'Call ExecCmd(strCmd)
Sub ExecCmd(strCmd)
Dim WshShellObj, WshShellExecObj, out
Set WshShellObj = CreateObject("WScript.Shell")
Set WshShellExecObj = WshShellObj.Exec("cmd.exe")
' Flush the stream
out = readTillChar(WshShellExecObj, ">")
Log.Message(out)
' Send the command and the new line character
WshShellExecObj.StdIn.Write(strCmd & VbCrLf)
out = readTillChar(WshShellExecObj, ">")
Log.Message(out)
End Sub
Function readTillChar(WshShellExecObj, endChar)
Dim out, curChar
Do While Not WshShellExecObj.StdOut.AtEndOfStream
curChar = WshShellExecObj.StdOut.Read(1)
out = out + curChar
If (curChar = endChar) Then
readTillChar = out
Exit Function
End If
Loop
End Function
附件:
http://download.csdn.net/source/3515481
内含:手把手教你TestComplete_调用cmd命令行程序.txt