"system" and Windows


(MENON Jean-Francois) #1

“Morris, Chris” wrote:

I have cut & paste the code in my program and it doesn’t work :-<
(in fact now, no command work :-<)

Here it is again copied from the very file I use. Maybe the web rendition
messed up some of the characters (I know it doesn’t render backslashes
correctly for me):

code by Hee-Sob Park - posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10006

def system(command)
Win32API.new(“crtdll”, “system”, [‘P’], ‘L’).Call(command)
end

code by Hee-Sob Park - posted here:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10006

def `(command)
popen = Win32API.new(“crtdll”, “_popen”, [‘P’,‘P’], ‘L’)
pclose = Win32API.new(“crtdll”, “_pclose”, [‘L’], ‘L’)
fread = Win32API.new(“crtdll”, “fread”, [‘P’,‘L’,‘L’,‘L’], ‘L’)
feof = Win32API.new(“crtdll”, “feof”, [‘L’], ‘L’)
saved_stdout = $stdout.clone
psBuffer = " " * 128
rBuffer = ""
f = popen.Call(command,“r”)
while feof.Call( f )==0
l = fread.Call( psBuffer,1,128,f )
rBuffer += psBuffer[0…l]
end
pclose.Call f
$stdout.reopen(saved_stdout)
rBuffer
end

thank you, “system” works very well now.

But I see two potential problems that make not consistent with the
original ruby command:

  1. it takes only one argument
  2. it always set “$?” variable to false
···


/ do you play Go? \

http://jeanfrancois.menon.free.fr/rubygo |
\ /


    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            >>----w |
            >>     >>

(Chris Morris) #2

thank you, “system” works very well now.

But I see two potential problems that make not consistent with the
original ruby command:

  1. it takes only one argument
  2. it always set “$?” variable to false

Ah, I’d not noticed that before. Thanks for bringing that up. If I get a
chance, I’ll see about tweaking it to handle these two things. If anyone
else has this, pls post it, I’d like to use it.

Chris