1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <windows.h> #include <process.h> #include <cstring> #include <stdio.h>
int main() { HANDLE hPipeRead; HANDLE hPipeWrite = CreateNamedPipe(TEXT("\\\\.\\Pipe\\test_write"), PIPE_ACCESS_DUPLEX, PIPE_READMODE_BYTE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, NULL); STARTUPINFO si = {0}; ZeroMemory(&si, sizeof(STARTUPINFO)); PROCESS_INFORMATION pi = {0}; BOOL bSuccess = CreateProcess(TEXT("child.exe"), NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); if (ConnectNamedPipe(hPipeWrite, NULL)) { printf("Connected\n"); printf("Send signal? [Y/n]\n"); char ch; scanf("%c",&ch); if (ch == 'y' || ch == 'Y') { DWORD dwWrite; const char* ptr = "start"; if (!WriteFile(hPipeWrite, ptr, strlen(ptr), &dwWrite, NULL)) { printf("ERROR\n"); } printf("Signal sent success\n"); } Sleep(1000); hPipeRead = CreateFile(TEXT("\\\\.\\Pipe\\test_read"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); char buffer[4096] = {0}; DWORD dwRead = 0; BOOL rSuccess = false; while (TRUE) { rSuccess = ReadFile(hPipeRead, buffer, 4096, &dwRead, NULL); if (!rSuccess) { break; } printf("Received: %s", buffer); } } DisconnectNamedPipe(hPipeWrite); CloseHandle(hPipeWrite); DisconnectNamedPipe(hPipeRead); CloseHandle(hPipeRead); system("pause"); return 0; }
|