简体   繁体   中英

Running a console app silently (suppress the console window from appearing)

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:

在此处输入图片说明

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.

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM