"Pipe is broken" 带有 C++ 后端的 C# .NET 管道 (WINDOWS)
"Pipe is broken" C# .NET pipe with C++ backend (WINDOWS)
所以我目前正在用管道做一些有趣的工作。我有一个 C++ 可注入 dll,我将其注入到一个进程中以获取有关信息并向我的 C# GUI 报告。我正在使用管道在后端和前端之间进行通信。这工作正常,但是当我尝试与 C++ 可注入程序通信时,我收到 "Pipe is broken." 错误。
在我的 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);
}
}
^ 我已经确认线程是 运行
在我的 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();
}
}
然而,当 运行 我从 .NET 收到 "Pipe is broken" 错误。为什么会这样?
您尝试过使用 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();
}
}
}
所以我目前正在用管道做一些有趣的工作。我有一个 C++ 可注入 dll,我将其注入到一个进程中以获取有关信息并向我的 C# GUI 报告。我正在使用管道在后端和前端之间进行通信。这工作正常,但是当我尝试与 C++ 可注入程序通信时,我收到 "Pipe is broken." 错误。
在我的 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);
}
}
^ 我已经确认线程是 运行
在我的 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();
}
}
然而,当 运行 我从 .NET 收到 "Pipe is broken" 错误。为什么会这样?
您尝试过使用 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();
}
}
}