2

After this: ruby executable won't start,

Now i have another issue with the following simple script:

require 'tk'
require 'tkextlib/tile'

root = TkRoot.new() 

button = Tk::Tile::TButton.new(root) {text "Hello World"}.grid
button.command {system("ipconfig > info.txt")}

Tk.mainloop()  

If i click the Hello World button, so the Windows cmd console will pop up for less then one second. No outputs on it, i also tried to redirect its output to a file and its empty.

If i run my scprit like this:

G:\WinRuby\efdsk>ruby efdsk.rb

There are no problems.

So this issue will appear when i run my exe built with ocra, and also when i run my script like this:

rubyw efdsk.rb  

If i comment the following line,

button.command {system("ipconfig > info.txt")}

The issue will disappear, so i think it's something related to system(). I also tried to replace the previous line with this:

cmd="ipconfig > info.txt"
Open3.popen3(cmd) {|stdin, stdout, stderr, wait_thr|}

But the cmd will appear the same when i click the button.

This is my ruby version:

ruby 2.3.3p222 (2016-11-21 revision 56859) [x64-mingw32]

And this issue will appear on the following ruby versions too, tested by me:

ruby 2.1.5p273 (2014-11-13 revision 48405) [x64-mingw32]
ruby 2.0.0p648 (2015-12-16) [x64-mingw32]
ruby 1.9.3p551 (2014-11-13) [i386-mingw32]

I tried to run the exe and to compile it on other pcs too, Windows 7 and 10. Still the same issue.

4

1 回答 1

2

我自己解决了。

首先,我在脚本中要求这样做:

require 'win32ole'

然后我制作了一个批处理文件,其中包含我需要在文件上打印的命令,在这种情况下ip config > info.txt,我将其命名为run.bat.

在此之后,我替换system()为以下内容:

WIN32OLE.new('Shell.Application').ShellExecute('run.bat','','','open',0)

这也可以:

WIN32OLE.new('WScript.Shell').Run("run.bat",0,0)

这里是win32ole的文档

作为第一个参数,我使用了我刚刚创建的批处理文件,最后一个参数 0 成功了。它将新的 cmd 窗口设置为隐藏,因此它不会再次弹出。

我尝试使用rubyw efdsk.rb并使用 ocra 构建一个 exe。没有烦人的窗口弹出。

于 2018-05-06T08:20:13.687 回答