[英]Named pipe client (c++) with c# server
What I want to do is have a named pipe client written in c++ to be able to communicate with a named pipe server written in C#. 我想做的是拥有一个用C ++编写的命名管道客户端,以便能够与用C#编写的命名管道服务器进行通信。 I have been unable to accomplish this so far.
到目前为止,我还无法完成。
CreateFile gives me an invalid handle value, GetLastError returns 2. CreateFile给我一个无效的句柄值,GetLastError返回2。
Here is the c++ part (client) 这是C ++部分(客户端)
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#define PIPE_NAME L"\\\\.\\pipe\\TestPipe"
#define BUFF_SIZE 512
int main()
{
HANDLE hPipe;
hPipe = CreateFile(PIPE_NAME, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr);
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << "INVALID_HANDLE_VALUE" << GetLastError() << endl;
cin.get();
return -1;
}
cout << hPipe << endl;
DWORD mode = PIPE_READMODE_MESSAGE;
SetNamedPipeHandleState(hPipe, &mode, nullptr, nullptr);
bool success = false;
DWORD read;
while(true)
{
TCHAR chBuff[BUFF_SIZE];
do
{
success = ReadFile(hPipe, chBuff, BUFF_SIZE*sizeof(TCHAR), &read, nullptr);
} while (!success);
_tprintf(TEXT("\"%s\"\n"), chBuff);
}
}
And here is the server 这是服务器
using System;
using System.IO.Pipes;
using System.Text;
namespace BasicServer
{
public static class Program
{
private static NamedPipeServerStream _server;
static void Main(string[] args)
{
_server = new NamedPipeServerStream(@"\\.\pipe\TestPipe", PipeDirection.Out, 1, PipeTransmissionMode.Message);
_server.WaitForConnection();
Console.WriteLine(_server.IsConnected);
Console.WriteLine("Client connected\n Sending message");
byte[] buff = Encoding.UTF8.GetBytes("Test message");
_server.Write(buff, 0, buff.Length);
while (true)
{
Console.ReadKey();
Console.Write("\b \b");
}
}
}
}
I have been able to connect with a client written in C# but the c++ -> C# communication should be possible as far as I know. 我已经能够与用C#编写的客户端连接,但是据我所知,应该可以进行c ++-> C#通信。
Here was my test client I wrote in c#, which works 这是我用c#编写的测试客户端,可以工作
using System;
using System.IO.Pipes;
using System.Text;
namespace BasicClientC
{
public class Program
{
private static NamedPipeClientStream client;
static void Main(string[] args)
{
client = new NamedPipeClientStream(@".", @"\\.\pipe\TestPipe", PipeDirection.In);
client.Connect();
byte[] buffer = new byte[512];
client.Read(buffer, 0, 512);
Console.WriteLine(Encoding.UTF8.GetString(buffer));
Console.ReadKey();
}
}
}
So where is my mistake? 那我的错误在哪里?
I know why. 我知道为什么。
When creating your pipe in C#, use just "TestPipe"
as the name of the pipe, don't include \\\\.\\pipe\\
as a prefix for this pipe's name. 在C#中创建管道时,仅使用
"TestPipe"
作为管道名称,请勿在该管道名称的前缀中包括\\\\.\\pipe\\
。
From C++, use the full path to the pipe: "\\\\.\\pipe\\TestPipe"
. 从C ++,使用管道的完整路径:
"\\\\.\\pipe\\TestPipe"
。 Your C++ logic doesn't need a change for this, as you have defined it just fine: L"\\\\\\\\.\\\\pipe\\\\TestPipe"
您的C ++逻辑不需要为此进行更改,因为您已经定义好了:
L"\\\\\\\\.\\\\pipe\\\\TestPipe"
This may help, as well . 这也可能有帮助 。 Funny enough, I ran into this three years ago or more, and now its all coming back to me.
够有趣的是,我在三年前或更早时遇到了这个问题,现在一切都回到了我的脑海。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.