gpt4 book ai didi

c# - 如何将命名管道字符串从非托管代码空间发送到托管代码空间?

转载 作者:可可西里 更新时间:2023-11-01 12:47:10 26 4
gpt4 key购买 nike

我似乎有命名管道 101 问题。我有一个非常简单的设置来连接从 C++ 非托管应用程序传输到 C# 托管应用程序的单工命名管道。管道连接,但我无法通过管道发送“消息”,除非我关闭似乎刷新缓冲区并传递消息的句柄。就像消息被阻止了一样。我曾尝试颠倒客户端/服务器的角色并使用不同的 Flag 组合调用它们,但没有任何运气。我可以轻松地从 C# 托管到 C++ 非托管的另一个方向发送消息。有没有人有任何见识。你们中的任何人都可以成功地将消息从 C++ 非托管发送到 C# 托管吗?我可以找到很多内部 amanged 或非托管管道的示例,但没有找到内部管理到/来自 unamanged 的​​示例 - 只是声称能够做到这一点。

在 list 中,为了清晰起见,我省略了很多包装器内容。我认为相关的关键位是管道连接/创建/读写方法。不要太担心这里的阻塞/线程。

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);
}
}
}
}

C++ 客户端

    // 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");
...

最佳答案

sr.ReadLine() 期望看到换行符以了解行尾。因为它既没有接收到换行符也没有接收到流结束符,所以它等待更多。尝试:

m_pNamedPipe->SendMessage("Hi de hi\n")

或一些 sr.Read() 方法。

关于c# - 如何将命名管道字符串从非托管代码空间发送到托管代码空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com