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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <windows.h> #include <process.h> #include<stdio.h>
int main() { HANDLE hParentRead, hParentWrite, hChildRead, hChildWrite; SECURITY_ATTRIBUTES sa = {0}; ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.bInheritHandle = TRUE; sa.nLength = sizeof(SECURITY_ATTRIBUTES); if (!CreatePipe(&hParentRead, &hChildWrite, &sa, 0)) { printf("Pipe failed."); } if (!CreatePipe(&hChildRead, &hParentWrite, &sa, 0)) { printf("Pipe failed."); } STARTUPINFO si = {0}; ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.wShowWindow = SW_SHOWDEFAULT; si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.hStdInput = hChildRead; si.hStdOutput = hChildWrite; si.hStdError = HANDLE(STD_ERROR_HANDLE); PROCESS_INFORMATION pi = {0}; printf("Create child process..."); Sleep(50); BOOL bSuccess = CreateProcess(TEXT("child.exe"), NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi); printf("Send signal? [Y/n]\n"); char ch; scanf("%c",&ch); if (ch == 'y' || ch == 'Y') { printf("Send signal...\n"); Sleep(50); char buffer[4096] = {'y', 0}; DWORD dwBytesWritten; if (!WriteFile(hParentWrite, buffer, strlen(buffer) * 2, &dwBytesWritten, NULL)) { printf("Sent failed"); } printf("Send success.\n"); Sleep(50); char result[4096] = {0}; int flag = 0; while (TRUE) { memset(result, 0, sizeof(result)); DWORD dwReadSize; if (!ReadFile(hParentRead, result, 4096, &dwReadSize, NULL)) { continue; } if (result[0] == 'e') { printf("%s\n", result); Sleep(50); if (flag < 5) { continue; } else { break; } } else { printf("Received: %s\n", result); Sleep(50); ++flag; } } printf("Success."); } CloseHandle(hParentRead); CloseHandle(hParentWrite); CloseHandle(hChildRead); CloseHandle(hChildWrite); system("pause"); return 0; }
|