Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (C++/win32) How to call an external console-mode exe, without showing the console window, and optionally capturing the output?

(C++/win32) How to call an external console-mode exe, without showing the console window, and optionally capturing the output?

Scheduled Pinned Locked Moved C / C++ / MFC
c++phphtmltoolshelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Petruza
    wrote on last edited by
    #1

    ( lang: C/C++. platform: windows ) Hi, I want to do the following from my win32 app (could be mfc or whatever): 1) Call php.exe, send as command line parameter the name of a .php script to execute. 2) The console window must not show up. 3) (optional) Grab php's output. An alternative to 3) would be executing something like "php.exe script.php > temp_output.html" and then reading temp_output.html I've tried ShellExecute(), CreateProcess() and System(), all of which show the console window, and with none of them I've been able to capture the process' output. Any [ hints | tutorials | code | help ] would be appreciated! Thanks!

    F 1 Reply Last reply
    0
    • P Petruza

      ( lang: C/C++. platform: windows ) Hi, I want to do the following from my win32 app (could be mfc or whatever): 1) Call php.exe, send as command line parameter the name of a .php script to execute. 2) The console window must not show up. 3) (optional) Grab php's output. An alternative to 3) would be executing something like "php.exe script.php > temp_output.html" and then reading temp_output.html I've tried ShellExecute(), CreateProcess() and System(), all of which show the console window, and with none of them I've been able to capture the process' output. Any [ hints | tutorials | code | help ] would be appreciated! Thanks!

      F Offline
      F Offline
      Force Code
      wrote on last edited by
      #2
        SECURITY\_ATTRIBUTES sa = {sizeof(sa),NULL,TRUE};
        HANDLE hProcOut = CreateFile("proc.out",GENERIC\_READ|GENERIC\_WRITE,
           FILE\_SHARE\_READ,&sa,CREATE\_ALWAYS, FILE\_ATTRIBUTE\_NORMAL, 0);
        .
        .
        .
            STARTUPINFO   si;
            ZeroMemory((PVOID)&si,sizeof(si));
            si.cb = sizeof(si);
            si.dwFlags = STARTF\_USESHOWWINDOW | STARTF\_USESTDHANDLES;
            si.wShowWindow = SW\_HIDE;
            si.hStdOutput = hProcOut;
            si.hStdError = hProcOut;
      
            PROCESS\_INFORMATION  pi;
      
            if (CreateProcess(0,cmd,0,0,TRUE,
                CREATE\_DEFAULT\_ERROR\_MODE,0,default\_dir,&si,&pi)) {
      
              DWORD  ExitCode;
              float elpsed\_crnt=0;
      
              while (1) {
      
                Sleep(250);
      
                GetExitCodeProcess(pi.hProcess,&ExitCode);
                if (bCancel || (ExitCode != STILL\_ACTIVE)) {
                  if (bCancel) TerminateProcess(pi.hProcess,0);
                  CloseHandle(pi.hProcess);
                  CloseHandle(pi.hThread);
                  break;
                }
      
              }
            }
      
      F 1 Reply Last reply
      0
      • F Force Code
          SECURITY\_ATTRIBUTES sa = {sizeof(sa),NULL,TRUE};
          HANDLE hProcOut = CreateFile("proc.out",GENERIC\_READ|GENERIC\_WRITE,
             FILE\_SHARE\_READ,&sa,CREATE\_ALWAYS, FILE\_ATTRIBUTE\_NORMAL, 0);
          .
          .
          .
              STARTUPINFO   si;
              ZeroMemory((PVOID)&si,sizeof(si));
              si.cb = sizeof(si);
              si.dwFlags = STARTF\_USESHOWWINDOW | STARTF\_USESTDHANDLES;
              si.wShowWindow = SW\_HIDE;
              si.hStdOutput = hProcOut;
              si.hStdError = hProcOut;
        
              PROCESS\_INFORMATION  pi;
        
              if (CreateProcess(0,cmd,0,0,TRUE,
                  CREATE\_DEFAULT\_ERROR\_MODE,0,default\_dir,&si,&pi)) {
        
                DWORD  ExitCode;
                float elpsed\_crnt=0;
        
                while (1) {
        
                  Sleep(250);
        
                  GetExitCodeProcess(pi.hProcess,&ExitCode);
                  if (bCancel || (ExitCode != STILL\_ACTIVE)) {
                    if (bCancel) TerminateProcess(pi.hProcess,0);
                    CloseHandle(pi.hProcess);
                    CloseHandle(pi.hThread);
                    break;
                  }
        
                }
              }
        
        F Offline
        F Offline
        Force Code
        wrote on last edited by
        #3

        Probably should add that whole thing is running in a seperate thread from the UI. (CreateThread, etc.)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups