C# 如何将命名管道字符串从umnanaged发送到托管代码空间?
我似乎有一个命名管道101问题。我有一个非常简单的设置来连接一个简单的管道,它将管道从C++的非托管应用程序传输到C托管的应用程序。管道连接,但我无法通过管道发送“消息”,除非我关闭句柄,该句柄似乎会刷新缓冲区并传递消息。这就像消息被阻止了一样。我曾经尝试过逆转客户机/服务器的角色,并使用不同的标志组合调用它们,但没有任何运气。我可以很容易地把消息从C方向管理到C++非托管。有人有什么见解吗。你们中的任何一个人成功地把来自C++非托管的消息发送到C托管吗?我可以找到很多内部托管或非托管管道的示例,但没有未更改的内部托管到/来自未更改的-只是声称能够做到这一点 在清单中,为了清晰起见,我省略了许多包装器内容。我认为相关的关键点是管道连接/创建/读写方法。这里不要太担心阻塞/线程 服务器端C# 如何将命名管道字符串从umnanaged发送到托管代码空间?,c#,c++,windows,mfc,named-pipes,C#,C++,Windows,Mfc,Named Pipes,我似乎有一个命名管道101问题。我有一个非常简单的设置来连接一个简单的管道,它将管道从C++的非托管应用程序传输到C托管的应用程序。管道连接,但我无法通过管道发送“消息”,除非我关闭句柄,该句柄似乎会刷新缓冲区并传递消息。这就像消息被阻止了一样。我曾经尝试过逆转客户机/服务器的角色,并使用不同的标志组合调用它们,但没有任何运气。我可以很容易地把消息从C方向管理到C++非托管。有人有什么见解吗。你们中的任何一个人成功地把来自C++非托管的消息发送到C托管吗?我可以找到很多内部托管或非托管管道的示例
// This runs in its own thread and so it is OK to block
private void ConnectToClient()
{
// This server will listen to the sending client
if (m_InPipeStream == null)
{
m_InPipeStream =
new NamedPipeServerStream("TestPipe", PipeDirection.In, 1);
}
// Wait for client to connect to our server
m_InPipeStream.WaitForConnection();
// Verify client is running
if (!m_InPipeStream.IsConnected)
{
return;
}
// Start listening for messages on the client stream
if (m_InPipeStream != null && m_InPipeStream.CanRead)
{
ReadThread = new Thread(new ParameterizedThreadStart(Read));
ReadThread.Start(m_InPipeStream);
}
}
// This runs in its own thread and so it is OK to block
private void Read(object serverObj)
{
NamedPipeServerStream pipeStream = (NamedPipeServerStream)serverObj;
using (StreamReader sr = new StreamReader(pipeStream))
{
while (true)
{
string buffer = "" ;
try
{
// Blocks here until the handle is closed by the client-side!!
buffer = sr.ReadLine(); // <<<<<<<<<<<<<< Sticks here
}
catch
{
// Read error
break;
}
// Client has disconnected?
if (buffer == null || buffer.Length == 0)
break;
// Fire message received event if message is non-empty
if (MessageReceived != null && buffer != "")
{
MessageReceived(buffer);
}
}
}
}
//它在自己的线程中运行,因此可以阻止
私有无效连接客户端()
{
//此服务器将侦听发送客户端
if(m_InPipeStream==null)
{
m_InPipeStream=
新名称为pipeserverstream(“TestPipe”,PipeDirection.In,1);
}
//等待客户端连接到我们的服务器
m_InPipeStream.WaitForConnection();
//验证客户端是否正在运行
如果(!m_InPipeStream.IsConnected)
{
返回;
}
//开始侦听客户端流上的消息
if(m_InPipeStream!=null&&m_InPipeStream.CanRead)
{
ReadThread=新线程(新参数化的threadstart(Read));
ReadThread.Start(管道流中的m_);
}
}
//它在自己的线程中运行,因此可以阻止
私有void读取(对象serverObj)
{
NamedPipeServerStream管道流=(NamedPipeServerStream)serverObj;
使用(StreamReader sr=新StreamReader(pipeStream))
{
while(true)
{
字符串缓冲区=”;
尝试
{
//在这里阻塞,直到客户端关闭句柄!!
buffer=sr.ReadLine();//sr.ReadLine()希望看到一个或多个换行字符以知道行的结尾。因为它既不接收新行,也不接收流的结尾,所以它会等待更多。
尝试:
或者是一些sr.Read()方法。就是这样!现在一切都正常了,非常感谢。我完全忘记了换行的必要性,我太专注于管道的机制(第1层),我忘记了消息格式(第2层)。
// Static - running in its own thread.
DWORD CNamedPipe::ListenForServer(LPVOID arg)
{
// The calling app (this) is passed as the parameter
CNamedPipe* app = (CNamedPipe*)arg;
// Out-Pipe: connect as a client to a waiting server
app->m_hOutPipeHandle =
CreateFile("\\\\.\\pipe\\TestPipe",
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// Could not create handle
if (app->m_hInPipeHandle == NULL ||
app->m_hInPipeHandle == INVALID_HANDLE_VALUE)
{
return 1;
}
return 0;
}
// Sends a message to the server
BOOL CNamedPipe::SendMessage(CString message)
{
DWORD dwSent;
if (m_hOutPipeHandle == NULL ||
m_hOutPipeHandle == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
BOOL bOK =
WriteFile(m_hOutPipeHandle,
message, message.GetLength()+1, &dwSent, NULL);
//FlushFileBuffers(m_hOutPipeHandle); // <<<<<<< Tried this
return (!bOK || (message.GetLength()+1) != dwSent) ? FALSE : TRUE;
}
}
// Somewhere in the Windows C++/MFC code...
...
// This write is non-blocking. It just passes through having loaded the pipe.
m_pNamedPipe->SendMessage("Hi de hi");
...
m_pNamedPipe->SendMessage("Hi de hi\n")