Advertisement
rnort

SPO-2-WIN

Oct 25th, 2012
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. // SPO-2.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include <process.h>
  7. #include <locale.h>
  8. #define MAX_COUNT 12
  9. #define BUFFER_SIZE 10
  10.  
  11. PROCESS_INFORMATION CreateNewProcess(TCHAR* AppName, TCHAR *cmd);
  12. void Parent(TCHAR *AppName);                                                   
  13. void Child(int);                                               
  14. void TryPrint(HANDLE hProcCanWrite, char* str);    
  15.  
  16. int _tmain(int argc, TCHAR* argv[])
  17. {
  18.     setlocale(LC_ALL, ".1251");
  19.     if(argc == 2){
  20.         Child( _ttoi(argv[1]) );
  21.     }  
  22.     else{
  23.         Parent(argv[0]);                                   
  24.     }
  25.     return 0;
  26. }
  27.  
  28. void Parent(TCHAR *AppName)
  29. {
  30.     HANDLE hProcCanWrite = CreateEvent(NULL, FALSE, TRUE, TEXT("ProcCanWrite"));   
  31.     char input;
  32.     int currentNumberOfProcesses = 0;
  33.     PROCESS_INFORMATION processesArray[MAX_COUNT];
  34.  
  35.     while( input = _getch() )
  36.     {
  37.         if(input == '-' && currentNumberOfProcesses) {
  38.  
  39.             TCHAR buffer[BUFFER_SIZE] = {0};
  40.             _itot_s(currentNumberOfProcesses-1, buffer, 10);
  41.             HANDLE hTerminateSignal = OpenEvent( EVENT_ALL_ACCESS, FALSE,  buffer);
  42.             SetEvent(hTerminateSignal);
  43.             if (WaitForSingleObject(processesArray[currentNumberOfProcesses-1].hProcess, INFINITE) ==  WAIT_OBJECT_0 ){
  44.                 CloseHandle(processesArray[currentNumberOfProcesses-1].hProcess);                          
  45.                 CloseHandle(processesArray[currentNumberOfProcesses-1].hThread);
  46.                 currentNumberOfProcesses--;
  47.                 CloseHandle(hTerminateSignal);
  48.             }
  49.             continue;
  50.         }
  51.         if((input == '+') && (currentNumberOfProcesses < MAX_COUNT ))
  52.         {
  53.             TCHAR buffer[BUFFER_SIZE] = {0};
  54.             _itot_s(currentNumberOfProcesses, buffer, 10);
  55.             processesArray[currentNumberOfProcesses] = CreateNewProcess( AppName, buffer); 
  56.             HANDLE hTerminateSignal = CreateEvent( NULL, FALSE, FALSE, buffer);
  57.             currentNumberOfProcesses++;
  58.             continue;
  59.         }
  60.         if(input == 'q'){
  61.             break;
  62.         }
  63.     }
  64.     for(int i = 0; i < currentNumberOfProcesses; i++)
  65.     {
  66.         TerminateProcess(processesArray[i].hProcess, 0);
  67.         CloseHandle(processesArray[i].hProcess);
  68.         CloseHandle(processesArray[i].hThread);
  69.     }
  70. }
  71.  
  72. TCHAR* createArgsString(TCHAR* app, TCHAR* arg)
  73. {
  74.     TCHAR args[100] = {0};
  75.     _tcscat_s( args, app);
  76.     _tcscat_s( args, TEXT(" "));
  77.     _tcscat_s( args, arg);
  78.     return args;
  79. }
  80.  
  81. PROCESS_INFORMATION CreateNewProcess(TCHAR* AppName, TCHAR *cmd)
  82. {
  83.     STARTUPINFO cif;
  84.     ZeroMemory(&cif,sizeof(STARTUPINFO));      
  85.     PROCESS_INFORMATION pi;
  86.  
  87.     if(!CreateProcess(
  88.         NULL,
  89.         createArgsString(AppName, cmd),
  90.         NULL,
  91.         NULL,
  92.         TRUE,
  93.         NULL,
  94.         NULL,
  95.         NULL,
  96.         &cif,
  97.         &pi )
  98.         )
  99.     {
  100.         puts("Create process failed!");
  101.     }
  102.     return pi;
  103. }
  104.  
  105. char* getTextString(int number){
  106.  
  107.     static char outputStringArray[MAX_COUNT][150] = {
  108.         "Nirvana ", // was an American rock band that was formed by singer/guitarist Kurt Cobain and bassist Krist Novoselic ",
  109.         "Queen ", //recorded the album A Kind of Magic, containing several reworkings of songs written for the fantasy action film Highlander. ",
  110.         "Scorpions ", //are a rock band from Hannover, Germany formed in 1965 by guitarist Rudolf Schenker. ",
  111.         "Metallica ", //has released nine studio albums, three live albums, five extended plays, 24 music videos, and 45 singles. ",
  112.         "Bon Jovi ", //is an American rock band from Sayreville, New Jersey. ",
  113.         "The Final Countdown ",// is the third studio album by the Swedish hard rock band Europe. Released on 26 May 1986. ",
  114.         "Rammstein ", //was founded  by guitarist Richard Z. Kruspe. In 1989, he escaped to West Berlin and started the band Orgasm Death Gimmicks. ",
  115.         "Louna ", //is a Russian Alternative/Punk rock band, formed in Moscow in 2008 by Tracktor Bowling's musicians Lusine Gevorkyan and Vitaly Demidenko ",
  116.         "Sonic Syndicate ",// is a heavy metal band from Falkenberg, Sweden. ",
  117.         "Skillet ",//is an American Christian rock band formed in Memphis, Tennessee in 1996. ",
  118.         "Надежда Никитична Кадышева ",//(род. 1 июня 1959, Горки, Лениногорский район, Татарская АССР) — российская певица эрзянского происхождения. ", // easter egg %)
  119.         "The Beatles "//are unquestionably the best and most important band in rock history, as well as the most compelling story. "
  120.     };
  121.     return outputStringArray[number];
  122. }
  123.  
  124. void Child( int number )
  125. {  
  126.     TCHAR buffer[BUFFER_SIZE] = {0};
  127.     _itot_s(number, buffer,10);
  128.     HANDLE hProcCanWrite = OpenEvent(EVENT_ALL_ACCESS, FALSE, TEXT("ProcCanWrite") );
  129.     HANDLE hTerminateSignal = OpenEvent( EVENT_ALL_ACCESS, FALSE, buffer);
  130.  
  131.     while( WaitForSingleObject( hTerminateSignal, 10) != WAIT_OBJECT_0 )
  132.     {  
  133.         TryPrint(hProcCanWrite, getTextString(number));
  134.     }
  135.     CloseHandle(hProcCanWrite);
  136.     CloseHandle(hTerminateSignal);
  137. }
  138.  
  139. void TryPrint(HANDLE hProcCanWrite,  char* str)
  140. {
  141.     if(WaitForSingleObject( hProcCanWrite, 10) == WAIT_OBJECT_0)
  142.     {
  143.         ResetEvent(hProcCanWrite);                         
  144.         for ( int i = 0; i < strlen(str); ++i)
  145.         {
  146.             printf_s("%c", str[i] );
  147.             Sleep(50);
  148.         }
  149.         SetEvent(hProcCanWrite);                           
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement