主题:shell调用 实时错误 5 无效的过程调用或参数
运行的时候,在这一句出错 pid = Shell(sFilePath)
有时候运行没有问题,有时候又出现问题,到底是怎么回事哦?
Private Sub Command1_Click()
Dim strPath As String, sFilePath As String
strPath = App.Path
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
'接上
sFilePath = strPath & "\Console1.exe"
RunProgramWaitExit sFilePath
End Sub
Private Function ShellEx(sFilePath As String, Optional wsWindowStyle As VbAppWinStyle = vbNormalFocus) As Long
Dim sDriver As String, sDir As String
sDriver = Left(sFilePath, 3)
sDir = Left(sFilePath, InStrRev(sFilePath, "\") - 1)
ChDrive sDriver '切换当前驱动器(盘)
ChDir sDir '切换当前目录
ShellEx = Shell(sFilePath, 0)
End Function
Private Sub RunProgramWaitExit(sFilePath As String)
Dim nExitCode As Long
Dim pid As Long, hProcess As Long
pid = Shell(sFilePath)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
nExitCode = STILL_ACTIVE
Do While nExitCode = STILL_ACTIVE
Call GetExitCodeProcess(hProcess, nExitCode)
DoEvents
Loop
Call CloseHandle(hProcess)
End Sub
有时候运行没有问题,有时候又出现问题,到底是怎么回事哦?
Private Sub Command1_Click()
Dim strPath As String, sFilePath As String
strPath = App.Path
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
'接上
sFilePath = strPath & "\Console1.exe"
RunProgramWaitExit sFilePath
End Sub
Private Function ShellEx(sFilePath As String, Optional wsWindowStyle As VbAppWinStyle = vbNormalFocus) As Long
Dim sDriver As String, sDir As String
sDriver = Left(sFilePath, 3)
sDir = Left(sFilePath, InStrRev(sFilePath, "\") - 1)
ChDrive sDriver '切换当前驱动器(盘)
ChDir sDir '切换当前目录
ShellEx = Shell(sFilePath, 0)
End Function
Private Sub RunProgramWaitExit(sFilePath As String)
Dim nExitCode As Long
Dim pid As Long, hProcess As Long
pid = Shell(sFilePath)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)
nExitCode = STILL_ACTIVE
Do While nExitCode = STILL_ACTIVE
Call GetExitCodeProcess(hProcess, nExitCode)
DoEvents
Loop
Call CloseHandle(hProcess)
End Sub