web-dev-qa-db-fra.com

Comment attendre que ShellExecute s'exécute?

J'ai réussi à utiliser ShellExecute dans VC++ afin de lancer un document. Maintenant, je souhaite exécuter un outil de ligne de commande qui reçoit des arguments, et s'exécuter en arrière-plan (comme masqué, non minimisé) et le laisser bloquer mon flux de programme, afin que je puisse attendre qu'il se termine. Comment modifier la ligne de commande de:

ShellExecute(NULL,"open",FULL_PATH_TO_CMD_LINE_TOOL,ARGUMENTS,NULL,SW_HIDE);

Le problème est que j'ai un outil qui convertit le html en pdf, et je souhaite qu'une fois l'outil terminé, aka pdf est prêt, d'avoir un autre ShellExecute pour le voir.

22
buddy123

Il y a un article CodeProject qui montre comment, en utilisant ShellExecuteEx au lieu de ShellExecute:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\\MyProgram.exe";        
ShExecInfo.lpParameters = "";   
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL; 
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
CloseHandle(ShExecInfo.hProcess);

Le point crucial est le drapeau SEE_MASK_NOCLOSEPROCESS, qui, comme le dit MSDN

Permet d'indiquer que le membre hProcess reçoit le descripteur de processus. Ce descripteur est généralement utilisé pour permettre à une application de savoir quand un processus créé avec ShellExecuteEx se termine

Notez également que:

L'application appelante est responsable de la fermeture de la poignée lorsqu'elle n'est plus nécessaire.

49
Roger Rowland

Vous pouvez également utiliser CreateProcess au lieu de ShellExecute/ShellExecuteEx. Cette fonction comprend une option de wrapper cmd.exe, renvoyant le code de sortie et retournant stdout. (Les inclusions peuvent ne pas être parfaites).

Notes: Dans mon utilisation, je savais qu'il devait y avoir des résultats stdout, mais la fonction PeekedNamePipe ne retournerait pas toujours le nombre d'octets au premier essai, d'où la boucle. Peut-être que quelqu'un peut comprendre cela et publier une révision? De plus, peut-être qu'une autre version devrait être produite qui renvoie stderr séparément?

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <Shellapi.h>


/*
Note: 
    The exitCode for a "Cmd Process" is not the exitCode
    for a sub process launched from it!  That can be retrieved
    via the errorlevel variable in the command line like so:
    set errorlevel=&[launch command]&echo.&echo exitCode=%errorlevel%&echo.
    The stdOut vector will then contain the exitCode on a seperate line
*/
BOOL executeCommandLine( const CStringW &command,
                         DWORD &exitCode,
                         const BOOL asCmdProcess=FALSE,
                         std::vector<CStringW> *stdOutLines=NULL )
{
    // Init return values
    BOOL bSuccess = FALSE;
    exitCode = 0;
    if( stdOutLines ) stdOutLines->clear();

    // Optionally prepend cmd.exe to command line to execute
    CStringW cmdLine( (asCmdProcess ? L"cmd.exe /C " : L"" ) +
                      command );

    // Create a pipe for the redirection of the STDOUT 
    // of a child process. 
    HANDLE g_hChildStd_OUT_Rd = NULL;
    HANDLE g_hChildStd_OUT_Wr = NULL;
    SECURITY_ATTRIBUTES saAttr; 
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 
    bSuccess = CreatePipe( &g_hChildStd_OUT_Rd, 
                           &g_hChildStd_OUT_Wr, &saAttr, 0);
    if( !bSuccess ) return bSuccess;         
    bSuccess = SetHandleInformation( g_hChildStd_OUT_Rd, 
                                     HANDLE_FLAG_INHERIT, 0 );
    if( !bSuccess ) return bSuccess;         

    // Setup the child process to use the STDOUT redirection
    PROCESS_INFORMATION piProcInfo; 
    STARTUPINFO siStartInfo;    
    ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
    ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
    siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.hStdError = g_hChildStd_OUT_Wr;
    siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

    // Execute a synchronous child process & get exit code
    bSuccess = CreateProcess( NULL, 
      cmdLine.GetBuffer(),  // command line 
      NULL,                 // process security attributes 
      NULL,                 // primary thread security attributes 
      TRUE,                 // handles are inherited 
      0,                    // creation flags 
      NULL,                 // use parent's environment 
      NULL,                 // use parent's current directory 
      &siStartInfo,         // STARTUPINFO pointer 
      &piProcInfo );        // receives PROCESS_INFORMATION    
    if( !bSuccess ) return bSuccess;         
    WaitForSingleObject( piProcInfo.hProcess, (DWORD)(-1L) );
    GetExitCodeProcess( piProcInfo.hProcess, &exitCode );   
    CloseHandle( piProcInfo.hProcess );
    CloseHandle( piProcInfo.hThread );

    // Return if the caller is not requesting the stdout results
    if( !stdOutLines ) return TRUE;

    // Read the data written to the pipe
    DWORD bytesInPipe = 0;
    while( bytesInPipe==0 ){
        bSuccess = PeekNamedPipe( g_hChildStd_OUT_Rd, NULL, 0, NULL, 
                                  &bytesInPipe, NULL );
        if( !bSuccess ) return bSuccess;
    }
    if( bytesInPipe == 0 ) return TRUE; 
    DWORD dwRead; 
    CHAR *pipeContents = new CHAR[ bytesInPipe ];    
    bSuccess = ReadFile( g_hChildStd_OUT_Rd, pipeContents, 
                         bytesInPipe, &dwRead, NULL);
    if( !bSuccess || dwRead == 0 ) return FALSE; 

    // Split the data into lines and add them to the return vector
    std::stringstream stream( pipeContents );
    std::string str;
    while( getline( stream, str ) ) 
        stdOutLines->Push_back( CStringW( str.c_str() ) );

    return TRUE;
}
1
BuvinJ