c++ 之间的通信和c#通过管道
我想通过管道将数据从 ac# 应用程序发送到 c++ 应用程序。 这是我所做的:
这是 c++ 客户端:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[]) {
HANDLE hFile;
BOOL flg;
DWORD dwWrite;
char szPipeUpdate[200];
hFile = CreateFile(L"\\\\.\\pipe\\BvrPipe", GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
0, NULL);
strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe");
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
printf("CreateFile failed for Named Pipe client\n:" );
}
else
{
flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
if (FALSE == flg)
{
printf("WriteFile failed for Named Pipe client\n");
}
else
{
printf("WriteFile succeeded for Named Pipe client\n");
}
CloseHandle(hFile);
}
return 0;
}
这里是 c# 服务器
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace PipeApplication1{
class ProgramPipeTest
{
public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("\\\\.\\pipe\\BvrPipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
Console.WriteLine("Connection lost");
}
static void Main(string[] args)
{
ProgramPipeTest Server = new ProgramPipeTest();
Thread ServerThread = new Thread(Server.ThreadStartServer);
ServerThread.Start();
}
}
}
当我启动服务器时,然后来自客户端的客户端 GetLastError 返回 2 (系统找不到指定的文件。)
对此有任何想法。 谢谢
I want to send data from a c# application to a c++ application through a pipe.
Here is what I've done:
this is the c++ client:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[]) {
HANDLE hFile;
BOOL flg;
DWORD dwWrite;
char szPipeUpdate[200];
hFile = CreateFile(L"\\\\.\\pipe\\BvrPipe", GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
0, NULL);
strcpy(szPipeUpdate,"Data from Named Pipe client for createnamedpipe");
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD dw = GetLastError();
printf("CreateFile failed for Named Pipe client\n:" );
}
else
{
flg = WriteFile(hFile, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
if (FALSE == flg)
{
printf("WriteFile failed for Named Pipe client\n");
}
else
{
printf("WriteFile succeeded for Named Pipe client\n");
}
CloseHandle(hFile);
}
return 0;
}
and here the c# server
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace PipeApplication1{
class ProgramPipeTest
{
public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("\\\\.\\pipe\\BvrPipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
Console.WriteLine("Connection lost");
}
static void Main(string[] args)
{
ProgramPipeTest Server = new ProgramPipeTest();
Thread ServerThread = new Thread(Server.ThreadStartServer);
ServerThread.Start();
}
}
}
when I start the server and then the client GetLastErrror from client returns 2 (The system cannot find the file specified.)
Any idea on this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据猜测,我想说在服务器中创建管道时不需要“\.\Pipe\”前缀。调用 NamedPipeServerStream 构造函数的示例我看到只是传入管道名称。例如,
您可以使用 SysInternals 进程资源管理器列出打开的管道及其名称。这应该可以帮助您验证管道的名称是否正确。请参阅此询问详细信息。
At a guess, I'd say you don't need the "\.\Pipe\" prefix when creating the pipe in the server. The examples of calling the NamedPipeServerStream constructor that I've seen just pass-in the pipe name. E.g.
You can list the open pipes, and their names, using SysInternals process explorer. This should help you verify that the pipe has the correct name. See this question for details.
请阅读此以了解管道的情况实施的。为什么不使用 CreateNamedPipes API 调用?您将 C++ 端的文件句柄视为普通文件而不是管道。因此,您的 C++ 代码在查找管道时会失败,而实际上它是在尝试从文件中读取数据。
Please read this to see how the pipes are implemented. Why are you not using CreateNamedPipes API call? You are treating the file handle on the C++ side as an ordinary file not a pipe. Hence your C++ code fails when it is looking for a pipe when in fact it was attempting to read from a file.