www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - _popen alternative in D?

reply Maraco <darkandan windowslive.com> writes:
Hello.

I have a specific problem. I need to redirect console output from a
console program to dfl textbox but i cant find function to do it.
std.process.system only shows exiting code. I think
std.process.shell should do it but it is crashing whole program.

If You don't know what im asking for there's code how i've done it


*CODE BEGINS
Process p = new Process();
            p.StartInfo.FileName = "shutdown";
            p.StartInfo.Arguments = " /?";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();

            string output = p.StandardOutput.ReadToEnd();

            textBox3.Text = output;
*CODE ENDS

Can you provide me an idea what im doing wrong or alternative to
popen in D langauge?

Regards.
Jul 21 2011
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thu, 21 Jul 2011 16:19:12 +0300, Maraco <darkandan windowslive.com>  
wrote:

 Hello.

 I have a specific problem. I need to redirect console output from a
 console program to dfl textbox but i cant find function to do it.
 std.process.system only shows exiting code. I think
 std.process.shell should do it but it is crashing whole program.

 If You don't know what im asking for there's code how i've done it


 *CODE BEGINS
 Process p = new Process();
             p.StartInfo.FileName = "shutdown";
             p.StartInfo.Arguments = " /?";
             p.StartInfo.UseShellExecute = false;
             p.StartInfo.RedirectStandardOutput = true;
             p.StartInfo.CreateNoWindow = true;
             p.Start();

             string output = p.StandardOutput.ReadToEnd();

             textBox3.Text = output;
 *CODE ENDS

 Can you provide me an idea what im doing wrong or alternative to
 popen in D langauge?

 Regards.
This functionality is currently not implemented in D's standard library. Lars Kyllingstad implemented this in his version of std.process, but only supporting the POSIX API. For Windows, you'll need to do it manually, in the same way as in C: http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx (In my own projects, I just redirect the output to a temporary file.) -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jul 21 2011
next sibling parent reply Maraco <darkandan windowslive.com> writes:
Ok so i need to wait until process will exit and read output from file
that process wrote, am i right?
Jul 21 2011
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thu, 21 Jul 2011 16:38:09 +0300, Maraco <darkandan windowslive.com>  
wrote:

 Ok so i need to wait until process will exit and read output from file
 that process wrote, am i right?
Here's how I do it (warning: not very good D1 code): https://github.com/CyberShadow/Team15/blob/master/Utils.d#L1024 -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jul 21 2011
parent Maraco <darkandan windowslive.com> writes:
Thanks. I will try.
Jul 21 2011
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Jul 2011 09:33:34 -0400, Vladimir Panteleev  
<vladimir thecybershadow.net> wrote:

 On Thu, 21 Jul 2011 16:19:12 +0300, Maraco <darkandan windowslive.com>  
 wrote:

 Hello.

 I have a specific problem. I need to redirect console output from a
 console program to dfl textbox but i cant find function to do it.
 std.process.system only shows exiting code. I think
 std.process.shell should do it but it is crashing whole program.

 If You don't know what im asking for there's code how i've done it


 *CODE BEGINS
 Process p = new Process();
             p.StartInfo.FileName = "shutdown";
             p.StartInfo.Arguments = " /?";
             p.StartInfo.UseShellExecute = false;
             p.StartInfo.RedirectStandardOutput = true;
             p.StartInfo.CreateNoWindow = true;
             p.Start();

             string output = p.StandardOutput.ReadToEnd();

             textBox3.Text = output;
 *CODE ENDS

 Can you provide me an idea what im doing wrong or alternative to
 popen in D langauge?

 Regards.
This functionality is currently not implemented in D's standard library. Lars Kyllingstad implemented this in his version of std.process, but only supporting the POSIX API. For Windows, you'll need to do it manually, in the same way as in C: http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx
In fact, my windows changes are merged into Lars' branch (https://github.com/kyllingstad/phobos/tree/new-std-process) *BUT* (and this is a big but), dmc's runtime incorrectly handles pipes with C's stdio (i.e. FILE *). Since everything in D right now is FILE * based, it means you can't use pipes at all. However, I have submitted a patch for DMC's runtime to Walter. Hopefully it will be approved in the near future, and then we can work on merging the new std.process into phobos. -Steve
Jul 21 2011
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thu, 21 Jul 2011 18:47:03 +0300, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Thu, 21 Jul 2011 09:33:34 -0400, Vladimir Panteleev  
 <vladimir thecybershadow.net> wrote:

 On Thu, 21 Jul 2011 16:19:12 +0300, Maraco <darkandan windowslive.com>  
 wrote:

 Hello.

 I have a specific problem. I need to redirect console output from a
 console program to dfl textbox but i cant find function to do it.
 std.process.system only shows exiting code. I think
 std.process.shell should do it but it is crashing whole program.

 If You don't know what im asking for there's code how i've done it


 *CODE BEGINS
 Process p = new Process();
             p.StartInfo.FileName = "shutdown";
             p.StartInfo.Arguments = " /?";
             p.StartInfo.UseShellExecute = false;
             p.StartInfo.RedirectStandardOutput = true;
             p.StartInfo.CreateNoWindow = true;
             p.Start();

             string output = p.StandardOutput.ReadToEnd();

             textBox3.Text = output;
 *CODE ENDS

 Can you provide me an idea what im doing wrong or alternative to
 popen in D langauge?

 Regards.
This functionality is currently not implemented in D's standard library. Lars Kyllingstad implemented this in his version of std.process, but only supporting the POSIX API. For Windows, you'll need to do it manually, in the same way as in C: http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx
In fact, my windows changes are merged into Lars' branch (https://github.com/kyllingstad/phobos/tree/new-std-process) *BUT* (and this is a big but), dmc's runtime incorrectly handles pipes with C's stdio (i.e. FILE *). Since everything in D right now is FILE * based, it means you can't use pipes at all. However, I have submitted a patch for DMC's runtime to Walter. Hopefully it will be approved in the near future, and then we can work on merging the new std.process into phobos.
I didn't know it was possible to wrap a Windows pipe in a FILE. Neither did I know that the source to DMC's runtime is available. Would it be possible to fix the ridiculously low open FILE limit on Windows as well? -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jul 21 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 7/22/11, Vladimir Panteleev <vladimir thecybershadow.net> wrote:
 Would it be possible to fix the ridiculously low open FILE limit on
 Windows as well?
I've run into this issue as well. How low is it exactly?
Jul 21 2011
parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Fri, 22 Jul 2011 01:47:28 +0300, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 On 7/22/11, Vladimir Panteleev <vladimir thecybershadow.net> wrote:
 Would it be possible to fix the ridiculously low open FILE limit on
 Windows as well?
I've run into this issue as well. How low is it exactly?
Seems to be 60, including the 3 standard streams. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Jul 21 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 21 Jul 2011 18:39:31 -0400, Vladimir Panteleev  
<vladimir thecybershadow.net> wrote:

 On Thu, 21 Jul 2011 18:47:03 +0300, Steven Schveighoffer  
 <schveiguy yahoo.com> wrote:

 In fact, my windows changes are merged into Lars' branch  
 (https://github.com/kyllingstad/phobos/tree/new-std-process)

 *BUT*  (and this is a big but), dmc's runtime incorrectly handles pipes  
 with C's stdio (i.e. FILE *).  Since everything in D right now is FILE  
 * based, it means you can't use pipes at all.

 However, I have submitted a patch for DMC's runtime to Walter.   
 Hopefully it will be approved in the near future, and then we can work  
 on merging the new std.process into phobos.
I didn't know it was possible to wrap a Windows pipe in a FILE.
Well, under the hood, the runtime ends up using Windows HANDLES. In fact, a DMC file descriptor is just an index into a global array of HANDLES. I had to create a druntime function to wrap a HANDLE in a file descriptor (and get the handle from the fd). The bug in the runtime essentially is that when the pipe is closed from the write end, a read of the file descriptor results in EBADF. This makes the FILE * report it as error instead of EOF. In fact, I think the error should be EPIPE, but dmc's low level handler returns BADF no matter what error occurred when doing ReadFile.
 Neither did I know that the source to DMC's runtime is available.
It is if you purchase a license ;)
 Would it be possible to fix the ridiculously low open FILE limit on  
 Windows as well?
It's just a #define, but I'm not sure what the effects would be everywhere. You'd probably have to petition Walter for that. And it's not FILE limit, it's the file descriptor limit (set to 60 in stdio.h). -Steve
Jul 22 2011