1
我創建了以下接口如何獲得使用非託管C++的WCF命名管道的名稱?
[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++的新手,所以我不知道我是否做錯了什麼,或者如果我想做的事是錯誤的。我是否正確讀取文件?