sco*_*ott 10 c++ wcf winapi named-pipes
更新:在这里查看协议,我无法弄清楚Unsized Envelope Record中的内容.我在网上找不到任何例子.
原版的:
我有以下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;
}
}
Run Code Online (Sandbox Code Playgroud)
我输出添加5行,所以我知道服务器是否处理了请求.
我有一个.NET客户端,我曾经用它来测试它,一切都按预期工作.
现在我想为此创建一个非托管的C++客户端.
我想出了如何获取管道的名称,并写入它.
我从这里下载了协议
我可以写到管道,但我无法读到它.每当我尝试从中读取时,我都会收到ERROR_BROKEN_PIPE 109 (0x6D) The pipe has been ended.错误消息.如果我用write替换读取,写入成功,所以我不认为管道是关闭的,至少在我尝试读取之前.
这是我如何连接到管道.
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, NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
wcout << "failed to create pipe" << endl;
system("pause");
return NULL;
}
return hPipe;
}
Run Code Online (Sandbox Code Playgroud)
这就是我正在创建我发送的第一条消息
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;
}
Run Code Online (Sandbox Code Playgroud)
这就是我将它写入文件的方式.
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;}
Run Code Online (Sandbox Code Playgroud)
以下是我编写结束消息的方法
wchar_t message = 12;
result = WriteMessage(hPipe, &message, 1);
if (result == 1)
{ return 1;}
Run Code Online (Sandbox Code Playgroud)
这是我试图阅读响应的方式
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;
}
Run Code Online (Sandbox Code Playgroud)
我是c ++的新手,所以我不知道我是不是正确地遵循了协议,或者在我尝试这样做的过程中犯了一个愚蠢的错误?
更新:问题是我正在将指针地址写入命名管道而不是列表的内容.我已经解决了这个问题而且我现在能够阅读Preamble Ack Record.现在我必须弄清楚需要为协议的下一部分发送什么.
小智 0
检查这是否适合您
| 归档时间: |
|
| 查看次数: |
1833 次 |
| 最近记录: |