简体   繁体   English

如何使用非托管C ++获取WCF命名管道的名称?

[英]How do I get the name of a WCF Named Pipe using unmanaged C++?

I created the following interface 我创建了以下界面

[ServiceContract]
public interface IPlusFive
{
    [OperationContract]
    int PlusFive(int value);
}

and the following server 和以下服务器

class Program
{
    static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(PlusFiver),
            new Uri[] { new Uri("net.pipe://localhost") }))
        {
            host.AddServiceEndpoint(typeof(IPlusFive), new NetNamedPipeBinding(), "PipePlusFive");
            host.Open();
            Console.WriteLine("Service is Available. Press enter to exit.");
            Console.ReadLine();
            host.Close();
        }
    }
}

Then I created a C# client to test it and that works fine. 然后,我创建了一个C#客户端来对其进行测试,效果很好。

According to this blog post I need to read a memory mapped file to get the name of the pipe. 根据博客文章,我需要读取一个内存映射文件来获取管道的名称。

So I wrote the following to get the name 所以我写了下面的名字

#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char pipeName[] = "EbmV0LnBpcGU6Ly8rL1BJUEVQTFVTRklWRS8=";

wcout << "Opening file map.."<< endl;
std::wstring mapFile;
mapFile.append(L"net.pipe:E");
mapFile.append(L"bmV0LnBpcGU6Ly8rLw==");
HANDLE fileMap = OpenFileMapping(FILE_MAP_READ, FALSE, mapFile.c_str());

if(fileMap == NULL)
{
    wcout << "Failed to connect to pipe" << endl;
    system("pause");
    return 1;
}

wcout << "map opened."<< endl;
wcout << "reading file map" << endl;
LPCTSTR pBuf = (LPTSTR)MapViewOfFile(fileMap,
    FILE_MAP_READ,
    0,
    0,
    0);

if(pBuf == NULL)
{
    wcout << "failed to read file map" << endl;
    CloseHandle(fileMap);
    system("pause");
    return 1;
}

wcout << "File map read successfully" << endl;

wcout << "pipe name: " << pBuf << endl;
MessageBox(NULL, pBuf, TEXT("test"),MB_OK);
system("pause");
UnmapViewOfFile(pBuf);
CloseHandle(fileMap);
return 0;
}

which gives the following output 它给出以下输出

Opening file map..
map opened.
reading file map
File map read successfully
pipe name: ☺
Press any key to continue . . .

It appears that the memory mapped file is there but it doesn't look like it contains the name of the pipe. 似乎内存映射文件在那里,但看起来不像它包含管道的名称。 I'm new to C++ so I don't know if I'm doing something wrong or if what I'm trying to do is wrong. 我是C ++的新手,所以我不知道自己做错了什么还是尝试做错了。 Am I reading the file incorrectly? 我读取文件不正确吗?

A quote from the blog post you refer to: 您引用的博客文章的引文:

Inside these memory mapped files you will find the GUID in binary format starting at the 5th character 在这些内存映射文件中,您会找到以5开头的二进制格式的GUID

You expect that the file is a text one which contains a string. 您希望文件是一个包含字符串的文本文件。 In fact, the file is a binary one which contains GUID structure . 实际上,该文件是包含GUID结构的二进制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM