CLIENT:
DWORD WINAPI PipeThreadProc(LPVOID lpParameter)
{
HANDLE hPipe;
//Connect to the server pipe using CreateFile()
hPipe = CreateFile(
g_szPipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (INVALID_HANDLE_VALUE == hPipe)
{
}
else
{
}
//We are done connecting to the server pipe,
//we can start communicating with the server using ReadFile()/WriteFile()
//on handle - hPipe
char szBuffer[1024]={"ExitProcess"};
DWORD cbBytes;
//Send the message to server
BOOL bResult = WriteFile(
hPipe, // handle to pipe
szBuffer, // buffer to write from
strlen(szBuffer)+1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ( (!bResult) || (strlen(szBuffer)+1 != cbBytes))
{
CloseHandle(hPipe);
return 1; //Error
}
else
{
OutputDebugString("WriteFile() was successful.");
}
CloseHandle(hPipe);
return 0;
}
SERVER:
DWORD WINAPI PipeThreadProc(LPVOID lpParameter)
{
HANDLE hPipe;
char szBuffer[1024];
DWORD cbBytes;
BOOL bResult;
hPipe = CreateNamedPipe(
g_szPipeName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
1024, // output buffer size
1024, // input buffer size
NMPWAIT_USE_DEFAULT_WAIT, // client time-out
NULL); // default security attribute
if (INVALID_HANDLE_VALUE == hPipe)
{
ShowErrorMessage("CreateNamedPipe");
return 1; //Error
}
else
{
printf("/nCreateNamedPipe() was successful.");
}
printf("/nWaiting for client connection...");
//Wait for the client to connect
while (1)
{
OutputDebugString("/nWaiting for client connection...");
if (FALSE == ConnectNamedPipe(hPipe, NULL))
{
ShowErrorMessage("ConnectNamedPipe");
CloseHandle(hPipe);
return 1; //Error
}
else
{
OutputDebugString("/nConnectNamedPipe() was successful.");
}
//We are connected to the client.
//To communicate with the client we will use ReadFile()/WriteFile()
//on the pipe handle - hPipe
//Read client message
bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ( (!bResult) || (0 == cbBytes))
{
// printf("/nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
// printf("/nReadFile() was successful.");
OutputDebugString(szBuffer);
if (lstrcmpi(szBuffer,"ExitProcess")==0)
{
OutputDebugString("SnedMessage");
SendMessage(_hThisWnd, WM_DESTROY, 0, 0L);
}else if (lstrcmpi(szBuffer,"LoadSettings")==0)
{
OutputDebugString("LoadSettings command");
//LoadAllSettings();
LoadQuickScrllSetting();
}
OutputDebugString("pelmiser------PipeTimerProc end");
}
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
// CloseHandle(hPipe);
}
}