orwelldevcpp

Untitled

Apr 20th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. // Execute & Pause
  2. // Runs a program, then keeps the console window open after it finishes
  3.  
  4. #include <string>
  5. using std::string;
  6. #include <stdio.h>
  7. #include <windows.h>
  8.  
  9. #define MAX_COMMAND_LENGTH 32768
  10. #define MAX_ERROR_LENGTH 2048
  11.  
  12. void Exit(int exitcode) {
  13.     system("pause");
  14.     exit(exitcode);
  15. }
  16.  
  17. string GetErrorMessage() {
  18.     string result(MAX_ERROR_LENGTH,0);
  19.    
  20.     FormatMessage(
  21.         FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  22.         NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),&result[0],result.size(),NULL);
  23.    
  24.     // Clear newlines at end of string
  25.     for(int i = result.length()-1;i >= 0;i--) {
  26.         if(isspace(result[i])) {
  27.             result[i] = 0;
  28.         } else {
  29.             break;
  30.         }
  31.     }
  32.     return result;
  33. }
  34.  
  35. string GetCommand(int argc,char** argv) {
  36.     string result;
  37.     for(int i = 1;i < argc;i++) {
  38.         // Quote the first argument in case the path name contains spaces
  39. //      if(i == 1) {
  40. //          result += string("\"") + string(argv[i]) + string("\"");
  41. //      } else {
  42.             result += string(argv[i]);
  43. //      }
  44.        
  45.         // Add a space except for the last argument
  46.         if(i != (argc-1)) {
  47.             result += string(" ");
  48.         }
  49.     }
  50.    
  51.     if(result.length() > MAX_COMMAND_LENGTH) {
  52.         printf("\n--------------------------------");
  53.         printf("\nError: Length of command line string is over %d characters\n",MAX_COMMAND_LENGTH);
  54.         Exit(EXIT_FAILURE);
  55.     }
  56.    
  57.     return result;
  58. }
  59.  
  60. DWORD ExecuteCommand(string& command) {
  61.     STARTUPINFO si;
  62.     PROCESS_INFORMATION pi;
  63.     memset(&si,0,sizeof(si));
  64.     si.cb = sizeof(si);
  65.     memset(&pi,0,sizeof(pi));
  66.    
  67.     if(!CreateProcess(NULL, (LPSTR)command.c_str(), NULL, NULL, false, 0, NULL, NULL, &si, &pi)) {
  68.         printf("\n--------------------------------");
  69.         printf("\nFailed to execute \"%s\":",command.c_str());
  70.         printf("\nError %lu: %s\n",GetLastError(),GetErrorMessage().c_str());
  71.         Exit(EXIT_FAILURE);
  72.     }
  73.     WaitForSingleObject(pi.hProcess, INFINITE); // Wait for it to finish
  74.    
  75.     DWORD result = 0;
  76.     GetExitCodeProcess(pi.hProcess, &result);
  77.     return result;
  78. }
  79.  
  80. int main(int argc, char** argv) {
  81.    
  82.     printf(GetCommand(argc,argv).c_str());
  83.    
  84.     // First make sure we aren't going to read nonexistent arrays
  85.     if(argc < 2) {
  86.         printf("\n--------------------------------");
  87.         printf("\nUsage: ConsolePauser.exe <filename> <parameters>\n");
  88.         Exit(EXIT_SUCCESS);
  89.     }
  90.    
  91.     // Make us look like the paused program
  92.     SetConsoleTitle(argv[1]);
  93.    
  94.     // Then build the to-run application command
  95.     string command = GetCommand(argc,argv);
  96.    
  97.     // Then execute said command
  98.     DWORD returnvalue = ExecuteCommand(command);
  99.  
  100.     // Done? Print return value of executed program
  101.     printf("\n--------------------------------");
  102.     printf("\nProcess exited with return value %lu\n",returnvalue);
  103.     Exit(EXIT_SUCCESS);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment