如何使用 VB6 调用 Windows shell 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/290899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:43:24  来源:igfitidea点击:

How do I call a Windows shell command using VB6?

windowscommand-linevb6

提问by Ray

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

我如何准确地使用 VB6 我可以像从命令行一样调用任何 Windows shell 命令?

For example, something as trivial as:

例如,一些微不足道的事情:

echo foo

回答by dub

Here's how you do it :

这是你如何做到的:

Shell "cmd echo foo", vbNormalFocus 

回答by JeffK

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:

我一直使用 wshShell 对象的 Run 方法,在您的项目中引用 Windows 脚本宿主对象模型后即可使用该方法:

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.

您将获得与普通 Shell 语句相同的功能,但最后一个参数让您决定是否同步运行外壳程序。上面使用 vbTrue 的调用是同步的。使用 vbFalse 异步启动程序。

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.

而且,如之前的答案所述,您需要使用“/c”开关运行命令外壳以执行内部命令,例如您问题中的“echo foo”。您将“cmd /c echo foo”发送到 Run 方法。

回答by Ed Guiness

Shell and ShellExecute?

Shell和ShellExecute?

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

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

Dim RetVal
RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)   ' Run Calculator.

回答by Eugenio Miró

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.

如果您希望支持像 windows 9x 或 me 这样的早期系统,您应该考虑扩展 COMSPEC 环境变量。

You can also obtain the process id using

您还可以使用

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

回答by raven

Shell "cmd /c echo foo"

回答by CerFyxz

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.

因为第一个"被关闭"C:\...并且字符串会停止。