Announcement

Collapse
No announcement yet.

Parameterübergabe bei CreateProcess

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parameterübergabe bei CreateProcess

    Hallo,<p>
    <b>Problem:</b> Die eigene Delphi-Anwendung soll ein anderes Programm mit Parameter (Optionen) starten.<br>
    <b>Lösung:</b><pre>
    begin
    ShellExecute(0, nil, PCHAR('test.exe'), PCHAR('-o'), 0, SW_SHOW);
    end;
    </pre>
    <b>Resultat:</b> Die Anwendung wird mit dem Parameter gestartet.<p>
    Nun sollte folgende Änderung eingebaut werden:<br>
    Das eigene Programm soll warten, bis die gestartet Anwendung beendet ist.<bR>
    <b>Lösung:</b><pre>
    var
    aSI : TStartupInfo;
    aPI : TProcessInformation;
    aProc : THandle;
    begin
    FillChar(aSI, SizeOf(aSI), 0);
    aSI.cb := SizeOf(aSI);
    aSI.wShowWindow := wShow;
    Win32Check(CreateProcess(PChar('test.exe'), PChar('-o'), nil, nil,
    False, Normal_Priority_Class, nil, nil, aSI, aPI));
    aProc := aPI.hProcess;
    CloseHandle(aPI.hThread);
    if WaitForSingleObject(aProc, Infinite) <> Wait_Failed then
    GetExitCodeProcess(aProc, Result);
    CloseHandle(aProc);
    end;</pre>
    <b>Resultat:</b> Die Anwendung wird gestartet, das eigene Programm wartet, bis das gestartet Programm beendet worden ist - aber: der Parameter wurde <u>nicht</u> mit übergeben.<p>
    Ich benutze Delphi 5. Hat jemand eine Idee, Lösung bzw. kennt dem Fehler?

  • #2
    Hallo Elmar,

    ich verwende fast den selben Code ohne Probleme. D5 und WinNT.
    <pre>
    function ExecAppAndWait(const sApp, sParams: string): dword;
    var
    aSI: TStartupInfo;
    aPI: TProcessInformation;
    aProc: THandle;
    begin
    FillChar(aSI, SizeOf(aSI), 0);
    aSI.cb := SizeOf(aSI);
    aSI.dwFlags := STARTF_USESHOWWINDOW;
    aSI.wShowWindow := sw_hide;
    Win32Check(CreateProcess(PChar(sApp), PChar(sParams), nil, nil, False,
    Normal_Priority_Class, nil, nil, aSI, aPI));
    aProc := aPI.hProcess;
    CloseHandle(aPI.hThread);
    if WaitForSingleObject( aProc, Infinite) <> Wait_Failed then
    GetExitCodeProcess( aProc, Result);
    CloseHandle( aProc);
    end;
    </pre>

    Tschau

    Torste

    Comment


    • #3
      Hallo Torsten,<p>
      auch Deine Lösung funktioniert bei mir nicht. Ich habe es mit verschiedenen Programmen versucht. Bei <b>ShellExecute</b> hat es <u>immer</u> funktioniert, bei <b>CreateProzess</b> hat's <u>nie</u> funktioniert. Und jetzt

      Comment


      • #4
        Hallo Elmar,

        hät mich ja auch gewundert, da der Code ja praktisch identisch ist. Mit welchem Betriebssystem und SP funktioniert es den nicht.

        Tschau

        Torste

        Comment


        • #5
          probiert CreateProcess(PChar(sApp + ' ' + sParams), nil, nil, nil, False, Normal_Priority_Class, nil, nil, aSI, aPI));

          Gruß Hage

          Comment


          • #6
            Hallo,<p>
            <b>@Hagen:</b> Auf die Idee bin ich auch schon gekommen, aber man erhält folgende Fehlermeldung:<br><br>
            <font color="red">Win32-Fehler. Code: 2.<br>
            Die angebene Datei wurde nicht gefunden.</font>
            <p>
            <b>@Torsten:</b> Ich habe es unter Windows 95B, Windows 98 SE, Windows 2000 (ohne SP) und Windows XP (ohne SP) getestet. In allen System das gleiche.
            <p>
            Nach einigen Versuchen habe ich festgestellt, dass folgende Lösung einwandfrei funktioniert:
            <pre>
            function ExecAppAndWait(const sApp, sParams: String; wShow: Word): DWord;
            var
            aSI : TStartupInfo;
            aPI : TProcessInformation;
            aProc : THandle;
            begin
            FillChar(aSI, SizeOf(aSI), 0);
            aSI.cb := SizeOf(aSI);
            aSI.dwFlags := STARTF_USESHOWWINDOW;
            aSI.wShowWindow := wShow;
            Win32Check(CreateProcess(PCHAR(sApp), PCHAR(sApp + ' ' + sParams), nil, nil,
            False, Normal_Priority_Class, nil, nil, aSI, aPI));
            aProc := aPI.hProcess;
            CloseHandle(aPI.hThread);
            if WaitForSingleObject(aProc, Infinite) <> Wait_Failed then
            GetExitCodeProcess(aProc, Result);
            CloseHandle(aProc);
            end;
            </pre&gt

            Comment


            • #7
              Richtig, hab eben nochmal in meinem Source geschaut. Den ersten PChar setze ich immer auf nil und nur in lpParameters gebe ich die Application + Leerzeichen + Parameter an.

              Gruß Hage

              Comment

              Working...
              X