Re: Problem with popen on windows



I just found a fix that works for me.. I hope this helps someone else out...

Nonetheless, it does require a small vb script, which acts as a
parent for the child script.. to execute, the command is as follows:

[code]
WScript RunHide.vbs Cmd /C MyBatch.cmd
[/code]

The internal source to RunHide.vbs is as follows:
##############################################################################
[code]
' ___________________________________________________________________
'
' VBScript File: RunHide.vbs
' Author: Frank-Peter Schultze
'
' Updates: http://www.fpschultze.de/w5.htm
' Enhancement Req.
' and Bug Reports: support@xxxxxxxxxxxxx
'
' Built/Tested On: Windows 2000 SP2
' Requirements: OS: Windows NT4+
' WSH: 1.0+
'
' Purpose: Create a new process that executes in a hidden
' window.
'
' Syntax: RunHide Command [Arg1 ["Arg 2" [...
'
' State Changes:
'
' Assumptions And
' Limitations:
'
' Last Update: 2004-01-29
' ___________________________________________________________________
'

Option Explicit

On Error Resume Next

Dim oSh, sCmd, i

If Not IsWscript() Then
WScript.Echo "Please run this script using WSCRIPT."
WScript.Quit(1)
End If

If ParseCmdLine(sCmd) Then
Set oSh = CreateObject("WScript.Shell")
oSh.Run Trim(sCmd), 0, False
End If

WScript.Quit(0)


Private Function ParseCmdLine(sCmd)

Dim i

ParseCmdLine = False

If WScript.Arguments.Count > 0 Then
Select Case WScript.Arguments(0)
Case "/?" : ShowUsage(True)
Case "-?" : ShowUsage(True)
Case "/h" : ShowUsage(True)
Case "-h" : ShowUsage(True)
Case "h" : ShowUsage(True)
Case Else : For i = 0 to WScript.Arguments.Count -1
If InStr(WScript.Arguments(i), " ") Then
sCmd = sCmd & " " & Chr(34) &
WScript.Arguments(i) & Chr(34)
Else
sCmd = sCmd & " " & WScript.Arguments(i)
End If
Next
ParseCmdLine = True
End Select
Else
ShowUsage(True)
End If

ParseCmdLine = True

End Function


Private Function IsWscript()

IsWscript = False

If InStrRev(LCase(WScript.FullName), "wscript.exe", -1) Then
IsWscript = True
End If

End Function


Private Sub ShowUsage(bExit)

Dim strHelp

strHelp = "Creates a new process that executes in a hidden window." & _
vbCrLf & vbCrLf & _
"RunHide Command [Arg1 [" & Chr(34) & "Arg 2" & Chr(34) & " [.."
MsgBox strHelp, 64, "RunHide Syntax"

If bExit Then
WScript.Quit(1)
End If

End Sub

[/code]
##############################################################################

Here's the quick test case I wrote (3 files):
c:\temp\dosprompt.rb
c:\temp\RunHide.vbs
c:\temp\test.bat

[dosprompt.rb code]
puts "Hi"
IO.popen("cmd /c c:/temp/test.bat")
sleep 2
puts "bye"
[/dosprompt.rb code]

[test.bat code]
echo hello > c:\asdfxxx.txt
[/test.bat code]

[RunHide.vbs code was previously mentioned above/]

To validate that it did in fact run, just run this from a command
prompt or from start > run
"WScript c:\temp\RunHide.vbs Cmd /C "C:\temp\dosprompt.rb""

To validate it ran ok...
c:\asdfxxx.txt should exist and contain "hello" :)

This seems to easy so.. perhaps I missed something.. if not, I'll be
happy about this tomorrow morning. Let me know if this works out for
you guys..

Credit to RunHide goes to this guy:
http://www.fpschultze.de/w5.htm

thx




On 9/8/05, x1 <caldridge@xxxxxxxxx> wrote:
> I second this and have battled it numerous times. Finally, I've just given
> up on it and execute the job with ruby.exe, and let a dos prompt sit there
> ignorantly.
>
> I've been sitting here hopelessly PRAYING that a fix for this will be
> provided with ruby2.
>
> *waits impatiently.
>
> On 9/8/05, stevetuckner <stevetuckner@xxxxxxxxxxxx> wrote:
> >
> > Kroeger Simon (ext) wrote:
> >
> > >Hi all,
> > >
> > >i'm using ruby 1.8.2 (2004-12-25) [i386-mswin32] on win2000.
> > >
> > >When starting my GUI app with rubyw (because i don't like the empty
> > >window) there is another window poping up whenever i call popen. (or use
> > >backquotes to start another process)
> > >
> > >Using just ruby.exe all seems fine (except the initial nasty empty
> > >window)
> > >
> > >Someone knows a way around?
> > >
> > >cheers
> > >
> > >Simon
> > >
> > >
> > >
> > >
> > >
> > Below was my attempt to say, here is how you should fix this issue. And
> > then it didn't work. If I run it with ruby it works fine. When I run it
> > with rubyw, it just goes away when I click run. Any ideas anyone? The
> > system code and `` code is taken from the list a long time ago.
> >
> > Steve Tuckner
> >
> > ---------------------------------------------
> > require "vr/vruby"
> > require "vr/vrcontrol"
> > require 'Win32API'
> >
> > def system(command)
> > Win32API.new("crtdll", "system", ['P'], 'L').Call(command)
> > end
> >
> > 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
> >
> > class MyForm < VRForm
> >
> > def construct
> > addControl VRButton, "run", "run", 0, 0, 150, 40
> > move 0, 0, 160, 73
> > end
> >
> > def run_clicked
> > messageBox(`dir`)
> > end
> > end
> >
> > VRLocalScreen.showForm MyForm
> > VRLocalScreen.messageloop
> >
> >
> >
> >
>
>


.