简体   繁体   English

在lua中使用os.execute时如何避免弹出窗口

[英]how to avoid popup a window when use os.execute in lua

I am using os.execute() to call other program in Lua, when program run,the cmd windows will popup for a blink, and I call the execute more than hundred times, and it become annoying. 我正在使用os.execute()来调用Lua中的其他程序,当程序运行时,cmd窗口会弹出一个闪烁,我调用执行超过一百次,它变得很烦人。 So is there any way to set the window invisible? 那么有没有办法让窗口看不见?

I personally wasn't happy with the "No, you can't" answer and being the Engineer type of guy that just has to "SOLVE THAT PROBLEM", I was able to get things to work using WScript.Shell: 我个人不满意“不,你不能”的答案,并且只是“解决这个问题”的工程师类型,我能够使用WScript.Shell获得工作:

Shell = luacom.CreateObject("WScript.Shell")
Shell:Run (command, 0)

The "0" is used to suppress the popup from occurring. “0”用于抑制弹出窗口的发生。 http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx http://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx

The short answer was given by hjpotter in a comment: no, you can't . 简短的回答是由hjpotter在评论中给出的: 不,你不能

A longer explanation follows. 更长的解释如下。

On Windows executable files come in "two flavors": GUI applications and command line applications . 在Windows上,可执行文件有“两种风格”: GUI应用程序命令行应用程序 This has nothing to do with the inner workings of the program, but it depends on how the program was built (there is a flag for it in the PE executable header which can be set using a linker option). 这与程序的内部工作无关,但它取决于程序的构建方式(在PE可执行文件头中有一个标志,可以使用链接器选项设置)。 It is the OS that automatically pops up a console window (the "ugly black box") when a command line application is executed. 当执行命令行应用程序时,操作系统会自动弹出控制台窗口(“丑陋的黑盒子”)。

The problem with os.execute is that it uses C system function under the hood , which in turn is probably implemented by executing the Windows command shell executable cmd.exe , which is a command line application. os.execute的问题在于它使用了引擎盖下的C system功能 ,而后者又可能通过执行Windows命令shell可执行文件cmd.exe ,这一个命令行应用程序。 Thus every time you use os.execute you are indeed executing cmd.exe . 因此,每次使用os.execute您确实在执行cmd.exe That black box is the console window associated with cmd.exe being executed. 该黑盒子是与正在执行的cmd.exe相关联的控制台窗口。

There is an additional solution I imagined using the alien library. 我想象使用外星人库有一个额外的解决方案。

Alien is a library to access windows dll's with lua in a disciplined way. Alien是一个图书馆,以纪律的方式与lua访问windows dll。 In order to execute a command file without opening a shell window the function ShellExecuteA from the Shell32.dll can be used: its documentation can be found here and a suitable approach is described by the following snippet. 为了在不打开shell窗口的情况下执行命令文件,可以使用Shell32.dll ShellExecuteA函数:可以在此处找到其文档,下面的代码片段描述了一种合适的方法。

require 'alien'
local shell32 = alien.load('Shell32.dll')
shell32.ShellExecuteA:types("pointer","pointer","pointer","pointer","pointer","pointer","int")
local exec = shell32.ShellExecuteA

function osexec(cmd, open)
  if open then
    -- execute opening a window
    exec(0,"open","cmd.exe","/C "..cmd,0,3)
  else
    -- execute, no window opened
    exec(0,"open","cmd.exe","/C "..cmd,0,0)
  end
end

Calling the previous function with the command only, runs it without opening a window, calling with two operands osexec ("dir", true) opens a window and is useful for debugging purposes. 仅使用该命令调用上一个函数,在不打开窗口的情况下运行它,使用两个操作数调用osexec ("dir", true)打开一个窗口,这对于调试非常有用。

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

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