ShellExecuteEx synchron / asynchron zum Programm?

sparco85

Grünschnabel
Hi Leute...

Hab gleich meine erste Frage... Wenn ich den Befehl ShellExecuteEx verwende und damit ein Executable öffne.... Wird mein restlicher Code dann abgearbeitet wenn das File noch offen ist, oder geht der Prozess erst weiter wenn das File wieder geschlossen wurde

Danke und Grüsse

Stefan
 
Das läuft asynchron, du erteilst der Shell lediglich den Befehl, das Programm zu starten. Danach laufen beide Programme gleichzeitig; bzw. deins läuft weiter und wartet nicht.

Wenn du auf das andere Programm warten willst, dann benutze CreateProcess oder mein folgendes Code-Schnipsel:

Code:
bool CWinUtils::StartAndWaitForProgram( const char* szCommandLine, GR::u32* pExitCode, int iShowWindowFlags )
{

  STARTUPINFO             sui;

  PROCESS_INFORMATION     pi;


  memset( &sui, 0, sizeof( sui ) );
  sui.cb = sizeof( sui );
  sui.wShowWindow = iShowWindowFlags;

  std::string   strFileName = szCommandLine,
                strCmdLine = szCommandLine;

  if ( ( strFileName.length() )
  &&   ( strFileName[0] == '"' ) )
  {
    // der Pfad ist in Anführungszeichen gehalten
    strCmdLine = strFileName;
    strFileName = strFileName.substr( 1 );
    if ( strFileName.find( '"' ) != std::string::npos )
    {
      strFileName = strFileName.substr( 0, strFileName.find( '"' ) );
    }
  }
  else if ( strFileName.length() )
  {
    if ( strFileName.find( ' ' ) != std::string::npos )
    {
      strFileName = strFileName.substr( 0, strFileName.find( ' ' ) );
    }
  }

  std::string   strCurDir = strFileName.c_str();

  while ( ( strCurDir.length() )
  &&      ( strCurDir[strCurDir.length() - 1] != '\\' ) )
  {
    strCurDir = strCurDir.substr( 0, strCurDir.length() - 1 );
  }

  if ( strCurDir.length() )
  {
    strCurDir = strCurDir.substr( 0, strCurDir.length() - 1 );
  }

  if ( strCurDir.length() == 2 )
  {
    strCurDir += '\"';
  }

  char    szCmdLine[MAX_PATH];

  wsprintf( szCmdLine, strCmdLine.c_str() );

  if ( !CreateProcess( strFileName.c_str(),
                       szCmdLine,
                       NULL,
                       NULL,
                       FALSE,
                       CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
                       NULL,
                       strCurDir.c_str(),
                       &sui,
                       &pi ) )
  {
    return false;
  }

  MSG     msg;

  DWORD   dwExitCode = STILL_ACTIVE;

  do
  {
    Sleep( 20 );
    if ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
      GetMessage( &msg, NULL, 0, 0 );
      if ( IsDialogMessage( ::GetParent( msg.hwnd ), &msg ) )
      {
        continue;
      }
      if ( msg.message == WM_ENDSESSION )
      {
        if ( (BOOL)msg.wParam )
        {
          // das aufgerufene Programm will Windows beenden
          // -> Loop beenden
          break;
        }
      }
      TranslateMessage( &msg );
      DispatchMessage( &msg );
    }
    GetExitCodeProcess( pi.hProcess, &dwExitCode );
  }
  while ( dwExitCode == STILL_ACTIVE );

  CloseHandle( pi.hThread );
  CloseHandle( pi.hProcess );

  if ( pExitCode != NULL )
  {
    *pExitCode = dwExitCode;
  }

  return true;

}
 
Zurück