服务端
DWORD WINAPI ServerThread1(LPVOID)
{
SECURITY_DESCRIPTOR sd;
OBJECT_ATTRIBUTES ObjAttr;
UNICODE_STRING PortName;
NTSTATUS Status;
HANDLE LpcPortHandle = NULL;
BYTE RequestBuffer[sizeof(PORT_MESSAGE) + MAX_LPC_DATA];
BOOL WeHaveToStop = FALSE;
int nError;
__try
{
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
{
nError = GetLastError();
__leave;
}
if(!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE))
{
nError = GetLastError();
__leave;
}
RtlInitUnicodeString(&PortName, LpcPortName);
InitializeObjectAttributes(&ObjAttr, &PortName, 0, NULL, &sd);
_tprintf(_T("Server: Creating LPC port \"%s\" (NtCreatePort) ...\n"), LpcPortName);
Status = NtCreatePort(&LpcPortHandle,
&ObjAttr,
NULL,
sizeof(PORT_MESSAGE) + MAX_LPC_DATA,
0);
_tprintf(_T("Server: NtCreatePort result 0x%08lX\n"), Status);
if(!NT_SUCCESS(Status))
__leave;
while(WeHaveToStop == FALSE)
{
PTRANSFERRED_MESSAGE LpcMessage = NULL;
HANDLE ServerHandle = NULL;
LpcMessage = (PTRANSFERRED_MESSAGE)RequestBuffer;
_tprintf(_T("Server: ------------- Begin loop ----------------------0x%x\n"), Status);
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Listening to LPC port (NtListenPort) ...%s\n"), LpcPortName);
Status = NtListenPort(LpcPortHandle,
&LpcMessage->Header);
_tprintf(_T("Server: NtListenPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Accepting LPC connection (NtAcceptConnectPort)%s ...\n"), LpcPortName);
Status = NtAcceptConnectPort(&ServerHandle,
NULL,
&LpcMessage->Header,
TRUE,
NULL,
NULL);
_tprintf(_T("Server: NtAcceptConnectPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Completing LPC connection (NtCompleteConnectPort) %s...\n"), LpcPortName);
Status = NtCompleteConnectPort(ServerHandle);
_tprintf(_T("Server: NtCompleteConnectPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Receiving LPC data (NtReplyWaitReceivePort)%s ...\n"), LpcPortName);
Status = NtReplyWaitReceivePort(ServerHandle,
NULL,
NULL,
&LpcMessage->Header);
_tprintf(_T("Server: NtReplyWaitReceivePort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
switch(LpcMessage->Command)
{
case LPC_COMMAND_REQUEST_NOREPLY:
_tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
break;
case LPC_COMMAND_REQUEST_REPLY:
_tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
_tprintf(_T("Server: Sending reply (NtReplyPort) %s...\n"), LpcPortName);
Status = NtReplyPort(LpcPortHandle, &LpcMessage->Header);
_tprintf(_T("Server: NtReplyPort result 0x%08lX\n"), Status);
break;
case LPC_COMMAND_STOP:
_tprintf(_T("Server: Stopping ...\n"));
WeHaveToStop = TRUE;
break;
}
}
if(ServerHandle != NULL)
{
_tprintf(_T("Server: Closing the request handle (NtClose) %s...\n"), LpcPortName);
Status = NtClose(ServerHandle);
_tprintf(_T("Server: NtClose result 0x%08lX\n"), Status);
}
_tprintf(_T("Server: ------------- End loop ---------------------- %x\n"), Status);
}
}
__finally
{
if(LpcPortHandle != NULL)
NtClose(LpcPortHandle);
}
return 0;
}
客户端
DWORD WINAPI ServerThread1(LPVOID)
{
SECURITY_DESCRIPTOR sd;
OBJECT_ATTRIBUTES ObjAttr;
UNICODE_STRING PortName;
NTSTATUS Status;
HANDLE LpcPortHandle = NULL;
BYTE RequestBuffer[sizeof(PORT_MESSAGE) + MAX_LPC_DATA];
BOOL WeHaveToStop = FALSE;
int nError;
__try
{
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
{
nError = GetLastError();
__leave;
}
if(!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE))
{
nError = GetLastError();
__leave;
}
RtlInitUnicodeString(&PortName, LpcPortName);
InitializeObjectAttributes(&ObjAttr, &PortName, 0, NULL, &sd);
_tprintf(_T("Server: Creating LPC port \"%s\" (NtCreatePort) ...\n"), LpcPortName);
Status = NtCreatePort(&LpcPortHandle,
&ObjAttr,
NULL,
sizeof(PORT_MESSAGE) + MAX_LPC_DATA,
0);
_tprintf(_T("Server: NtCreatePort result 0x%08lX\n"), Status);
if(!NT_SUCCESS(Status))
__leave;
while(WeHaveToStop == FALSE)
{
PTRANSFERRED_MESSAGE LpcMessage = NULL;
HANDLE ServerHandle = NULL;
LpcMessage = (PTRANSFERRED_MESSAGE)RequestBuffer;
_tprintf(_T("Server: ------------- Begin loop ----------------------0x%x\n"), Status);
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Listening to LPC port (NtListenPort) ...%s\n"), LpcPortName);
Status = NtListenPort(LpcPortHandle,
&LpcMessage->Header);
_tprintf(_T("Server: NtListenPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Accepting LPC connection (NtAcceptConnectPort)%s ...\n"), LpcPortName);
Status = NtAcceptConnectPort(&ServerHandle,
NULL,
&LpcMessage->Header,
TRUE,
NULL,
NULL);
_tprintf(_T("Server: NtAcceptConnectPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Completing LPC connection (NtCompleteConnectPort) %s...\n"), LpcPortName);
Status = NtCompleteConnectPort(ServerHandle);
_tprintf(_T("Server: NtCompleteConnectPort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
_tprintf(_T("Server: Receiving LPC data (NtReplyWaitReceivePort)%s ...\n"), LpcPortName);
Status = NtReplyWaitReceivePort(ServerHandle,
NULL,
NULL,
&LpcMessage->Header);
_tprintf(_T("Server: NtReplyWaitReceivePort result 0x%08lX\n"), Status);
}
if(NT_SUCCESS(Status))
{
switch(LpcMessage->Command)
{
case LPC_COMMAND_REQUEST_NOREPLY:
_tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
break;
case LPC_COMMAND_REQUEST_REPLY:
_tprintf(_T("Server: Received request \"%s\"\n"), LpcMessage->MessageText);
_tprintf(_T("Server: Sending reply (NtReplyPort) %s...\n"), LpcPortName);
Status = NtReplyPort(LpcPortHandle, &LpcMessage->Header);
_tprintf(_T("Server: NtReplyPort result 0x%08lX\n"), Status);
break;
case LPC_COMMAND_STOP:
_tprintf(_T("Server: Stopping ...\n"));
WeHaveToStop = TRUE;
break;
}
}
if(ServerHandle != NULL)
{
_tprintf(_T("Server: Closing the request handle (NtClose) %s...\n"), LpcPortName);
Status = NtClose(ServerHandle);
_tprintf(_T("Server: NtClose result 0x%08lX\n"), Status);
}
_tprintf(_T("Server: ------------- End loop ---------------------- %x\n"), Status);
}
}
__finally
{
if(LpcPortHandle != NULL)
NtClose(LpcPortHandle);
}
return 0;
}
流程图
|