【问题标题】:Named pipe client (c++) with c# server带有 c# 服务器的命名管道客户端 (c++)
【发布时间】:2015-10-14 19:29:59
【问题描述】:

我想做的是有一个用 C++ 编写的命名管道客户端,以便能够与用 C# 编写的命名管道服务器进行通信。到目前为止,我一直无法做到这一点。

CreateFile 给我一个无效的句柄值,GetLastError 返回 2。

这里是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);
    }
}

这里是服务器

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

我已经能够与用 C# 编写的客户端连接,但据我所知,c++ -> C# 通信应该是可能的。

这是我用 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();
        }
    }
}

那么我的错误在哪里?

【问题讨论】:

    标签: c# c++ windows named-pipes


    【解决方案1】:

    我知道为什么。

    1. 在 C# 中创建管道时,仅使用 "TestPipe" 作为管道名称,不要将 \\.\pipe\ 作为管道名称的前缀。

    2. 在 C++ 中,使用管道的完整路径:"\\.\pipe\TestPipe"。您的 C++ 逻辑不需要为此进行更改,因为您已经很好地定义了它:L"\\\\.\\pipe\\TestPipe"

    This may help, as well。有趣的是,我在三年或更长时间前遇到过这种情况,现在一切都回到了我的身边。

    【讨论】:

    • 好的,你解决了连接问题,但我现在明白了,prolly 没有正确解释它我认为:S i.gyazo.com/77ec6b69d14aec6ad7a0cc3f5168f8fe.png 其中 0xA0 是 HANDLE hPipe 的值
    • @HyunMi 我注意到你在 C++ 逻辑中使用了TCHAR。您的应用程序是否针对 Unicode 字符?
    • 是的,我只是忘记在我的测试服务器中更改它,:对不起
    • @HyunMi 如果您通过管道发送 Unicode 字符,您可能需要在命令提示符的设置中更改字体才能查看它们。查看更多详情:*.com/questions/22578567/…
    最近更新 更多