本文介绍了如何使命名管道在c ++和.NET之间工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚有一个非常艰难的时候使命名管道工作之间的c + +和.NET。
创建命名管道在2个c ++应用程序或2个.NET应用程序之间工作没有问题。
I just had a really tough time making Named Pipes work between c++ and .NET.I had no problems creating Named Pipes that worked between 2 c++ apps, or between 2 .NET apps.
推荐答案
我没有这个通信的问题,我在一些项目中使用这种情况。
I dont have problem with this communication, i use this scenario in some project.
C ++端:
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\pipename");
CHAR chReadBuf[1024];
DWORD cbRead;
BOOL fResult;
fResult = CallNamedPipe(
lpszPipename, // pipe name
_Message, // message to server
strlen(_Message), // message length
chReadBuf, // buffer to receive reply
sizeof(chReadBuf), // size of read buffer
&cbRead, // number of bytes read
NMPWAIT_WAIT_FOREVER); // wait;-)
在C#端:
public string GetMessageFromPipe()
{
int _lenght = 0;
/*
* Pipe Control Block
*/
_pipeserver.WaitForConnection();
do
{
_lenght += _pipeserver.Read(_buffer, _lenght, _buffer.Length);
}
while (!_pipeserver.IsMessageComplete);
_pipeserver.Disconnect();
/*
* End of Pipe Control Block
*/
if (_lenght == 0)
{
throw new ArgumentException("Message is empty ;-(");
}
return Encoding.UTF8.GetString(_buffer, 0, _lenght);
}
管道创建:
_pipeserver = new NamedPipeServerStream("pipename",
PipeDirection.InOut, 254, PipeTransmissionMode.Message,
PipeOptions.Asynchronous, 262144, 262144);
这篇关于如何使命名管道在c ++和.NET之间工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!