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.
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. Am I reading the file incorrectly?
A quote from the blog post you refer to:
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.