[英]“Pipe is broken” C# .NET pipe with C++ backend (WINDOWS)
因此,我目前正在使用管道進行一些有趣的工作。 我有一個C ++可注入dll,我在一個過程中注入它來獲取有關信息並向我的C#GUI報告。 我正在使用管道在后端和前端之間進行通信。 效果很好,但是當我嘗試與C ++注射劑通信時,我收到“管道損壞”的信息。 錯誤。
在我的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收到“管道損壞”錯誤。 為什么會這樣呢?
您嘗試使用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.