c# - 命名管道客户端 (C++) 与 C# 服务器

标签 c# c++ windows named-pipes

我想要做的是让一个用 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();
        }
    }
}

那么我的错误在哪里?

最佳答案

我知道为什么。

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

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

This may help, as well 。有趣的是,我在三年多前就遇到过这个问题,现在我又想起来了。

关于c# - 命名管道客户端 (C++) 与 C# 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33133993/

相关文章:

c# - GetPartitions 得到了偏斜的分区?

c++ - 为自定义迭代器特化 std::copy

C++ 概念 : invalid reference to function concept

c# - 有状态微服务中状态的位置

windows - 如何将 Windows 终端添加到 Windows 10 的上下文菜单中?

C#。如何以编程方式授予用户作为服务登录

c# - JWT 如何添加自定义声明和解码声明

c# - OpenIddict 的依赖注入(inject)错误

c# - 我可以使用 SafeHandle 而不是 IntPtr 吗?

c++ - 如何将 SHFileOperation() 与 CString 路径一起使用