How do I call a Windows shell command using VB6?

前端 未结 6 701
无人及你
无人及你 2020-11-30 10:31

How exactly using VB6 can I can call any Windows shell command as you would from the command-line?

For example, something as trivial as:

echo foo
         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 11:05

    I've always used the Run method of the wshShell object, which is available after you reference the Windows Script Host Object Model in your project:

    Dim shell As wshShell
    Dim lngReturnCode As Long
    Dim strShellCommand As String
    
    Set shell = New wshShell
    
    strShellCommand = "C:\Program Files\My Company\MyProg.exe " & _
       "-Ffoption -Ggoption"
    
    lngReturnCode = shell.Run(strShellCommand, vbNormalFocus, vbTrue)
    

    You get the same functionality as the normal Shell statement, but the final parameter lets you decide whether to run the shelled program synchronously. The above call, with vbTrue, is synchronous. Using vbFalse starts the program asynchronously.

    And, as noted in previous answers, you need to run the command shell with the "/c" switch to execute internal commands, like the "echo foo" from your question. You'd send "cmd /c echo foo" to the Run method.

提交回复
热议问题