Advertisement
Guest User

PipeTest

a guest
Jan 23rd, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <windows.h>
  4.  
  5.  
  6.  
  7.  
  8. VOID Parent(VOID)
  9. {
  10.   DWORD Written = 0;
  11.   PCHAR DataToWrite = "0xBADFOOD";
  12.   WCHAR ProcessCmdLine[] = L"pipetest.exe --child";
  13.   BOOL Res = FALSE;
  14.   HANDLE hInputReadEnd = INVALID_HANDLE_VALUE;
  15.   HANDLE hInputWriteEnd = INVALID_HANDLE_VALUE;
  16.   STARTUPINFO StartupInfo;
  17.   PROCESS_INFORMATION ProcessInfo;
  18.  
  19.   printf("PARENT: Creating pipe...\n");
  20.   Res = CreatePipe(&hInputReadEnd, &hInputWriteEnd, NULL, 0);
  21.   if (Res) {
  22.     printf("PARENT: Pipe created successfully:\nPARENT: Read end handle: 0x%p\nPARENT: Write end handle: 0x%p\n", hInputReadEnd, hInputWriteEnd);
  23.     printf("PARENT: Duplicating pies's read end in order to make the handle inheritable...\n");
  24.     Res = DuplicateHandle(GetCurrentProcess(), hInputReadEnd, GetCurrentProcess(), &hInputReadEnd, 0, TRUE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
  25.     if (Res) {
  26.       printf("PARENT: Duplicated handle value is 0x%p\n", hInputReadEnd);
  27.       ZeroMemory(&StartupInfo, sizeof(StartupInfo));
  28.       StartupInfo.cb = sizeof(StartupInfo);
  29.       StartupInfo.hStdInput = hInputReadEnd;
  30.       StartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  31.       StartupInfo.dwFlags = STARTF_USESTDHANDLES;
  32.       printf("PARENT: Creating child process...\n");
  33.       Res = CreateProcessW(NULL, (LPWSTR)&ProcessCmdLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, L"E:\\Temp", &StartupInfo, &ProcessInfo);
  34.       if (Res) {
  35.         printf("PARENT: Closing read end handle...\n");
  36.         Res = CloseHandle(hInputReadEnd);
  37.         if (!Res)
  38.           printf("PARENT: Close operation failed: %u\n", GetLastError());
  39.  
  40.         printf("PARENT: Writting data to the pipe...\n");
  41.         Res = WriteFile(hInputWriteEnd, DataToWrite, strlen(DataToWrite), &Written, NULL);
  42.         if (!Res)
  43.           printf("PARENT: Write operation failed: %u\n", GetLastError());
  44.  
  45.         printf("PARENT: That's all, folks!\n");
  46.         getchar();
  47.         CloseHandle(ProcessInfo.hThread);
  48.         CloseHandle(ProcessInfo.hProcess);
  49.         CloseHandle(hInputWriteEnd);
  50.       } else {
  51.         printf("PARENT: Child creation failed: %u\n", GetLastError());
  52.         CloseHandle(hInputReadEnd);
  53.         CloseHandle(hInputWriteEnd);
  54.       }
  55.     } else {
  56.       printf("PARENT: Failed to duplicate the handle: %u\n", GetLastError());
  57.       CloseHandle(hInputWriteEnd);
  58.     }
  59.   } else printf("PARENT: Failed to create pipe: %u\n", GetLastError());
  60.  
  61.   return;
  62. }
  63.  
  64.  
  65. VOID Child(VOID)
  66. {
  67.   DWORD Read = 0;
  68.   CHAR Buffer[255];
  69.   BOOL Res = FALSE;
  70.   HANDLE hInput = INVALID_HANDLE_VALUE;
  71.  
  72.   Sleep(1000);
  73.   printf("CHILD: Retrieving handle for standard input...\n");
  74.   hInput = GetStdHandle(STD_INPUT_HANDLE);
  75.   Res = hInput != NULL;
  76.   if (Res) {
  77.     printf("CHILD: Input handle is 0x%p\n", hInput);
  78.     ZeroMemory(&Buffer, sizeof(Buffer));
  79.     printf("CHILD: Reading input...\n");
  80.     Res = ReadFile(hInput, &Buffer, strlen("0xBADFOOD"), &Read, NULL);
  81.     if (Res) {
  82.       printf("CHILD: Input data: %s\n", &Buffer);
  83.       printf("CHILD: That's all, folks!\n");
  84.       getchar();
  85.     } else printf("CHILD: Failed to read input data: %u\n", GetLastError());
  86.   } else printf("CHILD: Failed to get input handle: %u\n", GetLastError());
  87.  
  88.   return;
  89. }
  90.  
  91.  
  92. int main(int argc, char **argv)
  93. {
  94.   if (argc == 2) {
  95.     if (strncmp(argv[1], "--parent", strlen("--parent")) == 0)
  96.       Parent();
  97.     else if (strncmp(argv[1], "--child", strlen("--child")) == 0)
  98.       Child();
  99.     else printf("PIPETEST: Invalid argument \"%s\"\n", argv[1]);
  100.   } else printf("PIPETEST: The process accepts one argument only\n");
  101.  
  102.   getchar();
  103.  
  104.   return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement