我创建了以下界面
[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#客户端来测试它,并且工作正常。
根据this博客文章,我需要读取内存映射文件以获取管道的名称。
所以我写了以下内容来获取名称
#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 ++的新手,所以我不知道我做错了什么,或者我想做的事情是错的。我是否错误地阅读了该文件?
答案 0 :(得分:1)