如何使用命名管道从c调用WCF方法?

更新:
通过协议 here,我无法弄清楚未知的信封记录.我在网上找不到任何例子.

原版的:

我有以下WCF服务

static void Main(string[] args)
    {
        var inst = new PlusFiver();
        using (ServiceHost host = new ServiceHost(inst,new Uri[] { new Uri("net.pipe://localhost") }))
        {
            host.AddServiceEndpoint(typeof(IPlusFive),new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),"PipePlusFive");
            host.Open();
            Console.WriteLine("Service is Available. Press enter to exit.");
            Console.ReadLine();
            host.Close();
        }
    }

   [ServiceContract]
   public interface IPlusFive
   {
       [OperationContract]
       int PlusFive(int value);
   }


   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
   public class PlusFiver : IPlusFive
   {
      public int PlusFive(int value)
      {
          Console.WriteLine("Adding 5 to " + value);
          return value + 5;
      }      
   }

我输出添加5行,所以我知道服务器是否处理了
请求与否.

我有一个.NET客户端,我曾经测试这一切,一切正常工作
预期.

现在我想为这个做一个非托管的C客户端.

我想出了如何得到管道的名称,并写信给它.
我从here下载了协议

我可以写信给管道,但我看不懂.每当我尝试读取它,我得到一个ERROR_BROKEN_PIPE 109(0x6D)管道已经结束.错误.如果我用写入替换读取,则写入是成功的,所以我不认为管道是封闭的,至少直到我尝试读取.

这是我如何连接到管道.

HANDLE OpenPipe(OLECHAR* bstrGuid)
{
    wstring pipeName = L"\\\\.\\pipe\\";
    wstring strGuid = bstrGuid;
    pipeName.append(strGuid.substr(1,36));
    wcout << "Pipe Name " << endl;
    wcout << pipeName.c_str() << endl;

    HANDLE hPipe = CreateFile(pipeName.c_str(),GENERIC_WRITE | GENERIC_READ,FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL);
    if(hPipe == INVALID_HANDLE_VALUE)
    {
        wcout << "failed to create pipe" << endl;
        system("pause");
        return NULL;
    }
    return hPipe;
}

这是我正在创建我发送的第一条消息

std::list<wchar_t> GetFirstMessage()
{
    std::list<wchar_t> message;
    message.push_back(0x00);// version record
    message.push_back(0x01);// major version
    message.push_back(0x00);// minor version
    message.push_back(0x01);// mode record
    message.push_back(0x01);// singleton-unsized mode
    message.push_back(0x02);// via record

    wstring url = L"net.pipe://localhost/PipePlusFive";
    message.push_back(url.length());// via length
    for(int x= 0;x<url.length();x++)
    {
        message.push_back(url[x]); // via
    }
    message.push_back(0x03);
    message.push_back(0x08);
    return message;
}

这是我如何写文件.

int WriteMessage(HANDLE hPipe,LPVOID message,int size)
{
    DWORD bytesWritten;

    BOOL bWrite = WriteFile(hPipe,&message,size,&bytesWritten,NULL);
    wcout << "Bytes Written: " << bytesWritten << endl;
    if(bWrite == false)
    {
        wcout << "fail"<<endl;
        CloseHandle(hPipe);
        system("pause");
        return 1;
    }
    return 0;
}

    list<wchar_t> full_message = GetFirstMessage();
int result = WriteMessage(hPipe,&full_message,full_message.size());   
if (result == 1)
{ return 1;}

这是我如何写最后的消息

wchar_t message = 12;
result = WriteMessage(hPipe,1);
if (result == 1)
{ return 1;}

这是我如何读取响应

char buffer[10];
DWORD bytesRead;
BOOL bRead = ReadFile(hPipe,buffer,1,&bytesRead,NULL);
if(bRead == false)
{
    wcout << "fail read"<<endl;
    wcout << "error: " << GetLastError() << endl;
    CloseHandle(hPipe);
    system("pause");
    return 1;
}

我是新来的c,所以我不知道我是不是正确地遵循协议,或者是以我这样做的方式犯了一个愚蠢的错误?

更新:
问题是我正在将命名地址写入命名管道而不是列表的内容.我已经解决了,我现在可以阅读前言Ack记录.现在我必须弄清楚下一部分协议需要发送什么.

解决方法

检查这是否适用于您

>尝试打开命名管道. (的CreateFile)>设置指定命名管道的读取模式和阻塞模式. (SetNamedPipeHandleState)>向管道服务器发送消息并接收其响应. (WriteFile,ReadFile)>关闭管道. (CloseHandle的)

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...