Advertisement
Guest User

Billy ONeal

a guest
Nov 30th, 2008
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.95 KB | None | 0 0
  1. /*Copyright (c) 2008 Billy O'Neal
  2.  *
  3.  *Permission is hereby granted, free of charge, to any person obtaining a copy
  4.  *of this software and associated documentation files (the "Software"), to deal
  5.  *in the Software without restriction, including without limitation the rights
  6.  *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.  *copies of the Software, and to permit persons to whom the Software is
  8.  *furnished to do so, subject to the following conditions:
  9.  *
  10.  *The above copyright notice and this permission notice shall be included in
  11.  *all copies or substantial portions of the Software.
  12.  *
  13.  *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.  *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.  *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.  *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.  *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18.  *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19.  *THE SOFTWARE.
  20.  *
  21.  * ConsoleTools.cpp -- A bunch of console management functions.
  22.  */
  23. #include "stdafx.h"
  24. using namespace std;
  25. consoleToolsProvider::consoleToolsProvider()
  26. {
  27.     initialize();
  28. }
  29. consoleToolsProvider::~consoleToolsProvider()
  30. {
  31.     std::map<HANDLE,control>::iterator it;
  32.     it = animationThreads.begin();
  33.     for(register unsigned int idx = 0; idx < animationThreads.size(); idx++) {
  34.         TerminateThread((*it).first,0);
  35.         CloseHandle((*it).first);
  36.         it++;
  37.     }
  38.     animationThreads.clear();
  39. }
  40. void consoleToolsProvider::initialize()
  41. {
  42.     //Zero out origin
  43.     origin.X = 0;
  44.     origin.Y = 0;
  45.     //Get the current console
  46.     current_Console = GetStdHandle(STD_OUTPUT_HANDLE);
  47.     //Get attributes of the current console
  48.     GetConsoleScreenBufferInfo(current_Console,&current_Screen);
  49.     //Reset threading
  50.     animationLocked = false;
  51.     handleValid = false;
  52. }
  53. void consoleToolsProvider::color(char background, char foreground)
  54. {
  55.     WORD attributes;
  56.     //Set the foreground attribute
  57.     switch (foreground) {
  58.         case '0':
  59.             //Black = 0
  60.             attributes = NULL;
  61.             break;
  62.         case '1':
  63.             //Blue = 1
  64.             attributes = FOREGROUND_BLUE;
  65.             break;
  66.         case '2':
  67.             //Green = 2
  68.             attributes = FOREGROUND_GREEN;
  69.             break;
  70.         case '3':
  71.             //Aqua = 3
  72.             attributes = FOREGROUND_BLUE | FOREGROUND_GREEN;
  73.             break;
  74.         case '4':
  75.             //Red = 4
  76.             attributes = FOREGROUND_RED;
  77.             break;
  78.         case '5':
  79.             //Purple = 5
  80.             attributes = FOREGROUND_BLUE | FOREGROUND_RED;
  81.             break;
  82.         case '6':
  83.             //Yellow = 6
  84.             attributes = FOREGROUND_RED | FOREGROUND_GREEN;
  85.             break;
  86.         case '7':
  87.             //White = 7
  88.             attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
  89.             break;
  90.         case '8':
  91.             //Gray = 8
  92.             attributes = FOREGROUND_INTENSITY;
  93.             break;
  94.         case '9':
  95.             //Light Blue = 9
  96.             attributes = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
  97.             break;
  98.         case 'A':
  99.             //Light Green = A
  100.             attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  101.             break;
  102.         case 'B':
  103.             //Light Aqua = B
  104.             attributes = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  105.             break;
  106.         case 'C':
  107.             //Light Red = C
  108.             attributes = FOREGROUND_RED | FOREGROUND_INTENSITY;
  109.             break;
  110.         case 'D':
  111.             //Light Purple = D
  112.             attributes = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY;
  113.             break;
  114.         case 'E':
  115.             //Light Yellow = E
  116.             attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  117.             break;
  118.         case 'F':
  119.             //Bright White = F
  120.             attributes = FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  121.             break;
  122.     }
  123.     //Set the background attribute
  124.     switch (background) {
  125.             //Black = 0
  126.         case '1':
  127.             //Blue = 1
  128.             attributes = attributes | BACKGROUND_BLUE;
  129.             break;
  130.         case '2':
  131.             //Green = 2
  132.             attributes = attributes | FOREGROUND_GREEN;
  133.             break;
  134.         case '3':
  135.             //Aqua = 3
  136.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_GREEN;
  137.             break;
  138.         case '4':
  139.             //Red = 4
  140.             attributes = attributes | BACKGROUND_RED;
  141.             break;
  142.         case '5':
  143.             //Purple = 5
  144.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_RED;
  145.             break;
  146.         case '6':
  147.             //Yellow = 6
  148.             attributes = attributes | BACKGROUND_RED | BACKGROUND_GREEN;
  149.             break;
  150.         case '7':
  151.             //White = 7
  152.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
  153.             break;
  154.         case '8':
  155.             //Gray = 8
  156.             attributes = attributes | BACKGROUND_INTENSITY;
  157.             break;
  158.         case '9':
  159.             //Light Blue = 9
  160.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
  161.             break;
  162.         case 'A':
  163.             //Light Green = A
  164.             attributes = attributes | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
  165.             break;
  166.         case 'B':
  167.             //Light Aqua = B
  168.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
  169.             break;
  170.         case 'C':
  171.             //Light Red = C
  172.             attributes = attributes | BACKGROUND_RED | BACKGROUND_INTENSITY;
  173.             break;
  174.         case 'D':
  175.             //Light Purple = D
  176.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY;
  177.             break;
  178.         case 'E':
  179.             //Light Yellow = E
  180.             attributes = attributes | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
  181.             break;
  182.         case 'F':
  183.             //Bright White = F
  184.             attributes = attributes | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
  185.             break;
  186.     }
  187.     SetConsoleTextAttribute(current_Console,attributes);
  188.     FillConsoleOutputAttribute(current_Console,attributes,current_Screen.dwSize.X * current_Screen.dwSize.Y,origin,NULL);
  189. }
  190. void consoleToolsProvider::clear()
  191. {
  192.     //Clear output buffer
  193.     FillConsoleOutputCharacter(current_Console,_T(' '),current_Screen.dwSize.X * current_Screen.dwSize.Y,origin,NULL);
  194.     SetConsoleCursorPosition(current_Console,origin);
  195. }
  196. void consoleToolsProvider::pause()
  197. {
  198.     _getch();
  199. }
  200. void consoleToolsProvider::title(LPCTSTR title)
  201. {
  202.     SetConsoleTitle(title);
  203. }
  204. void consoleToolsProvider::drawProgressBar(unsigned short topX, unsigned short topY, unsigned short bottomX, unsigned short bottomY, unsigned short percent, TCHAR usedChar, TCHAR unusedChar, TCHAR leftBoundChar, TCHAR rightBoundChar)
  205. {
  206.     //reinitialize the current console and screen
  207.     initialize();
  208.     //define the rectangle we will use to draw
  209.     SMALL_RECT drawRect;
  210.     drawRect.Left = topX;
  211.     drawRect.Top = topY;
  212.     drawRect.Right = bottomX;
  213.     drawRect.Bottom = bottomY;
  214.     //Build the array of characters to actually write
  215.     CHAR_INFO *drawnCharacters;
  216.     unsigned int width, height;
  217.     //Find the width and height of the area we are going to draw
  218.     width = bottomX - topX;
  219.     height = bottomY - topY + 1;
  220.     //Find the number of characters on each line which will be filled with each type of character.
  221.     unsigned short int usedLen = (percent*(width-2)/100);
  222.     unsigned short int unusedLen = (width-2) - usedLen;
  223.     //do the actual allocation
  224.     drawnCharacters = new CHAR_INFO[height*width+1];
  225.     //Apply the current console attributes to each CHAR_INFO struture
  226.     WORD attrs = current_Screen.wAttributes;
  227.     for(register unsigned int idx = 0; idx <= width*height;idx++) {
  228.         drawnCharacters[idx].Attributes = attrs;
  229.     }
  230.     //Loop Per Line
  231.     for(register unsigned int currentLine = 0; currentLine < height; currentLine++) {
  232.         #ifdef _UNICODE
  233.         //Draw the bounds characters
  234.         drawnCharacters[currentLine*width].Char.UnicodeChar = leftBoundChar;
  235.         drawnCharacters[(currentLine+1)*width-1].Char.UnicodeChar = rightBoundChar;
  236.         //Draw the used characters
  237.         for(register unsigned int curChar = 1; curChar <= usedLen; curChar++) {
  238.             drawnCharacters[currentLine*width+curChar].Char.UnicodeChar = usedChar;
  239.         }
  240.         //Draw the unused characters
  241.         for(register unsigned int curChar = usedLen+1; curChar < width-1; curChar++) {
  242.             drawnCharacters[currentLine*width+curChar].Char.UnicodeChar = unusedChar;
  243.         }
  244.         #else
  245.         //Draw the bounds characters
  246.         drawnCharacters[currentLine*width].Char.AsciiChar = leftBoundChar;
  247.         drawnCharacters[(currentLine+1)*width-1].Char.AsciiChar = rightBoundChar;
  248.         //Draw the used characters
  249.         for(register unsigned int curChar = 1; curChar <= usedLen; curChar++) {
  250.             drawnCharacters[currentLine*width+curChar].Char.AsciiChar = usedChar;
  251.         }
  252.         //Draw the unused characters
  253.         for(register unsigned int curChar = usedLen+1; curChar < width-1; curChar++) {
  254.             drawnCharacters[currentLine*width+curChar].Char.AsciiChar = unusedChar;
  255.         }
  256.         #endif
  257.     }
  258.     //Do the actual write
  259.     //Create arrayBounds coordinate for API
  260.     COORD arrayBounds;
  261.     arrayBounds.X = width;
  262.     arrayBounds.Y = height;
  263.     WriteConsoleOutput(current_Console,drawnCharacters,arrayBounds,origin,&drawRect);
  264.     delete [] drawnCharacters;
  265. }
  266. HANDLE consoleToolsProvider::drawActivityIndicator(enum activityIndicatorMode mode, unsigned short int topX, unsigned short int topY, unsigned short int bottomX, unsigned short int bottomY, DWORD sleepInterval, TCHAR usedChar, TCHAR unusedChar, TCHAR leftBoundChar, TCHAR rightBoundChar)
  267. {
  268.     HANDLE tempHandle;
  269.     LPTHREAD_START_ROUTINE targetFunction = &animate;
  270.     //Build a structure of arguments to pass to the animation function
  271.     animationArgs *valuesStruct;
  272.     valuesStruct = new animationArgs;
  273.     valuesStruct->caller = this;
  274.     valuesStruct->mode = mode;
  275.     valuesStruct->topX = topX;
  276.     valuesStruct->topY = topY;
  277.     valuesStruct->bottomX = bottomX;
  278.     valuesStruct->bottomY = bottomY;
  279.     valuesStruct->sleepInterval = sleepInterval;
  280.     valuesStruct->usedChar = usedChar;
  281.     valuesStruct->unusedChar = unusedChar;
  282.     valuesStruct->leftBoundChar = leftBoundChar;
  283.     valuesStruct->rightBoundChar = rightBoundChar;
  284.     while (animationLocked) {Sleep(1);}
  285.     animationLocked = true;
  286.     tempHandle = CreateThread(NULL,NULL,targetFunction,valuesStruct,NULL,NULL);
  287.     animationThreads.insert(std::pair<HANDLE,control>(tempHandle,NORMAL));
  288.     lastThread = tempHandle;
  289.     handleValid = true;
  290.     while (animationLocked) {Sleep(1);}; // Wait until thread starts
  291.     return tempHandle;
  292. }
  293. void consoleToolsProvider::stopActivityIndicator(HANDLE indicatorHandle, control action)
  294. {
  295.     while (animationLocked) {
  296.         Sleep(10);
  297.     }
  298.     animationLocked = true;
  299.     animationThreads[indicatorHandle] = action;
  300.     animationLocked = false;
  301. }
  302. DWORD WINAPI animate(LPVOID params)
  303. {
  304.     while(!((animationArgs *) params)->caller->handleValid) {Sleep(1);}; //Wait until handle is valid
  305.     HANDLE currentThread = ((animationArgs *) params)->caller->lastThread; //get copy of handle
  306.     ((animationArgs *) params)->caller->handleValid = false; //We've got the handle, mark as invalid
  307.     //Copy the content of the structure we were sent into our temporary thread, at the same time casting and freeing the ram used.
  308.     animationArgs arguments;
  309.     memcpy(&arguments,params,sizeof(animationArgs));
  310.     //Free the temp structure we were called with
  311.     delete params;
  312.     arguments.caller->animationLocked = false; //Release lock on the map
  313.     //define the rectangle we will use to draw
  314.     SMALL_RECT drawRect;
  315.     drawRect.Left = arguments.topX;
  316.     drawRect.Top = arguments.topY;
  317.     drawRect.Right = arguments.bottomX;
  318.     drawRect.Bottom = arguments.bottomY;
  319.     //Build the array of characters to actually write
  320.     CHAR_INFO *drawnCharacters;
  321.     unsigned int width, height;
  322.     //Find the width and height of the area we are going to draw
  323.     width = arguments.bottomX - arguments.topX;
  324.     height = arguments.bottomY - arguments.topY;
  325.     COORD arrayBounds;
  326.     arrayBounds.X = width;
  327.     arrayBounds.Y = height;
  328.     //do the actual allocation
  329.     drawnCharacters = new CHAR_INFO[height*width+1];
  330.     //variable for use in loop later defining current state
  331.     unsigned int state = 1;
  332.     unsigned short int writeIndex;
  333.     //Apply the current console attributes to each CHAR_INFO struture
  334.     WORD attrs = arguments.caller->current_Screen.wAttributes;
  335.     for(register unsigned int idx = 0; idx <= width*height;idx++) {
  336.         drawnCharacters[idx].Attributes = attrs;
  337.     }
  338.     //Infinite loop which will be terminated with threading message.
  339.     for(;;) {
  340.         //Zero out the current state of the buffer
  341.         for(register unsigned int idx = 0; idx <= width*height;idx++) {
  342.             #ifdef _UNICODE
  343.                 drawnCharacters[idx].Char.UnicodeChar = arguments.unusedChar;
  344.             #else
  345.                 drawnCharacters[idx].Char.AsciiChar = arguments.unusedChar;
  346.             #endif
  347.         }
  348.         switch(arguments.mode) {
  349.             case OSCILIATE:
  350.                 //If we've looped the whole way, then reset state
  351.                 if (state >= floor((float)width*2)-4) state = 1;
  352.                 writeIndex = state;
  353.                 if (writeIndex >= width - 1) {
  354.                     writeIndex = width - 2 - (writeIndex-width);
  355.                 }
  356.                 if (writeIndex == (width-2)) state++;
  357.                 for(unsigned short int currentLine = 0; currentLine < height; currentLine++) {
  358.                     #ifdef _UNICODE
  359.                     //Draw caps
  360.                     drawnCharacters[currentLine*width].Char.UnicodeChar = arguments.leftBoundChar;
  361.                     drawnCharacters[(currentLine+1)*width-1].Char.UnicodeChar = arguments.rightBoundChar;
  362.                     drawnCharacters[currentLine*width+writeIndex].Char.UnicodeChar = arguments.usedChar;
  363.                     #else
  364.                     drawnCharacters[currentLine*width].Char.AsciiChar = arguments.leftBoundChar;
  365.                     drawnCharacters[(currentLine+1)*width-1].Char.AsciiChar = arguments.rightBoundChar;
  366.                     drawnCharacters[currentLine*width+writeIndex].Char.AsciiChar = arguments.usedChar;
  367.                     #endif
  368.                 }
  369.                 break;
  370.             case CHASE:
  371.             default:
  372.                 //If we've looped the whole way, then reset state
  373.                 if (state == 2) state = 0;
  374.                 for(register unsigned short int idx = 0; idx < height; idx++) {
  375.                     #ifdef _UNICODE
  376.                     //Draw caps
  377.                     drawnCharacters[idx*width].Char.UnicodeChar = arguments.leftBoundChar;
  378.                     drawnCharacters[(idx+1)*width-1].Char.UnicodeChar = arguments.rightBoundChar;
  379.                     for(register unsigned short int charPos = idx*width+1; charPos < ((idx+1)*width)-2;charPos += 2) {
  380.                         if (state) {
  381.                         drawnCharacters[charPos].Char.UnicodeChar = arguments.usedChar;
  382.                         } else {
  383.                         drawnCharacters[charPos+1].Char.UnicodeChar = arguments.usedChar;
  384.                         }
  385.                     }
  386.                     #else
  387.                     //Draw caps
  388.                     drawnCharacters[idx*width].Char.AsciiChar = arguments.leftBoundChar;
  389.                     drawnCharacters[(idx+1)*width-1].Char.AsciiChar = arguments.rightBoundChar;
  390.                     for(register unsigned short int charPos = idx*width+1; charPos < ((idx+1)*width)-2;charPos += 2) {
  391.                         if (state) {
  392.                         drawnCharacters[charPos].Char.AsciiChar = arguments.usedChar;
  393.                         } else {
  394.                         drawnCharacters[charPos+1].Char.AsciiChar = arguments.usedChar;
  395.                         }
  396.                     }
  397.                     #endif
  398.                 }
  399.                 break;
  400.         }
  401.         state++;
  402.         //Draw the buffer we've created
  403.         WriteConsoleOutput(arguments.caller->current_Console,drawnCharacters,arrayBounds,arguments.caller->origin,&drawRect);
  404.         Sleep(arguments.sleepInterval);
  405.         while (arguments.caller->animationLocked == true) {Sleep(1);}; //Wait until we can lock it
  406.         arguments.caller->animationLocked = true;
  407.         if (arguments.caller->animationThreads[currentThread] == TERMINATE || arguments.caller->animationThreads[currentThread] == CLEAR) {
  408.             break;
  409.         }
  410.         arguments.caller->animationLocked = false;
  411.     }
  412.     if (arguments.caller->animationThreads[currentThread] == CLEAR) {
  413.         for(register unsigned int idx = 0; idx <= width*height;idx++) {
  414.                 #ifdef _UNICODE
  415.                     drawnCharacters[idx].Char.UnicodeChar = arguments.unusedChar;
  416.                 #else
  417.                     drawnCharacters[idx].Char.AsciiChar = arguments.unusedChar;
  418.                 #endif
  419.             }
  420.         WriteConsoleOutput(arguments.caller->current_Console,drawnCharacters,arrayBounds,arguments.caller->origin,&drawRect);
  421.     }
  422.     arguments.caller->animationLocked = false;
  423.     delete [] drawnCharacters;
  424.     return 0;
  425. }
  426. LPTSTR consoleToolsProvider::getTextOfCommand(const LPTSTR command)
  427. {
  428.     DWORD nothing;
  429.     HANDLE hBatchFile, hTempFile;
  430.     LPTSTR result;
  431.     //Initialize random number generator
  432.     static unsigned int randSeed = 0;
  433.     if (randSeed == 0) {
  434.         randSeed = (unsigned int) time(NULL);
  435.     }
  436.     std::srand(randSeed);
  437.     //
  438.     //Generate a random batch file name
  439.     //
  440.     DWORD tempLen;
  441.     tempLen = GetEnvironmentVariable(_T("temp"),NULL,NULL);
  442.     LPTSTR batchFileName;
  443.     batchFileName = new TCHAR[tempLen+14];
  444.     GetEnvironmentVariable(_T("temp"),batchFileName,tempLen+14);
  445.     _tcscat(batchFileName,_T("\\batch"));
  446.     TCHAR * finger = batchFileName + _tcslen(batchFileName);
  447.     for(register unsigned short int idx = 0; idx <= 3; idx++) {
  448.         *finger = 48 + rand() % 10;
  449.         finger++;
  450.     }
  451.     *finger = _T('\0');
  452.     _tcscat(batchFileName,_T(".bat"));
  453.     LPTSTR tempFileName = new TCHAR[tempLen+13];
  454.     GetEnvironmentVariable(_T("temp"),tempFileName,tempLen+13);
  455.     _tcscat(tempFileName,_T("\\text"));
  456.     finger = tempFileName + _tcslen(tempFileName);
  457.     for(register unsigned short int idx = 0; idx <= 3; idx++) {
  458.         *finger = 48 + rand() % 10;
  459.         finger++;
  460.     }
  461.     *finger = _T('\0');
  462.     _tcscat(tempFileName,_T(".txt"));
  463.     hBatchFile = CreateFile(batchFileName,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL);
  464.     if (hBatchFile == INVALID_HANDLE_VALUE) {
  465.         throw GetLastError();
  466.     }
  467.     WriteFile(hBatchFile,"@echo off\n",strlen("@echo off\n"),&nothing,NULL);
  468. #ifndef _UNICODE
  469.     if (!WriteFile(hBatchFile,command,_tcslen(command)*sizeof(TCHAR),&nothing,NULL)) {
  470.         throw GetLastError();
  471.     }
  472. #else
  473.     LPSTR commandTemp;
  474.     char toast = '?';
  475.     BOOL toast2 = false;
  476.     commandTemp = new char[_tcslen(command)+1];
  477.     WideCharToMultiByte(CP_ACP,WC_NO_BEST_FIT_CHARS|WC_COMPOSITECHECK,command,-1,commandTemp,_tcslen(command)+1,&toast,&toast2);
  478.     if (!WriteFile(hBatchFile,commandTemp,_tcslen(command),&nothing,NULL)) {
  479.         throw GetLastError();
  480.     }
  481.     delete [] commandTemp;
  482. #endif
  483.     CloseHandle(hBatchFile);
  484.     DWORD processResult;
  485.     STARTUPINFO si = { sizeof(STARTUPINFO) };
  486.     si.dwFlags = STARTF_USESHOWWINDOW;
  487.     si.wShowWindow = SW_HIDE;
  488.     PROCESS_INFORMATION pi;
  489.     LPTSTR PROCcommmand;
  490.     PROCcommmand = new TCHAR[_tcslen(batchFileName)+_tcslen(tempFileName) + 15];
  491.     _tcscpy(PROCcommmand,_T("\""));
  492.     _tcscat(PROCcommmand,batchFileName);
  493.     _tcscat(PROCcommmand,_T("\" > \""));
  494.     _tcscat(PROCcommmand,tempFileName);
  495.     _tcscat(PROCcommmand,_T("\""));
  496.     //Turn off stderror to show nothing
  497.     HANDLE stdOld;
  498.     stdOld = GetStdHandle(STD_ERROR_HANDLE);
  499.     HANDLE hNullFile = CreateFile(_T("nul"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  500.     SetStdHandle(STD_ERROR_HANDLE,hNullFile);
  501.     //CREATE_NO_WINDOW
  502.     CreateProcess(NULL,PROCcommmand,NULL,NULL,false,NULL,NULL,NULL,&si,&pi);
  503.     do
  504.     {
  505.         GetExitCodeProcess(pi.hProcess,&processResult);
  506.         Sleep(50);
  507.     } while (processResult == STILL_ACTIVE);
  508.     CloseHandle(pi.hThread);
  509.     CloseHandle(pi.hProcess);
  510.     delete [] PROCcommmand;
  511.     //Turn stderror back on
  512.     SetStdHandle(STD_ERROR_HANDLE,stdOld);
  513.     CloseHandle(hNullFile);
  514.     //Wait for the process to write out stuff to disk
  515.     Sleep(100);
  516.     LARGE_INTEGER allocateSize;
  517.     hTempFile = CreateFile(tempFileName,GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  518.     if (hTempFile == INVALID_HANDLE_VALUE) {
  519.         throw GetLastError();
  520.     }
  521.     GetFileSizeEx(hTempFile,&allocateSize);
  522.     result = (TCHAR *) new char[allocateSize.LowPart+1];
  523.     ReadFile(hTempFile,(LPVOID)result,allocateSize.LowPart,&nothing,NULL);
  524.     *((char*)result + allocateSize.LowPart) = '\0';
  525.     CloseHandle(hTempFile);
  526.     DeleteFile(batchFileName);
  527.     DeleteFile(tempFileName);
  528.     delete [] tempFileName;
  529.     delete [] batchFileName;
  530. #ifdef _UNICODE
  531.     LPTSTR resultConverted;
  532.     resultConverted = new TCHAR[allocateSize.LowPart+1];
  533.     MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,(LPCSTR)result,-1,resultConverted,allocateSize.LowPart+1);
  534.     delete [] result;
  535.     result = resultConverted;
  536. #endif
  537.     return result;
  538. }
  539. void consoleToolsProvider::Error(DWORD code)
  540. {
  541.     //Translate the DWORD into a usable error message.
  542.     LPTSTR lpBuffer;
  543.     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,code,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)&lpBuffer,0,NULL);
  544.     //Write the message
  545.     WriteConsole(current_Console,lpBuffer,_tcslen(lpBuffer),NULL,NULL);
  546.     //Free ram allocated by FormatMessage
  547.     LocalFree(&lpBuffer);
  548. }
  549. void consoleToolsProvider::Write(const LPTSTR toWrite)
  550. {
  551.     LPSTR converted;
  552.     CHAR toast = '?';
  553. #ifdef _UNICODE
  554.     BOOL toastBool;
  555.     converted = new CHAR[_tcslen(toWrite)+1];
  556.     WideCharToMultiByte(CP_ACP,WC_NO_BEST_FIT_CHARS|WC_COMPOSITECHECK,toWrite,-1,converted,_tcslen(toWrite)+1,&toast,&toastBool);
  557. #else
  558.     converted = toWrite;
  559. #endif
  560.     std::cout << converted;
  561. #ifdef _UNICODE
  562.     delete [] converted;
  563. #endif
  564. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement