在 windows 中使用 lua os.execute 来启动一个程序,不需要一闪而过的 CMD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6362841/
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
Use lua os.execute in windows to launch a program with out a flash of CMD
提问by Jane T
I am happily launching a program in a windows system from Lua using
我很高兴使用 Lua 在 Windows 系统中启动一个程序
strProgram = '"C:\Program Files\Ps Pad\PSPad.exe"'
strCmd = 'start "" '..strProgram
os.execute(strCmd)
This works correctly, launching the program and the script finishing. How ever it flashes up a command window for a fraction of a second, does any one have a way from Lua to launch a program.
这工作正常,启动程序和脚本完成。它如何闪现一个命令窗口几分之一秒,有没有人有办法从 Lua 启动程序。
采纳答案by Nicol Bolas
Lua's os.execute command is based on the C standard library "shell" function. In Windows, this function will always create a command window, and it will always halt your current process until the window finishes. The latter also happens in Linux.
Lua 的 os.execute 命令基于 C 标准库“shell”函数。在 Windows 中,此函数将始终创建一个命令窗口,并且它始终会停止您当前的进程,直到该窗口结束。后者也发生在 Linux 中。
There is ultimately no way around this. Not through the Lua standard API. Because Lua needs to be light-weight and platform independent, the API is not allowed to use OS-dependent native APIs.
最终没有办法解决这个问题。不是通过 Lua 标准 API。由于 Lua 需要轻量级和平台无关,API 不允许使用依赖于 OS 的原生 API。
Your best bet would be to use the Lua Ex-Apimodule. It is effectively abandonware, and you may need to patch up a few compiler issues (I'm guessing the Windows port wasn't their first priority). But it is a reasonably good way to spawn processes. You can choose to wait until it finishes yourself, or let them run in parallel. And it won't throw up a command prompt window, unless the application itself uses one.
您最好的选择是使用Lua Ex-Api模块。它实际上是废弃软件,您可能需要修补一些编译器问题(我猜 Windows 端口不是他们的首要任务)。但这是产生进程的一种相当好的方式。您可以选择等到它自己完成,或者让它们并行运行。它不会弹出命令提示符窗口,除非应用程序本身使用一个。
回答by Franco Properzi
This is the piece of code I use to call a batch from Lua, maybe help. In win console (command prompt) open and execute, same in unix (mac|nix)
这是我用来从 Lua 调用批处理的一段代码,也许有帮助。在 win 控制台(命令提示符)中打开并执行,在 unix (mac|nix) 中相同
-- sBatchFile = .bat for windows, .sh for x
function vfFork2(sBatchFile)
local b = package.cpath:match("%p[\|/]?%p(%a+)")
if b == "dll" then
-- windows
os.execute('start cmd /k call "'..sBatchFile..'"')
elseif b == "dylib" then
-- macos
os.execute('chmod +x "'..sBatchFile..'"')
os.execute('open -a Terminal.app "'..sBatchFile..'"')
elseif b == "so" then
-- Linux
os.execute('chmod +x "'..sBatchFile..'"')
os.execute('xterm -hold -e "'..sBatchFile..'" & ')
end
end
回答by Tamás Szelei
This is a way to run a command without a console window using only the Lua standard API (i.e. no extra libraries). Tested on Win7 x64.
这是一种仅使用 Lua 标准 API(即没有额外的库)在没有控制台窗口的情况下运行命令的方法。在 Win7 x64 上测试。
function exec_silent(command)
local p = assert(io.popen(command))
local result = p:read("*all")
p:close()
return result
end
Edit: see the comments below, it might not work for everyone. I'm not sure why.
编辑:请参阅下面的评论,它可能不适用于所有人。我不知道为什么。