Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 17, 2005 capturing process output on windows | ||||
---|---|---|---|---|
| ||||
What's the correct way of capturing a process output on Windows? I found a piece of code on the net, and I tried it, but since I haven't worked with pipes before, I don't know what's going on. Anyway, here's what I'm trying (using the core32 library): //------------------------------------------- //code found in //http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682069.html void main() { char [] stdoutname=r"\\.\pipe\my_output_tmp", stderrname=r"\\.\pipe\my_error_tmp", stdinname=r"\\.\pipe\my_input_tmp"; Stream fstderr, fstdout, fstdin; HANDLE hstderr, hstdout, hstdin; HANDLE outstreamhandle, errstreamhandle, instreamhandle; char * pOutputFile=toStringz(stdoutname), pInputFile=toStringz(stdinname); SECURITY_ATTRIBUTES SecAttrs; SecAttrs.nLength=SECURITY_ATTRIBUTES.sizeof; SecAttrs.bInheritHandle=true; outstreamhandle = CreateNamedPipeA( pOutputFile, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 8192, 8192, 10000, &SecAttrs); assert(outstreamhandle!=INVALID_HANDLE_VALUE); hstdout = CreateFileA( pOutputFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &SecAttrs, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, null ); assert(hstdout!=INVALID_HANDLE_VALUE); instreamhandle= CreateNamedPipeA( pInputFile, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 8192, 8192, 10000, &SecAttrs); assert(instreamhandle!=INVALID_HANDLE_VALUE); hstdin = CreateFileA( pInputFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &SecAttrs, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, null ); assert(hstdin!=INVALID_HANDLE_VALUE); STARTUPINFOA sui; sui.cb = STARTUPINFOA.sizeof; sui.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; sui.wShowWindow = SW_HIDE; sui.hStdOutput = hstdout; sui.hStdInput = hstdin; PROCESS_INFORMATION pi; int res = CreateProcessA(null, "dir", null, null, true, CREATE_NEW_CONSOLE | REALTIME_PRIORITY_CLASS, null, null, &sui, &pi); DWORD size; size = GetFileSize(outstreamhandle, null); printf("%d\n", size); size = GetFileSize(hstdout, null); printf("%d\n", size); size = GetFileSize(instreamhandle, null); printf("%d\n", size); size = GetFileSize(hstdin, null); printf("%d\n", size); assert (size != INVALID_FILE_SIZE); /* fstdout=new BufferedFile(new File(cast(std.stream.HANDLE) outstreamhandle, FileMode.In | FileMode.Out )); foreach(char[] ln;fstdout) stdout.writeLine(ln); */ /* fstdin=new BufferedFile(new File(cast(std.stream.HANDLE) instreamhandle, FileMode.In | FileMode.Out)); foreach(char[] ln;fstdin) stdout.writeLine(ln); */ } //------------------------------------------- All asserts pass. All sizes return 0. Obviosly, even in an empty directory, "dir" outputs something. Eventually, I'd like to do what's commented out. BTW, I've never worked with pipes before on linux, but I also found some code to do the same, and it's unbelievably easy. When/if I get this Windows thing sorted out (a couple of days, maybe?) I'll release the code for anyone in need of it. -- Carlos Santander Bernal JP2, you'll always live in our minds |
April 18, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | It should not be so complicated.... Read this: http://www.developersdomain.com/vb/articles/redirectpipe.htm Or this: http://support.microsoft.com/kb/q173085/ Funny, google returns VB links mostly.... |
April 18, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/14470 news://news.digitalmars.com:119/cscp40$pee$1@digitaldaemon.com I posted this earlier. It's popen for Windows, which is probably that "unbelievably easy" method you mentioned. Syntax is the same, or you cna use the ProcessStream implementation. -[Unknown] > What's the correct way of capturing a process output on Windows? I found a piece of code on the net, and I tried it, but since I haven't worked with pipes before, I don't know what's going on. Anyway, here's what I'm trying (using the core32 library): |
April 18, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Fedoniouk | Andrew Fedoniouk wrote: > It should not be so complicated.... > > Read this: > http://www.developersdomain.com/vb/articles/redirectpipe.htm > Or this: > http://support.microsoft.com/kb/q173085/ > > Funny, google returns VB links mostly.... > > Thanks for that. It seems to work. Well, sort of, I still have a couple of things to check. -- Carlos Santander Bernal JP2, you'll always live in our minds |
April 18, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Unknown W. Brackets wrote: > http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/14470 > news://news.digitalmars.com:119/cscp40$pee$1@digitaldaemon.com > > I posted this earlier. It's popen for Windows, which is probably that "unbelievably easy" method you mentioned. Syntax is the same, or you cna use the ProcessStream implementation. > > -[Unknown] > Thanks. However I'd like to do both reading and writing. What I have so far works that way on linux, and is kinda buggy on Windows. -- Carlos Santander Bernal JP2, you'll always live in our minds |
April 19, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote: > > When/if I get this Windows thing sorted out (a couple of days, > maybe?) I'll release the code for anyone in need of it. > It's not like it's really working on Windows, but I've uploaded it to dsource. It's in http://svn.dsource.org/svn/projects/walnut/downloads/some_other_things/ -- Carlos Santander Bernal JP2, you'll always live in our minds |
April 19, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote:
> What's the correct way of capturing a process output on Windows? I found a piece of code on the net, and I tried it, but since I haven't worked with pipes before, I don't know what's going on. Anyway, here's what I'm trying (using the core32 library):
>
> //-------------------------------------------
> //code found in
> //http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682069.html
>
>
> void main()
> {
> char [] stdoutname=r"\\.\pipe\my_output_tmp",
> stderrname=r"\\.\pipe\my_error_tmp",
> stdinname=r"\\.\pipe\my_input_tmp";
> Stream fstderr, fstdout, fstdin;
> HANDLE hstderr, hstdout, hstdin;
> HANDLE outstreamhandle, errstreamhandle, instreamhandle;
> char * pOutputFile=toStringz(stdoutname), pInputFile=toStringz(stdinname);
>
> SECURITY_ATTRIBUTES SecAttrs;
> SecAttrs.nLength=SECURITY_ATTRIBUTES.sizeof;
> SecAttrs.bInheritHandle=true;
>
> outstreamhandle = CreateNamedPipeA(
> pOutputFile,
> PIPE_ACCESS_DUPLEX,
> PIPE_TYPE_BYTE | PIPE_WAIT,
> PIPE_UNLIMITED_INSTANCES,
> 8192,
> 8192,
> 10000,
> &SecAttrs);
>
> assert(outstreamhandle!=INVALID_HANDLE_VALUE);
>
> hstdout = CreateFileA(
> pOutputFile,
> GENERIC_READ | GENERIC_WRITE,
> FILE_SHARE_READ | FILE_SHARE_WRITE,
> &SecAttrs,
> CREATE_ALWAYS,
> FILE_ATTRIBUTE_TEMPORARY,
> null );
>
> assert(hstdout!=INVALID_HANDLE_VALUE);
>
> instreamhandle= CreateNamedPipeA(
> pInputFile,
> PIPE_ACCESS_DUPLEX,
> PIPE_TYPE_BYTE | PIPE_WAIT,
> PIPE_UNLIMITED_INSTANCES,
> 8192,
> 8192,
> 10000,
> &SecAttrs);
>
> assert(instreamhandle!=INVALID_HANDLE_VALUE);
>
> hstdin = CreateFileA(
> pInputFile,
> GENERIC_READ | GENERIC_WRITE,
> FILE_SHARE_READ | FILE_SHARE_WRITE,
> &SecAttrs,
> OPEN_ALWAYS,
> FILE_ATTRIBUTE_TEMPORARY,
> null );
>
> assert(hstdin!=INVALID_HANDLE_VALUE);
>
> STARTUPINFOA sui;
> sui.cb = STARTUPINFOA.sizeof;
> sui.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
> sui.wShowWindow = SW_HIDE;
> sui.hStdOutput = hstdout;
> sui.hStdInput = hstdin;
>
> PROCESS_INFORMATION pi;
> int res = CreateProcessA(null,
> "dir",
> null,
> null,
> true,
> CREATE_NEW_CONSOLE | REALTIME_PRIORITY_CLASS,
> null,
> null,
> &sui,
> &pi);
>
> DWORD size;
> size = GetFileSize(outstreamhandle, null);
> printf("%d\n", size);
> size = GetFileSize(hstdout, null);
> printf("%d\n", size);
> size = GetFileSize(instreamhandle, null);
> printf("%d\n", size);
> size = GetFileSize(hstdin, null);
> printf("%d\n", size);
> assert (size != INVALID_FILE_SIZE);
>
> /*
> fstdout=new BufferedFile(new File(cast(std.stream.HANDLE) outstreamhandle, FileMode.In | FileMode.Out ));
> foreach(char[] ln;fstdout)
> stdout.writeLine(ln);
> */
>
> /*
> fstdin=new BufferedFile(new File(cast(std.stream.HANDLE) instreamhandle, FileMode.In | FileMode.Out));
> foreach(char[] ln;fstdin)
> stdout.writeLine(ln);
> */
> }
> //-------------------------------------------
>
> All asserts pass. All sizes return 0. Obviosly, even in an empty directory, "dir" outputs something.
>
> Eventually, I'd like to do what's commented out.
>
> BTW, I've never worked with pipes before on linux, but I also found some code to do the same, and it's unbelievably easy. When/if I get this Windows thing sorted out (a couple of days, maybe?) I'll release the code for anyone in need of it.
>
You can get the source to DManager, which is in Delphi but uses the Win32 API to capture output. Look in MyUtils.pas
-David
|
April 19, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | I have some Win32 C code for doing just this.. I reckon I can whip it into a stream class or similar, give me a few days...
Regan
On Sun, 17 Apr 2005 17:01:07 -0500, Carlos Santander B. <csantander619@gmail.com> wrote:
> What's the correct way of capturing a process output on Windows? I found a piece of code on the net, and I tried it, but since I haven't worked with pipes before, I don't know what's going on. Anyway, here's what I'm trying (using the core32 library):
>
> //-------------------------------------------
> //code found in
> //http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682069.html
>
> void main()
> {
> char [] stdoutname=r"\\.\pipe\my_output_tmp",
> stderrname=r"\\.\pipe\my_error_tmp",
> stdinname=r"\\.\pipe\my_input_tmp";
> Stream fstderr, fstdout, fstdin;
> HANDLE hstderr, hstdout, hstdin;
> HANDLE outstreamhandle, errstreamhandle, instreamhandle;
> char * pOutputFile=toStringz(stdoutname), pInputFile=toStringz(stdinname);
>
> SECURITY_ATTRIBUTES SecAttrs;
> SecAttrs.nLength=SECURITY_ATTRIBUTES.sizeof;
> SecAttrs.bInheritHandle=true;
>
> outstreamhandle = CreateNamedPipeA(
> pOutputFile,
> PIPE_ACCESS_DUPLEX,
> PIPE_TYPE_BYTE | PIPE_WAIT,
> PIPE_UNLIMITED_INSTANCES,
> 8192,
> 8192,
> 10000,
> &SecAttrs);
>
> assert(outstreamhandle!=INVALID_HANDLE_VALUE);
>
> hstdout = CreateFileA(
> pOutputFile,
> GENERIC_READ | GENERIC_WRITE,
> FILE_SHARE_READ | FILE_SHARE_WRITE,
> &SecAttrs,
> CREATE_ALWAYS,
> FILE_ATTRIBUTE_TEMPORARY,
> null );
>
> assert(hstdout!=INVALID_HANDLE_VALUE);
>
> instreamhandle= CreateNamedPipeA(
> pInputFile,
> PIPE_ACCESS_DUPLEX,
> PIPE_TYPE_BYTE | PIPE_WAIT,
> PIPE_UNLIMITED_INSTANCES,
> 8192,
> 8192,
> 10000,
> &SecAttrs);
>
> assert(instreamhandle!=INVALID_HANDLE_VALUE);
>
> hstdin = CreateFileA(
> pInputFile,
> GENERIC_READ | GENERIC_WRITE,
> FILE_SHARE_READ | FILE_SHARE_WRITE,
> &SecAttrs,
> OPEN_ALWAYS,
> FILE_ATTRIBUTE_TEMPORARY,
> null );
>
> assert(hstdin!=INVALID_HANDLE_VALUE);
>
> STARTUPINFOA sui;
> sui.cb = STARTUPINFOA.sizeof;
> sui.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
> sui.wShowWindow = SW_HIDE;
> sui.hStdOutput = hstdout;
> sui.hStdInput = hstdin;
>
> PROCESS_INFORMATION pi;
> int res = CreateProcessA(null,
> "dir",
> null,
> null,
> true,
> CREATE_NEW_CONSOLE | REALTIME_PRIORITY_CLASS,
> null,
> null,
> &sui,
> &pi);
>
> DWORD size;
> size = GetFileSize(outstreamhandle, null);
> printf("%d\n", size);
> size = GetFileSize(hstdout, null);
> printf("%d\n", size);
> size = GetFileSize(instreamhandle, null);
> printf("%d\n", size);
> size = GetFileSize(hstdin, null);
> printf("%d\n", size);
> assert (size != INVALID_FILE_SIZE);
>
> /*
> fstdout=new BufferedFile(new File(cast(std.stream.HANDLE) outstreamhandle, FileMode.In | FileMode.Out ));
> foreach(char[] ln;fstdout)
> stdout.writeLine(ln);
> */
>
> /*
> fstdin=new BufferedFile(new File(cast(std.stream.HANDLE) instreamhandle, FileMode.In | FileMode.Out));
> foreach(char[] ln;fstdin)
> stdout.writeLine(ln);
> */
> }
> //-------------------------------------------
>
> All asserts pass. All sizes return 0. Obviosly, even in an empty directory, "dir" outputs something.
>
> Eventually, I'd like to do what's commented out.
>
> BTW, I've never worked with pipes before on linux, but I also found some code to do the same, and it's unbelievably easy. When/if I get this Windows thing sorted out (a couple of days, maybe?) I'll release the code for anyone in need of it.
>
|
April 19, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath Attachments: | As promised... Tho it should be noted that you cannot simply run "dir" with this. it wants an actual executable not a cmd.exe command like "dir". However you can run "cmd /c dir" and get a directory listing.
It would be nice if Ben had time to look at it and make sure I have not abused Stream ;)
Regan
On Tue, 19 Apr 2005 16:40:55 +1200, Regan Heath <regan@netwin.co.nz> wrote:
> I have some Win32 C code for doing just this.. I reckon I can whip it into a stream class or similar, give me a few days...
>
> Regan
>
> On Sun, 17 Apr 2005 17:01:07 -0500, Carlos Santander B. <csantander619@gmail.com> wrote:
>
>> What's the correct way of capturing a process output on Windows? I found a piece of code on the net, and I tried it, but since I haven't worked with pipes before, I don't know what's going on. Anyway, here's what I'm trying (using the core32 library):
>>
>> //-------------------------------------------
>> //code found in
>> //http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682069.html
>>
>> void main()
>> {
>> char [] stdoutname=r"\\.\pipe\my_output_tmp",
>> stderrname=r"\\.\pipe\my_error_tmp",
>> stdinname=r"\\.\pipe\my_input_tmp";
>> Stream fstderr, fstdout, fstdin;
>> HANDLE hstderr, hstdout, hstdin;
>> HANDLE outstreamhandle, errstreamhandle, instreamhandle;
>> char * pOutputFile=toStringz(stdoutname),
>> pInputFile=toStringz(stdinname);
>>
>> SECURITY_ATTRIBUTES SecAttrs;
>> SecAttrs.nLength=SECURITY_ATTRIBUTES.sizeof;
>> SecAttrs.bInheritHandle=true;
>>
>> outstreamhandle = CreateNamedPipeA(
>> pOutputFile,
>> PIPE_ACCESS_DUPLEX,
>> PIPE_TYPE_BYTE | PIPE_WAIT,
>> PIPE_UNLIMITED_INSTANCES,
>> 8192,
>> 8192,
>> 10000,
>> &SecAttrs);
>>
>> assert(outstreamhandle!=INVALID_HANDLE_VALUE);
>>
>> hstdout = CreateFileA(
>> pOutputFile,
>> GENERIC_READ | GENERIC_WRITE,
>> FILE_SHARE_READ | FILE_SHARE_WRITE,
>> &SecAttrs,
>> CREATE_ALWAYS,
>> FILE_ATTRIBUTE_TEMPORARY,
>> null );
>>
>> assert(hstdout!=INVALID_HANDLE_VALUE);
>>
>> instreamhandle= CreateNamedPipeA(
>> pInputFile,
>> PIPE_ACCESS_DUPLEX,
>> PIPE_TYPE_BYTE | PIPE_WAIT,
>> PIPE_UNLIMITED_INSTANCES,
>> 8192,
>> 8192,
>> 10000,
>> &SecAttrs);
>>
>> assert(instreamhandle!=INVALID_HANDLE_VALUE);
>>
>> hstdin = CreateFileA(
>> pInputFile,
>> GENERIC_READ | GENERIC_WRITE,
>> FILE_SHARE_READ | FILE_SHARE_WRITE,
>> &SecAttrs,
>> OPEN_ALWAYS,
>> FILE_ATTRIBUTE_TEMPORARY,
>> null );
>>
>> assert(hstdin!=INVALID_HANDLE_VALUE);
>>
>> STARTUPINFOA sui;
>> sui.cb = STARTUPINFOA.sizeof;
>> sui.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
>> sui.wShowWindow = SW_HIDE;
>> sui.hStdOutput = hstdout;
>> sui.hStdInput = hstdin;
>>
>> PROCESS_INFORMATION pi;
>> int res = CreateProcessA(null,
>> "dir",
>> null,
>> null,
>> true,
>> CREATE_NEW_CONSOLE | REALTIME_PRIORITY_CLASS,
>> null,
>> null,
>> &sui,
>> &pi);
>>
>> DWORD size;
>> size = GetFileSize(outstreamhandle, null);
>> printf("%d\n", size);
>> size = GetFileSize(hstdout, null);
>> printf("%d\n", size);
>> size = GetFileSize(instreamhandle, null);
>> printf("%d\n", size);
>> size = GetFileSize(hstdin, null);
>> printf("%d\n", size);
>> assert (size != INVALID_FILE_SIZE);
>>
>> /*
>> fstdout=new BufferedFile(new File(cast(std.stream.HANDLE)
>> outstreamhandle, FileMode.In | FileMode.Out ));
>> foreach(char[] ln;fstdout)
>> stdout.writeLine(ln);
>> */
>>
>> /*
>> fstdin=new BufferedFile(new File(cast(std.stream.HANDLE)
>> instreamhandle, FileMode.In | FileMode.Out));
>> foreach(char[] ln;fstdin)
>> stdout.writeLine(ln);
>> */
>> }
>> //-------------------------------------------
>>
>> All asserts pass. All sizes return 0. Obviosly, even in an empty directory, "dir" outputs something.
>>
>> Eventually, I'd like to do what's commented out.
>>
>> BTW, I've never worked with pipes before on linux, but I also found some code to do the same, and it's unbelievably easy. When/if I get this Windows thing sorted out (a couple of days, maybe?) I'll release the code for anyone in need of it.
>>
>
|
April 19, 2005 Re: capturing process output on windows | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Err.. I forgot to mention the linux version of this code is _totally_ untested, in fact I haven't even compiled it.
On Wed, 20 Apr 2005 01:06:15 +1200, Regan Heath <regan@netwin.co.nz> wrote:
> As promised... Tho it should be noted that you cannot simply run "dir"
> with this. it wants an actual executable not a cmd.exe command like "dir".
> However you can run "cmd /c dir" and get a directory listing.
>
> It would be nice if Ben had time to look at it and make sure I have not
> abused Stream ;)
>
> Regan
>
> On Tue, 19 Apr 2005 16:40:55 +1200, Regan Heath <regan@netwin.co.nz> wrote:
>> I have some Win32 C code for doing just this.. I reckon I can whip it
>> into a stream class or similar, give me a few days...
>>
>> Regan
>>
>> On Sun, 17 Apr 2005 17:01:07 -0500, Carlos Santander B.
>> <csantander619@gmail.com> wrote:
>>
>>> What's the correct way of capturing a process output on Windows? I
>>> found a piece of code on the net, and I tried it, but since I haven't
>>> worked with pipes before, I don't know what's going on. Anyway, here's
>>> what I'm trying (using the core32 library):
>>>
>>> //-------------------------------------------
>>> //code found in
>>> //http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20682069.html
>>>
>>> void main()
>>> {
>>> char [] stdoutname=r"\\.\pipe\my_output_tmp",
>>> stderrname=r"\\.\pipe\my_error_tmp",
>>> stdinname=r"\\.\pipe\my_input_tmp";
>>> Stream fstderr, fstdout, fstdin;
>>> HANDLE hstderr, hstdout, hstdin;
>>> HANDLE outstreamhandle, errstreamhandle, instreamhandle;
>>> char * pOutputFile=toStringz(stdoutname),
>>> pInputFile=toStringz(stdinname);
>>>
>>> SECURITY_ATTRIBUTES SecAttrs;
>>> SecAttrs.nLength=SECURITY_ATTRIBUTES.sizeof;
>>> SecAttrs.bInheritHandle=true;
>>>
>>> outstreamhandle = CreateNamedPipeA(
>>> pOutputFile,
>>> PIPE_ACCESS_DUPLEX,
>>> PIPE_TYPE_BYTE | PIPE_WAIT,
>>> PIPE_UNLIMITED_INSTANCES,
>>> 8192,
>>> 8192,
>>> 10000,
>>> &SecAttrs);
>>>
>>> assert(outstreamhandle!=INVALID_HANDLE_VALUE);
>>>
>>> hstdout = CreateFileA(
>>> pOutputFile,
>>> GENERIC_READ | GENERIC_WRITE,
>>> FILE_SHARE_READ | FILE_SHARE_WRITE,
>>> &SecAttrs,
>>> CREATE_ALWAYS,
>>> FILE_ATTRIBUTE_TEMPORARY,
>>> null );
>>>
>>> assert(hstdout!=INVALID_HANDLE_VALUE);
>>>
>>> instreamhandle= CreateNamedPipeA(
>>> pInputFile,
>>> PIPE_ACCESS_DUPLEX,
>>> PIPE_TYPE_BYTE | PIPE_WAIT,
>>> PIPE_UNLIMITED_INSTANCES,
>>> 8192,
>>> 8192,
>>> 10000,
>>> &SecAttrs);
>>>
>>> assert(instreamhandle!=INVALID_HANDLE_VALUE);
>>>
>>> hstdin = CreateFileA(
>>> pInputFile,
>>> GENERIC_READ | GENERIC_WRITE,
>>> FILE_SHARE_READ | FILE_SHARE_WRITE,
>>> &SecAttrs,
>>> OPEN_ALWAYS,
>>> FILE_ATTRIBUTE_TEMPORARY,
>>> null );
>>>
>>> assert(hstdin!=INVALID_HANDLE_VALUE);
>>>
>>> STARTUPINFOA sui;
>>> sui.cb = STARTUPINFOA.sizeof;
>>> sui.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
>>> sui.wShowWindow = SW_HIDE;
>>> sui.hStdOutput = hstdout;
>>> sui.hStdInput = hstdin;
>>>
>>> PROCESS_INFORMATION pi;
>>> int res = CreateProcessA(null,
>>> "dir",
>>> null,
>>> null,
>>> true,
>>> CREATE_NEW_CONSOLE | REALTIME_PRIORITY_CLASS,
>>> null,
>>> null,
>>> &sui,
>>> &pi);
>>>
>>> DWORD size;
>>> size = GetFileSize(outstreamhandle, null);
>>> printf("%d\n", size);
>>> size = GetFileSize(hstdout, null);
>>> printf("%d\n", size);
>>> size = GetFileSize(instreamhandle, null);
>>> printf("%d\n", size);
>>> size = GetFileSize(hstdin, null);
>>> printf("%d\n", size);
>>> assert (size != INVALID_FILE_SIZE);
>>>
>>> /*
>>> fstdout=new BufferedFile(new File(cast(std.stream.HANDLE)
>>> outstreamhandle, FileMode.In | FileMode.Out ));
>>> foreach(char[] ln;fstdout)
>>> stdout.writeLine(ln);
>>> */
>>>
>>> /*
>>> fstdin=new BufferedFile(new File(cast(std.stream.HANDLE)
>>> instreamhandle, FileMode.In | FileMode.Out));
>>> foreach(char[] ln;fstdin)
>>> stdout.writeLine(ln);
>>> */
>>> }
>>> //-------------------------------------------
>>>
>>> All asserts pass. All sizes return 0. Obviosly, even in an empty
>>> directory, "dir" outputs something.
>>>
>>> Eventually, I'd like to do what's commented out.
>>>
>>> BTW, I've never worked with pipes before on linux, but I also found
>>> some code to do the same, and it's unbelievably easy. When/if I get
>>> this Windows thing sorted out (a couple of days, maybe?) I'll release
>>> the code for anyone in need of it.
>>>
>>
>
|
Copyright © 1999-2021 by the D Language Foundation