VC++ 之间的命名管道6.0 和 C# Visual Studio 2008

发布于 2024-08-10 04:30:40 字数 698 浏览 7 评论 0原文

我正在做一个项目,其中我同时使用 VC++ 和 C#。 VC++ 用于硬件接口(没有其他选择,我必须仅使用 VC++),而应用程序端我使用 C#(micro Soft Visual Studio 2008)。

为了使两个程序能够相互通信,我使用命名管道(这也是必须的)。

我能够在 C# 和 VC++ 之间进行通信,但不能在 VC++ 与 C# 之间进行通信。

我给出了下面我在 C# 中使用的代码。

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
{
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");

using (StreamReader sr = new StreamReader(pipeStream))
 {
   while ((temp = sr.ReadLine()) != null)                    
   {
      MessageBox.Show(temp));    
   }
 }
}

这里的问题是 sr.ReadLine()。根本没有终止。一旦发现null就应该停止,但是VC++给出的null值在C#中不被视为NULL。

现在我该怎么办?

I am doing a project in which i am using VC++ and C# both. VC++ is for hardware interface (no other go, i must use VC++ only) and the application side i am using C# (micro soft visual studio 2008).

For both the programs to communicate to each other i am using named pipes (this is also must).

I am able to communicate between C# and VC++, but NOT VC++ to C#.

I have given the code below which i am using in C#.

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
{
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");

using (StreamReader sr = new StreamReader(pipeStream))
 {
   while ((temp = sr.ReadLine()) != null)                    
   {
      MessageBox.Show(temp));    
   }
 }
}

The problem here is the sr.ReadLine(). is not terminated at all. It should stop once it finds null, but the null value given by VC++ is not taken as NULL in C#.

Now how should i go about?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-08-17 04:30:40

C++ 中的 NULL 常量实际上只是一个零。 “int”类型的普通零,没有什么特别的。因此,当您将 NULL 写入管道时,您只是传输一个零,仅此而已。 C# 端尽职尽责地读取零作为它正在读取的字符串的一部分。

ReadLine() 在两种情况下终止:

1) 遇到“换行”序列(即 0x0D 0x0A)后。

2) 当底层流结束时(对于这种情况意味着当管道已关闭时)

在第二种情况下,它返回在流关闭之前有机会读取的任何内容,并且任何后续调用 ReadLine() 将返回 null。

因此,为了使 ReadLine() 返回 null,您必须从 VC++ 端关闭管道。

但是,我想这不是您想要做的。因此,我的建议是首先开发并仔细指定要用于通信的协议,然后才开始实际编码接收和发送端。

The NULL constant in C++ is actually just a zero. A plain zero of type "int", nothing special about it. So when you write NULL to the pipe, you're just transmitting a zero, that's all. And the C# side dutifully reads that zero as part of the string it's reading.

ReadLine() terminates in two cases:

1) After encountering a "new line" sequence (that is, 0x0D 0x0A).

2) When the underlying stream has ended (which for this case means, when the pipe has been closed)

In second case, it returns whatever it had a chance to read before the stream was closed, and any subsequent calls to ReadLine() will return null.

Therefore, in order to make ReadLine() return null, you have to close the pipe from VC++ side.

However, I guess this is not what you're trying to do. Therefore, my advice is to first develop and carefully specify the protocol that you're going to use for communication, and only then get to actually coding receiving and transmitting sides.

晚风撩人 2024-08-17 04:30:40

ReadLine 仅当到达文件末尾时才返回 NULL。发送数据后是否关闭客户端的管道?

ReadLine will only return NULL when it reaches the end of file. Do you close the pipe on the client side after sending the data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文