Advertisement
3axap_010

IPC.cpp

Mar 18th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #include "Stack.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. HANDLE CreateNewEvent(LPCSTR lpName)
  7. {
  8.     HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, lpName);
  9.  
  10.     if (hEvent == NULL)
  11.     {
  12.         fprintf(stderr, "Can't create an event, error: %d\n", GetLastError());
  13.         return NULL;
  14.     }
  15.  
  16.     return hEvent;
  17. }
  18.  
  19. TCHAR* GenerateCmdArgs(const TCHAR* tPattern, const TCHAR* tEventName)
  20. {
  21.     TCHAR* tCmdLine = (TCHAR*)malloc(sizeof(TCHAR) * (strlen(tEventName) + strlen(tPattern) + 1));
  22.     strncpy(tCmdLine, tPattern, strlen(tPattern) + 1);
  23.     strncat(tCmdLine, tEventName, strlen(tEventName));
  24.  
  25.     return tCmdLine;
  26. }
  27.  
  28. TCHAR* GenerateEventName(const TCHAR* tEventNamePattern, int nChildCount)
  29. {
  30.     int lenght = snprintf(NULL, 0, "%d", nChildCount);
  31.  
  32.     TCHAR* tChildCountStr = (TCHAR*)malloc(sizeof(TCHAR) * (lenght + 1));
  33.     sprintf(tChildCountStr, "%d", nChildCount);
  34.  
  35.     TCHAR* tEventName = (TCHAR*)malloc(sizeof(TCHAR) * (strlen(tEventNamePattern) + strlen(tChildCountStr) + 1));
  36.     strncpy(tEventName, tEventNamePattern, strlen(tEventNamePattern) + 1);
  37.     strncat(tEventName, tChildCountStr, strlen(tChildCountStr));
  38.  
  39.     return tEventName;
  40. }
  41.  
  42. int CreateNewProcess(stack** head, TCHAR* tCmdLine, TCHAR* tEventName)
  43. {
  44.     PROC_INFO* pr_in = (PROC_INFO*)malloc(sizeof(PROC_INFO));
  45.  
  46.     ZeroMemory(&pr_in->si, sizeof(STARTUPINFO));
  47.     ZeroMemory(&pr_in->pi, sizeof(PROCESS_INFORMATION));
  48.     pr_in->si.cb = sizeof(STARTUPINFO);
  49.     pr_in->hTerminateEvent = CreateNewEvent(tEventName);   
  50.  
  51.     if (!CreateProcess(NULL, tCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &pr_in->si, &pr_in->pi))
  52.     {
  53.         fprintf(stderr, "CreateProcess failed, error: %d\n", GetLastError());
  54.         free(pr_in);
  55.         return 0;
  56.     }
  57.  
  58.     push(head, pr_in);
  59.  
  60.     return 1;
  61. }
  62.  
  63. int KillProcess(stack** head)
  64. {
  65.     if (empty(*head))
  66.     {
  67.         return 0;
  68.     }
  69.  
  70.     PROC_INFO* pr_in = pop(head);
  71.  
  72.     if (pr_in)
  73.     {
  74.         SetEvent(pr_in->hTerminateEvent);
  75.  
  76.         DWORD dWaitResult = WaitForSingleObject(pr_in->pi.hProcess, INFINITE);
  77.         if (dWaitResult != WAIT_OBJECT_0)
  78.         {
  79.             fprintf(stderr, "Can't wait for a child process, error: %d\n", GetLastError());
  80.         }
  81.  
  82.         CloseHandle(pr_in->hTerminateEvent);
  83.         CloseHandle(pr_in->pi.hProcess);
  84.         CloseHandle(pr_in->pi.hThread);
  85.     }
  86.  
  87.     free(pr_in);
  88.  
  89.     return 1;
  90. }
  91.  
  92. int main()
  93. {
  94.     const TCHAR tPattern[] = TEXT("WaitForEvent.exe ");
  95.     const TCHAR tEventNamePattern[] = TEXT("Process");
  96.     TCHAR* tCmdLine = NULL;
  97.     TCHAR* tEventName = NULL;
  98.     stack* head = NULL;
  99.  
  100.     HANDLE hSemaphore = CreateSemaphore(NULL, 1, 1, "Semaphore");
  101.     if (!hSemaphore)
  102.     {
  103.         fprintf(stderr, "Can't create a semaphore, error: %d", GetLastError());
  104.         return 1;
  105.     }
  106.  
  107.     int nChildCount = 0;
  108.     char c;
  109.  
  110.     while ((c = _getch()) != 'q')
  111.     {
  112.         switch (c)
  113.         {
  114.         case '+':
  115.             tEventName = GenerateEventName(tEventNamePattern, nChildCount++);
  116.             tCmdLine = GenerateCmdArgs(tPattern, tEventName);
  117.             CreateNewProcess(&head, tCmdLine, tEventName);
  118.             free(tCmdLine);
  119.             free(tEventName);
  120.             break;
  121.         case '-':
  122.             KillProcess(&head);
  123.             nChildCount--;
  124.             break;
  125.         default:
  126.             break;
  127.         }
  128.     }
  129.  
  130.     while (!empty(head))
  131.     {
  132.         KillProcess(&head);
  133.     }
  134.  
  135.     CloseHandle(hSemaphore);
  136.  
  137.     system("pause");
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement