Page 1 of 1

Capturing program output

Posted: Thu Oct 04, 2012 7:53 pm
by StanDan
I'm doing something that you would think would be blindingly simple, yet I'm tripping all over myself finding an answer. How can I pop up a window asking a user for a value and then capture that from STDOUT? This works on Linux and Mac but not Windows.

Code: Select all

OpenConsole()
If OpenWindow(0, 100,100,200, 200, "Best power ranger?")
  StringGadget(1, 10, 10, 180, 140, "")
  ButtonGadget(2, 10, 150, 180, 30, "Done")
EndIf

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget
    If EventGadget() = 2
      PrintN( GetGadgetText(1) )
      End
    EndIf
  EndIf
Until Event = #PB_Event_CloseWindow
What I want to be able to do is make a simple little pop-up which asks for something from a scripting language. Like so:

Code: Select all

$variable = `demo.exe`
It seems like it loses it's connection to the console when I run the program and I don't get the output. Also, I don't really need the console window so I'm hiding it with ShowWindow_() and GetConsoleWindow_(). I'm feeling pretty sad about the prospects of this working. Is it possible?

Many thanks in advance.

Re: Capturing program output

Posted: Thu Oct 04, 2012 7:59 pm
by Sirius-2337
The PB Help for PrintN() says
To output raw data on the non-graphical console (for pipe communication) WriteConsoleData() can be used.

Re: Capturing program output

Posted: Thu Oct 04, 2012 8:52 pm
by StanDan
Thank you. I'm not sure how I missed that.

Re: Capturing program output

Posted: Thu Oct 04, 2012 9:37 pm
by StanDan
Yikes, still doesn't work.

Code: Select all

str.s = "Whooolly Booly"

WriteConsoleData(@str, Len(str))
With the above code compiled with either Windows or Console set, I get the following.

Code: Select all

C:\Users\jonathan\dev\Citation>perl -le "print `build\test.exe`"


C:\Users\jonathan\dev\Citation>ruby -e "puts `build\\test.exe`"


C:\Users\jonathan\dev\Citation>build\test.exe

Is the bug in PureBasic... or my poor brain?

Re: Capturing program output

Posted: Thu Oct 04, 2012 10:06 pm
by StanDan
Finally got it, it was missing OpenConsole(), which is required for it to work but there's no warning about it being needed.

Code: Select all

str.s = "Whooolly Booly"
OpenConsole()
WriteConsoleData(@str, Len(str))
This works. Thank you guys!