2012-08-01 77 views
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++的新手,所以我不知道我是否做错了什么,或者如果我想做的事是错误的。我是否正确读取文件?

回答

1

从你指的博客文章引述:

里面,这些内存映射文件,你会发现GUID二进制格式在第5个字符开始

您希望该文件一个包含字符串的文本。 实际上,该文件是一个包含GUID structure的二进制文件。