Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "exercicio7.h"
  3.  
  4. #define BUFFSIZE 4098
  5.  
  6. BOOL _CreateProcess();
  7.  
  8. // child read
  9. HANDLE Child_rd;
  10. // father write
  11. HANDLE Father_wr;
  12.  
  13. // father read
  14. HANDLE Father_rd;
  15. // father write
  16. HANDLE Child_wr;
  17.  
  18. /* Father console read -> Father pipe write -> Child pipe read -> Child console write */
  19.  
  20. int _tmain(VOID){
  21. HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
  22. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  23.  
  24. CHAR buff[BUFFSIZE];
  25.  
  26. DWORD sizeOfRead;
  27. DWORD sizeOfWrite;
  28.  
  29. OVERLAPPED isOverlapped = {0};
  30. isOverlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, "ReadParent");
  31.  
  32. SECURITY_ATTRIBUTES saAttr;
  33.  
  34. // Set the bInheritHandle flag so pipe handles are inherited.
  35.  
  36. saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  37. saAttr.bInheritHandle = TRUE;
  38. saAttr.lpSecurityDescriptor = NULL;
  39.  
  40. // Define the channels for the comunication to happen
  41. // The child should not inherit the Fathers Handles
  42.  
  43. if (!CreatePipe(&Child_rd, &Father_wr, &saAttr, 0))
  44. printf("Failed to CreatePipe");
  45.  
  46. if (!SetHandleInformation(Father_wr, HANDLE_FLAG_INHERIT, 0))
  47. printf("Failed to SetHandleInformation");
  48.  
  49. if (!CreatePipe(&Father_rd, &Child_wr, &saAttr, 0))
  50. printf("Failed to CreatePipe");
  51.  
  52. if (!SetHandleInformation(Father_rd, HANDLE_FLAG_INHERIT, 0))
  53. printf("Failed to SetHandleInformation");
  54.  
  55. if (!_CreateProcess())
  56. printf("Failed to CreateProcess");
  57.  
  58. // Comunicate
  59.  
  60. while (true){
  61. ReadFile(in, buff, BUFFSIZE, &sizeOfRead, &isOverlapped);
  62. //WaitForSingleObject(isOverlapped.hEvent, INFINITE);
  63. WriteFile(Father_wr, buff, sizeOfRead, &sizeOfWrite, NULL);
  64.  
  65. ReadFile(Father_rd, buff, BUFFSIZE, &sizeOfRead, &isOverlapped);
  66. //WaitForSingleObject(isOverlapped.hEvent, INFINITE);
  67. WriteFile(out, buff, sizeOfRead, &sizeOfWrite, NULL);
  68. }
  69. }
  70.  
  71. BOOL _CreateProcess(){
  72.  
  73. // Location of the Child executable file
  74.  
  75. PCHAR file = "H:\\Dropbox\\LEIC\\1415i_SO\\Trabalho2\\Exemplos Windows\\Debug\\Exercicio7-Child.exe";
  76.  
  77. // Set Process Information
  78.  
  79. PROCESS_INFORMATION pi;
  80.  
  81. ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
  82.  
  83. // Set Start up information
  84.  
  85. STARTUPINFO si;
  86.  
  87. ZeroMemory(&si, sizeof(STARTUPINFO));
  88.  
  89. si.cb = sizeof(STARTUPINFO);
  90. si.hStdError = NULL;
  91. //si.hStdOutput = Father_wr;
  92. //si.hStdInput = Child_rd; // **
  93. si.dwFlags |= STARTF_USESTDHANDLES;
  94.  
  95. // Child handles location -> String
  96.  
  97. TCHAR cmdLineArgs[64];
  98.  
  99. _tprintf("Child read: %p \nChild write: %p\n", Child_rd, Child_wr);
  100.  
  101. _stprintf(cmdLineArgs, _T("Exercicio7-Child.exe %d %d"), Child_rd, Child_wr);
  102.  
  103. _tprintf(_T("Args sent to Child Process: %s"), cmdLineArgs);
  104.  
  105. // Create the Process - second field is for parameters
  106.  
  107. return CreateProcess(file, cmdLineArgs, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement