Output wxInputStream in console Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Output wxInputStream in console

Post by mathieumg »

The following code runs ok:

Code: Select all

wxProcess externalProcess;
wxInputStream *consoleOutput;

externalProcess.Redirect();
externalProcess.Open(wxT("psgetsid.exe Administrator"), wxEXEC_ASYNC);

consoleOutput = externalProcess.GetInputStream();
But when I try to read the program output with the following code (inspired by the 'exec' sample), the program simply crashes:

Code: Select all

while ( consoleOutput->CanRead() )
{
    wxChar buffer[4096];
    buffer[consoleOutput->Read(buffer, WXSIZEOF(buffer) - 1).LastRead()] = _T('\0');

    cout << buffer << endl;
}
I'm not sure about the cout << buffer part, but the program crashes even if I comment this line out. Help would be very welcome! If you know another way to translate from username to SID or vice-versa under MSW, it could work too :P

Thanks!
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Please use a debugger to find where it crashes

Also, check the value of

Code: Select all

consoleOutput->Read(buffer, WXSIZEOF(buffer) - 1).LastRead()
is the value fine (within bounds)?
"Keyboard not detected. Press F1 to continue"
-- Windows
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

Code::Blocks doesn't come with a debugger (afaik), do you have a good suggestion? Also, I've tried to dumb down the code as much as possible but it still crashes:

Code: Select all

wxProcess externalProcess;
wxInputStream *consoleOutput;
wxChar buffer[4096];

externalProcess.Redirect();
externalProcess.Open(wxT("cmd"), wxEXEC_ASYNC);
consoleOutput = externalProcess.GetInputStream();
consoleOutput->Read(buffer, 256);
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Yes Code::Blocks has debugger support.

http://wiki.codeblocks.org/index.php?ti ... de::Blocks


PS: check the return value of GetInputStream(), the docs say it may be NULL.
"Keyboard not detected. Press F1 to continue"
-- Windows
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

Thanks for the info Auria! I have tried different things but I seem to be regressing.

The following code:

Code: Select all

wxProcess externalProcess;
wxInputStream *consoleOutput;

externalProcess.Redirect();
externalProcess.Open(wxT("ipconfig.exe"));
consoleOutput = externalProcess.GetInputStream();

cout << externalProcess.IsInputAvailable() << endl;
cout << externalProcess.IsInputOpened() << endl;
cout << consoleOutput << endl;
Outputs:
0
0
0
And I don't even see output from ipconfig? (Even if I comment out the externalProcess.Redirect(); line, and I looked in the memory with the debugger and redirect is set to false then)

Thanks for helping further!
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Can you try the "exec" sample that comes with wx? Does it work for you?[/i]
"Keyboard not detected. Press F1 to continue"
-- Windows
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

Just compiled the exec sample and it works perfectly D:

Even weirder, when I try to redirect with SYNC on in my program, it complains whereas the exec sample allows me to choose between SYNC and ASYNC and both work perfectly :(
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

Sorry for double posting, you can merge my posts if needed.

I got it to work with the SYNC execution this way:

Code: Select all

wxArrayString output, errors;
size_t count;

wxExecute(wxT("psgetsid.exe Administrator"), output, errors);

count = output.GetCount();

for ( size_t n = 0; n < count; n++ )
{
    cout << output[n].mb_str(wxConvUTF8) << endl;
}
I removed some validations for the sake of simplicity.

I would still like help to be able to get it to work asynchronously with wxProcess, thanks!
DavidHart
Site Admin
Site Admin
Posts: 4252
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart »

Hi,

I'm not sure that you can use wxProcess without wxExecute. I do:

Code: Select all

DisplayProcess* process = new DisplayProcess(this);
long pid = wxExecute(command, wxEXEC_ASYNC, process);
where DisplayProcess is derived from wxProcess.

See http://fourpane.svn.sourceforge.net/vie ... p?view=log for the rest of the code.

Regards,

David
mathieumg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Fri Aug 06, 2010 5:16 pm

Post by mathieumg »

Thanks for the information David!
Post Reply