1

I have looked at these posts addressing similar issues: From Python and Extract Silently and Command Line Option

None addresses my specific issue.

I would like to call 7za.exe (and possibly other console applications, that by default result in the shell command window) from within an application written in ANSI C without the shell prompt window popping up for each call. From within the Windows command shell, I can append > nul to the end of a command line call of 7za, and it suppresses everything, as shown here:

enter image description here

However, I need to call it in a loop (several hundred times), from a Windows application, resulting in a constant flicker as Windows launches a shell then kills it when the command is complete.

So far I have tried appending > nul, just as I illustrated in the command prompt image above, then using the system() command, as well as a modified system command which launches exe in another process:

sprintf(command, "7za.exe x -y -o%s %s > nul", filepathUnComp, filepath); 
system(command);  

or:

SystemX(command);  

Where SystemX is defined:

int SystemX(command)
{
    STARTUPINFO sj;
    PROCESS_INFORMATION pj;
    int exit;

    ZeroMemory( &sj, sizeof(sj) );
    sj.cb = sizeof(sj);
    ZeroMemory( &pj, sizeof(pj) );

    if(CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &sj, &pj) == 0)
    {
        //AED_GetErrorMessage(AEDNV_FAILED_CREATE_PROCESS,cmd);
        return -1;
    }

    // Wait until child processes exit.
    WaitForSingleObject( pj.hProcess, IGNORE ); //ingnore signal

    //Get exit code
    GetExitCodeProcess(pj.hProcess, (LPDWORD)(&exit));

    return exit;
}  

Both of these methods result in the shell prompt window flicker.

Is there a way to run 7za.exe within an application completely silent, that is, without instantiating the shell command window flicker?

If this is not possible using 7za, I am also open to hearing about other approaches.

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • no, it's not possible. you're starting a shell, which will NECESSARILY start the window. `> nul` will do NOTHING to "hide" that window. a window is not "output" of the program, it's where the program's output would GO, if you hadn't redirected it to nul. – Marc B Jan 06 '15 at 19:40
  • @MarcB Are you aware of a method, other than 7za (or similar)? I am open to a completely different approach, not locked into using 7za. – ryyker Jan 06 '15 at 19:44
  • 1
    relevant: http://stackoverflow.com/questions/12010103/launch-a-program-from-command-line-without-opening-a-new-window, but start itself would run in a shell and cause its own window to be created. basically you'd have to tie into the windows api to start a background process without window. – Marc B Jan 06 '15 at 19:45
  • 2
    If you look around a little, I'm sure you might find some [appropriate `CreateProcess` flag](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684863%28v=vs.85%29.aspx) that you can use to tell `CreateProcess` to not create any console windows. – Some programmer dude Jan 06 '15 at 19:45
  • @JoachimPileborg - I think your link shows some possibilities. Thanks – ryyker Jan 06 '15 at 19:50
  • @MarcB - Thanks, your first explanation helps me to see the real issue. I Will look into windows APIs. – ryyker Jan 06 '15 at 19:51
  • @JoachimPileborg - DWORD `CREATE_NO_WINDOW` (defined in windows API) added to argument 6 of `CreateProcess` worked. No command window pops up. Thanks! – ryyker Jan 06 '15 at 19:58

1 Answers1

2

This may be useful to other Windows programmers who need to suppress the shell prompt from appearing when running an executable program that by default launches a shell: (thanks to @Marc B and @ Joachim Pileborg )

Change the original line in the OP SystemX code from:

 if(CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &sj, &pj) == 0);  

to:

 if(CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &sj, &pj) == 0); 
ryyker
  • 22,849
  • 3
  • 43
  • 87