[英]“Pipe is broken” C# .NET pipe with C++ backend (WINDOWS)
So I am currently doing some interesting work with pipes. 因此,我目前正在使用管道进行一些有趣的工作。 I have a C++ injectable dll which I inject in a process to get info about and report back to my C# GUI.
我有一个C ++可注入dll,我在一个过程中注入它来获取有关信息并向我的C#GUI报告。 I am using pipes to communicate between the backend and the frontend.
我正在使用管道在后端和前端之间进行通信。 That works fine, but when I try to communicate to the C++ injectable I receive a "Pipe is broken."
效果很好,但是当我尝试与C ++注射剂通信时,我收到“管道损坏”的信息。 error.
错误。
In my c++ dll I have the following : 在我的C ++ dll中,我具有以下内容:
HANDLE Pipe = CreateNamedPipe("\\\\.\\pipe\\TestPipe",
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
999999,
999999,
NMPWAIT_USE_DEFAULT_WAIT,
NULL);
void PipeThread()
{
int numbuff;
DWORD dwRead;
while (Pipe != INVALID_HANDLE_VALUE)
{
if (ConnectNamedPipe(Pipe, NULL) != FALSE)
{
if (ReadFile(Pipe, (LPVOID)numbuff, sizeof(int), &dwRead, NULL) != FALSE)
{
std::stringstream o;
o << numbuff << "\r\n";
Output(o.str());
char buff[numbuff];
while (ReadFile(Pipe, buff, numbuff, &dwRead, NULL) != FALSE)
{
Output(std::string(buff)+"\r\n");
}
}
}
DisconnectNamedPipe(Pipe);
}
}
^ I have confirmed that thread was running ^我已经确认线程正在运行
In my C# GUI I have something like so: 在我的C#GUI中,我有类似以下内容:
private void button1_Click(object sender, EventArgs e)
{
if (!connected)
return;
using (var pipe = new NamedPipeClientStream(".", "TestPipe", PipeDirection.Out))
{
pipe.Connect();
byte[] outBuffer = Encoding.ASCII.GetBytes(TextBox.Text);
byte[] strSize = BitConverter.GetBytes(outBuffer.Length);
pipe.Write(strSize, 0, strSize.Length);
pipe.Write(outBuffer, 0, outBuffer.Length);
pipe.Flush();
}
}
However when running I get a "Pipe is broken" error from .NET. 但是,运行时,我从.NET收到“管道损坏”错误。 Why is this happening?
为什么会这样呢?
您尝试使用StreamWriter吗?
private void button1_Click(object sender, EventArgs e) { if (!connected) return; using (var pipe = new NamedPipeClientStream(".", "TestPipe", PipeDirection.Out)) { using (var stream = new StreamWriter(pipe)) { pipe.Connect(); stream.Write(TextBox.Text); pipe.Flush(); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.