我正在使用这样的函数在 C 代码中创建 C 管道:
extern int *create_pipe() {
int *_pipe = (int *)malloc(2 * sizeof(int));
pipe(_pipe);
return _pipe;
}
我在 C# 中绑定了这个函数,如下所示:
[DllImport("mydll")]
private static unsafe extern int *create_pipe();
我这样调用 C# 代码:
unsafe {
int *pipe = create_pipe();
hostout = pipe[1];
hostin = pipe[0];
}
然后我尝试BinaryWriter
像这样创建一个管道:
var writer = new BinaryWriter(new AnonymousPipeClientStream(PipeDirection.Out, new SafePipeHandle(new IntPtr(hostout), false)));
我尝试给它写一个简单的整数值:
writer.Write(50);
但我得到这个错误:
Unhandled Exception:
System.IO.IOException: Invalid handle to path "[Unknown]"
at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync, Boolean isZeroSize) [0x0006a] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/corlib/System.IO/FileStream.cs:86
at System.IO.FileStream..ctor (IntPtr handle, FileAccess access, Boolean ownsHandle, Int32 bufferSize, Boolean isAsync) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (intptr,System.IO.FileAccess,bool,int,bool)
at System.IO.Pipes.PipeStream.get_Stream () [0x00021] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/System.Core/System.IO.Pipes/PipeStream.cs:134
at System.IO.Pipes.PipeStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00006] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/System.Core/System.IO.Pipes/PipeStream.cs:273
at System.IO.BinaryWriter.Write (Int32 value) [0x0004b] in /Users/builder/data/lanes/mono-mac-ui-refresh-2-10/2baeee2f/source/bockbuild/profiles/mono-2-10/build-root/mono-2.10.11/_build/mono-2.10.11.git/mcs/class/corlib/System.IO/BinaryWriter.cs:244
任何想法如何完成我正在尝试做的事情?
先感谢您!