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.

lucaortis
  • 430
  • 2
  • 11
  • Can you try to rename your script to `efdsk.rbw` and "compile" it again with ocra? – knut May 05 '18 at 18:21
  • @knut I did this right now, still the same issue. I click the button and the cmd appears for less then a second, but i can see that there is no output on it – lucaortis May 05 '18 at 18:25
  • OK, then it seems to be something else. Can you open a cmd-shell (Command cmd) and start the exe from the command shell? Then you could see a message, even if the script close immediate after the start. – knut May 05 '18 at 20:22
  • @knut i tried this too. But the console is still empty. I also tried to do 'start efdsk.exe > log.txt' but then the file is empty. If i remove from my script the 'system()' or 'popup3' methods, so this issue will be solved. But i really need them – lucaortis May 05 '18 at 22:09

1 Answers1

3

I solved by myself.

First, i required this in my script:

require 'win32ole'

Then i made a batch file, containing the command i need to be printed on a file, in this case ip config > info.txt, and i named it run.bat.

After this, I replaced system() with the following:

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

This will work too:

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

Here the documentation for win32ole

As first parameter i used the batch file i just created, and the last parameter, the 0, made the trick. It sets the new cmd window to be hided and so it wont ever pop up again.

I tried with rubyw efdsk.rb and also to build an exe with ocra. No annoying windows popping up.

lucaortis
  • 430
  • 2
  • 11