Advertisement
obernardovieira

Run program by C++ code (2 ways, Windows)

Jul 29th, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. //---------------------------------------------------------
  2. //Run in new console
  3. //---------------------------------------------------------
  4.  
  5. #include <windows.h>    //You need shell32.lib for this one
  6.  
  7. bool main() {
  8.     LPCWSTR szPath = TEXT("C:\\WINDOWS\\system32\\Calc.exe");
  9.  
  10.     HINSTANCE hRet = ShellExecute(
  11.         HWND_DESKTOP,   //Parent window
  12.         TEXT("open"),   //Operation to perform
  13.         szPath,         //Path to program
  14.         NULL,           //Parameters
  15.         NULL,           //Default directory
  16.         SW_SHOW);       //How to open
  17.  
  18.     /*
  19.     The function returns a HINSTANCE (not really useful in this case)
  20.     So therefore, to test its result, we cast it to a LONG.
  21.     Any value over 32 represents success!
  22.     */
  23.  
  24.     if((LONG)hRet <= 32)
  25.     {
  26.         MessageBox(HWND_DESKTOP,TEXT("Unable to start program"),TEXT(""),MB_OK);
  27.         return true;
  28.     }
  29.     system("pause");
  30.     return true;
  31. }
  32.  
  33. //---------------------------------------------------------
  34. //Run inside console
  35. //---------------------------------------------------------
  36.  
  37. #include <iostream>
  38. #include <windows.h>
  39.  
  40. bool main() {
  41.     WinExec("C:\\WINDOWS\\system32\\Calc.exe",SW_SHOW);
  42.     system("pause");
  43.     return true;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement