我创建了以下界面
[ServiceContract]
public interface IPlusFive
{
[OperationContract]
int PlusFive(int value);
}
和以下服务器
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();
}
}
}
然后我创建了一个 C# 客户端来测试它,它工作正常。
根据这篇博文,我需要读取一个内存映射文件来获取管道的名称。
所以我写了以下内容来获取名称
#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;
}
给出以下输出
Opening file map..
map opened.
reading file map
File map read successfully
pipe name: ☺
Press any key to continue . . .
似乎内存映射文件在那里,但它看起来不像包含管道的名称。我是 C++ 新手,所以我不知道我做错了什么,或者我想做的事情是否错了。我是否错误地读取了文件?