1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <windows.h>
5 #include <tchar.h>
6 
7 #define BUFSIZE 1024
8 #define PIPE_TIMEOUT 1000
9 
10 VOID InstanceThread (LPVOID);
11 
12 VOID
13 GetAnswerToRequest(LPTSTR lpRequest,
14 		   LPTSTR lpReply,
15 		   LPDWORD lpcbReplyBytes)
16 {
17 }
18 
19 VOID MyErrExit(LPTSTR Message)
20 {
21 //	MessageBox(NULL, Message, NULL, MB_OK);
22    puts(Message);
23    ExitProcess(0);
24 }
25 
26 
27 
28 
29 int xx = 0;
30 
31 int main(int argc, char *argv[])
32 {
33    BOOL fConnected;
34    DWORD dwThreadId;
35    HANDLE hPipe, hThread;
36    LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe");
37 
38 //   for (;;)
39 //     {
40 	hPipe = CreateNamedPipe(lpszPipename,
41 				PIPE_ACCESS_DUPLEX,
42 				PIPE_TYPE_MESSAGE |
43 				PIPE_READMODE_MESSAGE |
44 				PIPE_WAIT,
45 				PIPE_UNLIMITED_INSTANCES,
46 				BUFSIZE,
47 				BUFSIZE,
48 				PIPE_TIMEOUT,
49 				NULL);
50 	if (hPipe == INVALID_HANDLE_VALUE)
51 	  {
52 	     printf("CreateNamedPipe() failed\n");
53 	     return 0;
54 	  }
55 
56 	fConnected = ConnectNamedPipe(hPipe,
57 				      NULL) ? TRUE : (GetLastError () ==
58 					    ERROR_PIPE_CONNECTED);
59 	if (fConnected)
60 	  {
61 	     printf("Pipe connected!\n");
62 
63 	     DisconnectNamedPipe(hPipe);
64 
65 #if 0
66 	     hThread = CreateThread(NULL,
67 				    0,
68 				    (LPTHREAD_START_ROUTINE) InstanceThread,
69 				    (LPVOID) hPipe,
70 				    0,
71 				    &dwThreadId);
72 	     if (hThread == NULL)
73 	       MyErrExit("CreateThread");
74 #endif
75 	  }
76 	else
77 	  {
78 //	     CloseHandle(hPipe);
79 	  }
80 //     }
81 
82    CloseHandle(hPipe);
83 
84    return 0;
85 }
86 
87 VOID InstanceThread (LPVOID lpvParam)
88 {
89    CHAR chRequest[BUFSIZE];
90    CHAR chReply[BUFSIZE];
91    DWORD cbBytesRead, cbReplyBytes, cbWritten;
92    BOOL fSuccess;
93    HANDLE hPipe;
94 
95    hPipe = (HANDLE)lpvParam;
96    while (1)
97      {
98 	fSuccess = ReadFile(hPipe,
99 			    chRequest,
100 			    BUFSIZE,
101 			    &cbBytesRead,
102 			    NULL);
103 	if (!fSuccess || cbBytesRead == 0)
104 	  break;
105 
106 	GetAnswerToRequest(chRequest, chReply, &cbReplyBytes);
107 
108 	fSuccess = WriteFile(hPipe,
109 			     chReply,
110 			     cbReplyBytes,
111 			     &cbWritten,
112 			     NULL);
113 	if (!fSuccess || cbReplyBytes != cbWritten)
114 	  break;
115     }
116 
117    FlushFileBuffers(hPipe);
118    DisconnectNamedPipe(hPipe);
119    CloseHandle(hPipe);
120 }
121