Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Stack.h"
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- HANDLE CreateNewEvent(LPCSTR lpName)
- {
- HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, lpName);
- if (hEvent == NULL)
- {
- fprintf(stderr, "Can't create an event, error: %d\n", GetLastError());
- return NULL;
- }
- return hEvent;
- }
- TCHAR* GenerateCmdArgs(const TCHAR* tPattern, const TCHAR* tEventName)
- {
- TCHAR* tCmdLine = (TCHAR*)malloc(sizeof(TCHAR) * (strlen(tEventName) + strlen(tPattern) + 1));
- strncpy(tCmdLine, tPattern, strlen(tPattern) + 1);
- strncat(tCmdLine, tEventName, strlen(tEventName));
- return tCmdLine;
- }
- TCHAR* GenerateEventName(const TCHAR* tEventNamePattern, int nChildCount)
- {
- int lenght = snprintf(NULL, 0, "%d", nChildCount);
- TCHAR* tChildCountStr = (TCHAR*)malloc(sizeof(TCHAR) * (lenght + 1));
- sprintf(tChildCountStr, "%d", nChildCount);
- TCHAR* tEventName = (TCHAR*)malloc(sizeof(TCHAR) * (strlen(tEventNamePattern) + strlen(tChildCountStr) + 1));
- strncpy(tEventName, tEventNamePattern, strlen(tEventNamePattern) + 1);
- strncat(tEventName, tChildCountStr, strlen(tChildCountStr));
- return tEventName;
- }
- int CreateNewProcess(stack** head, TCHAR* tCmdLine, TCHAR* tEventName)
- {
- PROC_INFO* pr_in = (PROC_INFO*)malloc(sizeof(PROC_INFO));
- ZeroMemory(&pr_in->si, sizeof(STARTUPINFO));
- ZeroMemory(&pr_in->pi, sizeof(PROCESS_INFORMATION));
- pr_in->si.cb = sizeof(STARTUPINFO);
- pr_in->hTerminateEvent = CreateNewEvent(tEventName);
- if (!CreateProcess(NULL, tCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &pr_in->si, &pr_in->pi))
- {
- fprintf(stderr, "CreateProcess failed, error: %d\n", GetLastError());
- free(pr_in);
- return 0;
- }
- push(head, pr_in);
- return 1;
- }
- int KillProcess(stack** head)
- {
- if (empty(*head))
- {
- return 0;
- }
- PROC_INFO* pr_in = pop(head);
- if (pr_in)
- {
- SetEvent(pr_in->hTerminateEvent);
- DWORD dWaitResult = WaitForSingleObject(pr_in->pi.hProcess, INFINITE);
- if (dWaitResult != WAIT_OBJECT_0)
- {
- fprintf(stderr, "Can't wait for a child process, error: %d\n", GetLastError());
- }
- CloseHandle(pr_in->hTerminateEvent);
- CloseHandle(pr_in->pi.hProcess);
- CloseHandle(pr_in->pi.hThread);
- }
- free(pr_in);
- return 1;
- }
- int main()
- {
- const TCHAR tPattern[] = TEXT("WaitForEvent.exe ");
- const TCHAR tEventNamePattern[] = TEXT("Process");
- TCHAR* tCmdLine = NULL;
- TCHAR* tEventName = NULL;
- stack* head = NULL;
- HANDLE hSemaphore = CreateSemaphore(NULL, 1, 1, "Semaphore");
- if (!hSemaphore)
- {
- fprintf(stderr, "Can't create a semaphore, error: %d", GetLastError());
- return 1;
- }
- int nChildCount = 0;
- char c;
- while ((c = _getch()) != 'q')
- {
- switch (c)
- {
- case '+':
- tEventName = GenerateEventName(tEventNamePattern, nChildCount++);
- tCmdLine = GenerateCmdArgs(tPattern, tEventName);
- CreateNewProcess(&head, tCmdLine, tEventName);
- free(tCmdLine);
- free(tEventName);
- break;
- case '-':
- KillProcess(&head);
- nChildCount--;
- break;
- default:
- break;
- }
- }
- while (!empty(head))
- {
- KillProcess(&head);
- }
- CloseHandle(hSemaphore);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement