LPC通信连接建立成功后NtReplyPort失败

yjip267 2016-10-25 10:24:46
LPC通信连接建立成功后,客户进程向服务进程发送消息NtReplyPort失败 错误值0xc000000d,就两个参数。不知道哪里错误了。

服务代码:
DWORD CdirveInjectDlg::NewlpcServer(LPVOID lpParam)
{
CdirveInjectDlg *pThis = (CdirveInjectDlg*)lpParam;

HANDLE hserverPort;
HANDLE hAcceptPort;
OBJECT_ATTRIBUTES oa;//此结构可以看msdn
TLPC_PORTMSG pmRecv;
TLPC_PORTMSG pmSend;
NTSTATUS status;
TCHAR outInfo[MAX_PATH] = { 0 };

do
{
//初始化Unicode字串
pThis->meRtlInitUnicodeString(&pThis->portName, L"\\RPC Control\\DEMO PORT");
//定义并填充结构体
InitializeObjectAttributes(&oa, &pThis->portName, /*0*/OBJ_CASE_INSENSITIVE| OBJ_FORCE_ACCESS_CHECK, NULL,NULL);
//创建LPC server port
status = pThis->meNtCreatePort(&hserverPort, &oa, TLPC_MAX_MSG_DATA_LENGTH,sizeof(TLPC_PORTMSG), 0);
if (!NT_SUCCESS(status)) { OutputDebugString("NtCreatePort fail\r\n");break; }
else{ OutputDebugString("NtCreatePort succeed\r\n"); }
status = pThis->meNtListenPort(hserverPort, (PPORT_MESSAGE)(&pmRecv));
if (!NT_SUCCESS(status)) { OutputDebugString("NtListenPort fail\r\n");break; }
//////连接消息
sprintf_s(outInfo, "连接消息格式\nprocessID:%08x\nthreadID%08x\nMessageType %d\nMessageSize %d\nMessageID %x\nDataSize %d\n\n",
pmRecv.header.ClientId.UniqueProcess,
pmRecv.header.ClientId.UniqueThread,
pmRecv.header.u2.s2.Type,
pmRecv.header.u1.s1.TotalLength,
pmRecv.header.MessageId,
pmRecv.header.u1.s1.DataLength);
OutputDebugString(outInfo);
sprintf_s(outInfo, "请求内容:\r\n%s\r\n", pmRecv.Data);
OutputDebugString(outInfo);
//接收连接
status = pThis->meNtAcceptConnectPort(&hAcceptPort, 0, /*&pmSend.header*/(PPORT_MESSAGE)&pmRecv, TRUE, 0, 0);
if (!NT_SUCCESS(status)) { OutputDebugString("NtAcceptConnectPort fail\r\n");break; }
else { OutputDebugString("NtAcceptConnectPort succeed\r\n");}
//唤醒客户
status = pThis->meNtCompleteConnectPort(hAcceptPort);
if (!NT_SUCCESS(status)) { OutputDebugString("NtCompleteConnectPort fail\r\n");break; }
//握手完成之后就开始循环与client通信了
while (true)
{
OutputDebugString("等待客户端消息...\r\n");
status = pThis->meNtReplyWaitReceivePort(hAcceptPort, 0, 0, (PPORT_MESSAGE)&pmRecv);
if (!NT_SUCCESS(status)) { OutputDebugString("NtReplyWaitReceivePort fail\r\n");break; }
sprintf_s(outInfo, "收到客户端%d新消息:\n%s\n", pmRecv.header.ClientId.UniqueProcess, pmRecv.Data);
OutputDebugString(outInfo);
}

} while (FALSE);


return 0;
}


//客户
DWORD ClpcclDlg::lpcthreadCL(LPVOID lpParam)
{
ClpcclDlg * pThis = (ClpcclDlg*)lpParam;
TCHAR outInfo[MAX_PATH] = { 0 };
HANDLE portServer = NULL;
SECURITY_QUALITY_OF_SERVICE SecurityQos;
NTSTATUS status;
UNICODE_STRING portName;
TLPC_PORTMSG pmRecv;
TLPC_PORTMSG pmSend;
memset(&pmSend, 0, sizeof(pmSend));
memset(&pmRecv, 0, sizeof(pmRecv));
ULONG ConnectInfoSize = sizeof(TLPC_PORTMSG);

do
{
pThis->meRtlInitUnicodeString(&portName, L"\\RPC Control\\DEMO PORT");
SecurityQos.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
SecurityQos.EffectiveOnly = TRUE/*FALSE*/;
SecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
SecurityQos.ImpersonationLevel = SecurityImpersonation;
sprintf_s(outInfo, "connect %ws ...\r\n", portName.Buffer);
OutputDebugString(outInfo);
memcpy(pmRecv.Data,"HAOYJIP", TLPC_MAX_MSG_DATA_LENGTH);
//通过该服务,客户进程可通过名称来连接到一个服务器进程
status = pThis->meNtConnectPort(&portServer,
&(portName),
&SecurityQos,
0, // No attributes
0,
0,
(char *)&pmRecv,
&ConnectInfoSize
);
if (!NT_SUCCESS(status))
{
sprintf_s(outInfo, "NtConnectPort Failed:%x\n", status);
OutputDebugString(outInfo); break;
}
OutputDebugString("client connct port ok!\r\n");
//pmSend.h.u1.s1.DataLength = strlen("12345689")+1/*TLPC_MAX_MSG_DATA_LENGTH*/;
//pmSend.h.u1.s1.TotalLength = sizeof(PORT_MESSAGE)+ strlen("12345689")+1/*TLPC_MAX_MSG_DATA_LENGTH*/;
//pmSend.h.u2.s2.Type = LPC_REQUEST;
strcpy_s((PSTR)pmSend.Data , TLPC_MAX_MSG_DATA_LENGTH, "12345689");
//发送一个请求消息 ,并且等待此请求的应答消息
// status = pThis->meNtRequestWaitReplyPort(portServer, (PPORT_MESSAGE)&pmSend, (PPORT_MESSAGE)&pmRecv);
status = pThis->meNtReplyPort(portServer, (PPORT_MESSAGE)&pmSend);
Sleep(500000);
CloseHandle(portServer);
} while (false);

return 0;
}
...全文
959 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-10-26
  • 打赏
  • 举报
回复
GetLastError();
yjip267 2016-10-26
  • 打赏
  • 举报
回复
GetLastError();这个没用。 这里用的未公开的API。所以这个获取不到。错误值0xc000000d,说的是参数问题。但是不知道参数哪里有问题。

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧