|
| 1 | + |
| 2 | + |
| 3 | +// ipc-client-win.c |
| 4 | + |
| 5 | + |
| 6 | + #include <windows.h> |
| 7 | + #include <stdio.h> |
| 8 | + |
| 9 | + |
| 10 | + int main(void){ |
| 11 | + |
| 12 | + const char *pipeName = "\\\\.\\pipe\\mypipe"; |
| 13 | + printf("Connecting to server...\n"); |
| 14 | + HANDLE hPipe = CreateFileA( |
| 15 | + pipeName, |
| 16 | + GENERIC_READ | GENERIC_WRITE, // read + write |
| 17 | + 0, // no sharing |
| 18 | + NULL, // default security |
| 19 | + OPEN_EXISTING, // must already exist |
| 20 | + 0, // default attributes |
| 21 | + NULL // no template |
| 22 | + ); |
| 23 | + |
| 24 | + if(hPipe==INVALID_HANDLE_VALUE){ |
| 25 | + printf("CreateFile failed with error %lu\n", GetLastError()); |
| 26 | + return 1; |
| 27 | + } |
| 28 | + printf("Connected to server.\n"); |
| 29 | + // Read message from server |
| 30 | + char buffer[256]; |
| 31 | + DWORD bytesRead; |
| 32 | + if(ReadFile(hPipe,buffer,sizeof(buffer)-1,&bytesRead,NULL)){ |
| 33 | + buffer[bytesRead] = '\0'; |
| 34 | + printf("Received from server: %s\n", buffer); |
| 35 | + } else { |
| 36 | + printf("ReadFile failed with error %lu\n", GetLastError()); |
| 37 | + CloseHandle(hPipe); |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + // Send reply to server |
| 42 | + const char *reply = "Hello back from client!"; |
| 43 | + DWORD bytesWritten; |
| 44 | + if(!WriteFile(hPipe,reply,(DWORD)strlen(reply),&bytesWritten,NULL)){ |
| 45 | + printf("WriteFile failed with error %lu\n",GetLastError()); |
| 46 | + CloseHandle(hPipe); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + printf("Sent reply to server.\n"); |
| 50 | + |
| 51 | + CloseHandle(hPipe); |
| 52 | + |
| 53 | + |
| 54 | + return 0; |
| 55 | + |
| 56 | + }//main |
| 57 | + |
| 58 | + |
0 commit comments