forks/pipes and std.socket
Nick Sabalausky
a at a.a
Thu Sep 9 15:29:55 PDT 2010
"Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
news:op.vir82cineav7ka at localhost.localdomain...
> On Thu, 09 Sep 2010 16:25:44 -0400, Nick Sabalausky <a at a.a> wrote:
>
>> "Steven Schveighoffer" <schveiguy at yahoo.com> wrote in message
>> news:op.vipr20xfeav7ka at localhost.localdomain...
>>> On Tue, 07 Sep 2010 16:51:48 -0400, Nick Sabalausky <a at a.a> wrote:
>>>
>>>>
>>>> Ah cool, looking forward to it. I was just about ready to launch into a
>>>> bunch of std.process improvements myself ;) In the meantime, it seems
>>>> that
>>>> File has an undocumented constructor that takes a file handle instead
>>>> of
>>>> a
>>>> filename, so that should work for me.
>>>
>>> Beware, it takes a file *descriptor*, which is different from a *HANDLE*
>>> on windows. In writing the updated std.process I had to write D code
>>> that
>>> mimics the internals of DMC's runtime in order to translate between file
>>> descriptors and handles, not a pretty sight...
>>>
>>
>> I'm just feeding it the values I get from CreatePipe:
>> http://msdn.microsoft.com/en-us/library/aa365152(VS.85).aspx
>>
>> Seems to be working fine, though...
>
> That won't work. I'm surprised it works at all. DMC maintains the list
> of handles in an array, with the index of the array being the file
> descriptor, and the value at that element being the HANDLE. If it's
> working, you're extremely lucky that the unallocated slot at the index
> that happens to be that HANDLE value has the same value as the HANDLE
> itself.
>
> Or maybe I'm misunderstanding something... Walter?
>
Here's a simplified example of what I'm doing:
--------------------------------
> type mainapp.d
import std.conv;
import std.process;
import std.stdio;
import std.stream;
import core.sys.windows.windows;
// From here: http://msdn.microsoft.com/en-us/library/aa365152(VS.85).aspx
extern(Windows) int CreatePipe(
HANDLE* hReadPipe,
HANDLE* hWritePipe,
SECURITY_ATTRIBUTES* lpPipeAttributes,
uint nSize);
void main()
{
// Set up pipe
HANDLE readHandle;
HANDLE writeHandle;
auto secAttr = SECURITY_ATTRIBUTES(SECURITY_ATTRIBUTES.sizeof, null,
true);
if(!CreatePipe(&readHandle, &writeHandle, &secAttr, 0))
throw new Exception("Couldn't create pipe");
auto pipeReader = new std.stream.File(readHandle, FileMode.In);
// Run subapp
system("subapp "~to!string(cast(size_t)writeHandle));
// Retrieve value from pipe
int valueFromSubApp;
pipeReader.read(valueFromSubApp);
writeln("Received: ", valueFromSubApp);
}
> type subapp.d
import std.conv;
import std.stdio;
import std.stream;
import std.c.windows.windows;
void main(string[] args)
{
auto writeHandle = cast(HANDLE)std.conv.to!size_t(args[1]);
auto pipeWriter = new std.stream.File(writeHandle, FileMode.Out);
int value = 777;
writeln("Sending: ", value);
pipeWriter.write(value);
}
> dmd subapp.d
> dmd mainapp.d
> mainapp
Sending: 777
Received: 777
--------------------------------
Maybe the OS is just allowing me to use the wrong file descriptor?
More information about the Digitalmars-d-learn
mailing list