How do I call a Windows shell command using VB6?

2019-01-09 07:20发布

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条回答
闹够了就滚
2楼-- · 2019-01-09 07:52

Only use double quotes: ""...""

Example - send confirmation pass to make a task:

shell (""echo pass|schtasks /create /TR "C:\folder\...\program.exe" /more_parameters"")

because the first " are closed in "C:\... and the string would stop.


Ahora explico en Español
Solo usa doble comillas: ""...""

Ejemplo - mando un pass para confirmar la creacion de la tarea:

shell (""echo pass|schtasks /create /TR "C:\folder\...\program.exe" /more_parameters"")

la causa es que la primera comillas " se cierra con las comillas de la ruta "C:\... y se pierde la cadena String.

:) Espero sirva y buena suerte

查看更多
相关推荐>>
3楼-- · 2019-01-09 07:56

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.

查看更多
你好瞎i
4楼-- · 2019-01-09 08:01

a combination of all

Shell Environ("COMSPEC") & " /c echo foo", vbNormalFocus

you should think in expanding COMSPEC environment variable if you wish to support earlier systems like windows 9x or me.

You can also obtain the process id using

pid = Shell(Environ("COMSPEC") & " /c echo foo", vbNormalFocus)
查看更多
Viruses.
5楼-- · 2019-01-09 08:03

Here's how you do it :

Shell "cmd echo foo", vbNormalFocus 
查看更多
forever°为你锁心
6楼-- · 2019-01-09 08:04
Shell "cmd /c echo foo"
查看更多
何必那么认真
7楼-- · 2019-01-09 08:11

Shell and ShellExecute?

http://msdn.microsoft.com/en-us/library/aa242087.aspx

Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)   ' Run Calculator.
查看更多
登录 后发表回答