#include "windows.h"
#include <iostream>
using namespace std;
#define BUF_SIZE 4096
#define EXAMP_PIPE L"\\\\.\\PIPE\\EB3F2E4B_52E2_40F9_A17D_B4A2588F23AB"
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
HANDLE hPipe = NULL;
hPipe = CreateNamedPipe(
EXAMP_PIPE,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUF_SIZE,
BUF_SIZE,
0,
NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
cout << "Create Read Pipe Error" << endl;
return FALSE;
}
cout << "Wait for the connection" << endl;
if (!ConnectNamedPipe(hPipe, NULL))
{
cout << "Connect Failed" << endl;
return FALSE;
}
DWORD dwReturn = 0;
char szBuffer[BUF_SIZE] = { 0 };
cout << "Wait for input" << endl;
cin >> szBuffer;
if (!WriteFile(hPipe, szBuffer, strlen(szBuffer), &dwReturn, NULL))
{
cout << "Write Failed" << endl;
}
memset(szBuffer, 0, BUF_SIZE);
if (ReadFile(hPipe, szBuffer, BUF_SIZE, &dwReturn, NULL))
{
szBuffer[dwReturn] = '\0';
cout << szBuffer << endl;
}
else
{
cout << "Read Failed" << endl;
}
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
return 0;
}