Odd "system" command behaviour with CUI and GUI

Windows 7 64-bit, Ruby 1.9.3.

If I create a text file called “RDTlockfile.dat”

And in the same directory, a single line rb file:

system “attrib +h RDTlockfile.dat”

Running this file with Ruby’s CUI interpreter hides the target file, but
running it as an rbw or with the GUI interpreter doesn’t hide the file.

I have tried IO.popen and various other methods to hide this file but so
far have drawn a blank, the Win32API and dl approaches keep giving me
invalid methods or deprecation errors. It works fine using the console
Ruby, but I’m using Tk as a GUI so I don’t want a console window to
appear. It looks like a bug, as I thought the same commands would work
with either interpreter.

Any ideas as to why this happens or how to get around it?

thanks,

Joe

It works for me on Win XP. What is the return value of #system call?

Have you tried using backticks or Kernel#spawn instead?

– Matma R.

Bartosz Dziewoński wrote in post #1069474:

It works for me on Win XP. What is the return value of #system call?

Have you tried using backticks or Kernel#spawn instead?

– Matma R.

Thanks for your response.
Spawn and backticks both work with CUI but not GUI. I’ll try out another
PC and see if it’s my interpreter that’s acting up. I’ll reply with the
results.

Joe

Joel P. wrote in post #1069476:

Bartosz Dziewoński wrote in post #1069474:

It works for me on Win XP. What is the return value of #system call?

Have you tried using backticks or Kernel#spawn instead?

– Matma R.

Thanks for your response.
Spawn and backticks both work with CUI but not GUI. I’ll try out another
PC and see if it’s my interpreter that’s acting up. I’ll reply with the
results.

Joe

It works in Windows XP but fails on both Win7-64 machines I have acess
to. Looks like WinPE platform issue?

Joel P. wrote in post #1069477:

Joel P. wrote in post #1069476:

Bartosz Dziewoński wrote in post #1069474:

It works for me on Win XP. What is the return value of #system call?

Have you tried using backticks or Kernel#spawn instead?

– Matma R.

Thanks for your response.
Spawn and backticks both work with CUI but not GUI. I’ll try out another
PC and see if it’s my interpreter that’s acting up. I’ll reply with the
results.

Joe

It works in Windows XP but fails on both Win7-64 machines I have acess
to. Looks like WinPE platform issue?

I have tried the attrib command on Windows 7 independantly of Ruby and
it works from both the command console and the Run command. I can only
assume that the command isn’t being properly passed by Rubyw.exe on
post-XP Windows systems.

Joel P. wrote in post #1069478:

Joel P. wrote in post #1069477:

Joel P. wrote in post #1069476:

Bartosz Dziewoński wrote in post #1069474:

It works for me on Win XP. What is the return value of #system call?

Have you tried using backticks or Kernel#spawn instead?

– Matma R.

Thanks for your response.
Spawn and backticks both work with CUI but not GUI. I’ll try out another
PC and see if it’s my interpreter that’s acting up. I’ll reply with the
results.

Joe

It works in Windows XP but fails on both Win7-64 machines I have acess
to. Looks like WinPE platform issue?

I have tried the attrib command on Windows 7 independantly of Ruby and
it works from both the command console and the Run command. I can only
assume that the command isn’t being properly passed by Rubyw.exe on
post-XP Windows systems.

Based on further rb/rbw testing on Win7, it looks like GUI doesn’t allow
system commands that would normally be linked the the console, like
attrib or cmd. Notepad launches fine from both of the interpreters.
Interestingly the system command returns true even when it has no
effect.
I disabled User Account Control, and checked that the permissions are
the same for CUI and GUI, no change. It must be something in the exe
itself.

Bartosz Dziewoński wrote in post #1069528:

That’s weird.

As a stopgap solution, have you tried using something like

system 'cmd /c "attrib +h aaa.txt"'

?

– Matma R.

That works with an .rb extension but not .rbw
Seems to be DOS based commands that aren’t working without a console,
and that extends to cmd itself.
Interestingly I do get a flicker of a cmd window running that with
rubyw, but it doesn’t hide the file.
It works if I use command prompt with ruby and run ‘rubyw.exe test.rbw’
from inside the command prompt window.

Joe

That’s weird.

As a stopgap solution, have you tried using something like

system 'cmd /c "attrib +h aaa.txt"'

?

– Matma R.

Am Sat, 21 Jul 2012 04:06:37 +0900
schrieb Joel P. [email protected]:

That works with an .rb extension but not .rbw
Seems to be DOS based commands that aren’t working without a console,
and that extends to cmd itself.
Interestingly I do get a flicker of a cmd window running that with
rubyw, but it doesn’t hide the file.
It works if I use command prompt with ruby and run ‘rubyw.exe
test.rbw’ from inside the command prompt window.

Joe

It’s been a while since I last did some Windows stuff, but if I
remember correctly, to run any external command you need to attach a
console window to a process. ruby.exe does this by default (resulting
in the CMD popup), and rubyw.exe doesn’t do this (causing no popup).
However, this behaviour is inherited by any child processes, so if you
want to run a command from a process not having a console window
attached, you first have to allocate one using the Win32API and then
attach it to your process. I’ve not done this, but AFAIK it’s possible
with the win32-process gem and its wrapper around the CreateProcess()
Win32API function.

Valete,
Marvin


Blog: http://pegasus-alpha.eu/blog

ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign ()

Marvin Gülker wrote in post #1069546:

Am Sat, 21 Jul 2012 04:06:37 +0900
schrieb Joel P. [email protected]:

That works with an .rb extension but not .rbw
Seems to be DOS based commands that aren’t working without a console,
and that extends to cmd itself.
Interestingly I do get a flicker of a cmd window running that with
rubyw, but it doesn’t hide the file.
It works if I use command prompt with ruby and run ‘rubyw.exe
test.rbw’ from inside the command prompt window.

Joe

It’s been a while since I last did some Windows stuff, but if I
remember correctly, to run any external command you need to attach a
console window to a process. ruby.exe does this by default (resulting
in the CMD popup), and rubyw.exe doesn’t do this (causing no popup).
However, this behaviour is inherited by any child processes, so if you
want to run a command from a process not having a console window
attached, you first have to allocate one using the Win32API and then
attach it to your process. I’ve not done this, but AFAIK it’s possible
with the win32-process gem and its wrapper around the CreateProcess()
Win32API function.

Valete,
Marvin


Blog: http://pegasus-alpha.eu/blog

ASCII-Ribbon-Kampagne () | ASCII Ribbon Campaign ()

By that point I may as well be using the windows API to hide the file
directly
:frowning: I’m using OCRA to package this program so that may complicate matters
further.
Process.detach and Thread.new didn’t work properly from an rbw either.

Joel P. wrote in post #1069559:

This workaround seems to work in principle. Not sure how I’m going to be
able to add it with OCRA…

Script1 (rbw)
require ‘win32/process’
Process.create :app_name => “ruby.exe testy2.rb”

Script2 (rb)
system ‘cmd /c “attrib +h aaa.txt”’

1 calls 2 as a different proc and different interpreter. No idea if
that’ll work in a packaged exe though.

Aha, replace Script2 with a bat file written by Script1, and that
approach works without needing ruby.exe

This workaround seems to work in principle. Not sure how I’m going to be
able to add it with OCRA…

Script1 (rbw)
require ‘win32/process’
Process.create :app_name => “ruby.exe testy2.rb”

Script2 (rb)
system ‘cmd /c “attrib +h aaa.txt”’

1 calls 2 as a different proc and different interpreter. No idea if
that’ll work in a packaged exe though.