简体   繁体   English

在Windows中使用lua os.execute启动没有CMD刷新的程序

[英]Use lua os.execute in windows to launch a program with out a flash of CMD

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中启动程序?

Lua's os.execute command is based on the C standard library "shell" function. Lua的os.execute命令基于C标准库的“ shell”功能。 In Windows, this function will always create a command window, and it will always halt your current process until the window finishes. 在Windows中,此函数将始终创建命令窗口,并且将始终停止当前进程,直到该窗口结束。 The latter also happens in Linux. 后者也发生在Linux中。

There is ultimately no way around this. 最终,这没有办法。 Not through the Lua standard API. 不通过Lua标准API。 Because Lua needs to be light-weight and platform independent, the API is not allowed to use OS-dependent native APIs. 由于Lua必须轻巧且不依赖平台,因此该API不允许使用与操作系统有关的本机API。

Your best bet would be to use the Lua Ex-Api module. 最好的选择是使用Lua Ex-Api模块。 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). 它实际上是废弃软件,您可能需要修补一些编译器问题(我猜Windows端口并不是他们的首要任务)。 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. 除非应用程序本身使用一个,否则它不会抛出命令提示符窗口。

This is the piece of code I use to call a batch from Lua, maybe help. 这是我用来从Lua调用批处理的代码段,也许有帮助。 In win console (command prompt) open and execute, same in unix (mac|nix) 在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 

This is a way to run a command without a console window using only the Lua standard API (ie no extra libraries). 这是一种仅使用Lua标准API(即没有额外的库)而无需控制台窗口即可运行命令的方法。 Tested on Win7 x64. 在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. 我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM