Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler) (https://www.delphipraxis.net/80133-shellapiex-funzt-aber-hauptprogramm-stuerzt-ab-einzeiler.html)

Ajin 3. Nov 2006 11:18


ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Huhu ich mal wieder!

Ich nutze die ShellApiEx.pas wie folgt:

Delphi-Quellcode:
if ShellExecuteAndWaitW(Application.Handle, 'open', 'MSDE\Setup_MSDE2K.EXE', nil, nil, sw_shownormal, TRUE) = 42 then
begin
 //Gestartete Anwendung wurde beendet
 showmessage('MSDE Setup läuft nicht mehr');
end;
Ich versuche auf das Beenden des MSDE 2.0 Setups reagieren. Verschiedene Tests mit NOTEPAD ergaben, dass die Funktion den Wert 42 zurückgiebt wenn das aufgerufene Programm geschlossen wird. Alles wunderbar!

Jetzt hab ich halt das NOTEPAD gegen ein Setup getauscht. Der Aufruf klappt prima, auch auf das vorzeitige Beenden des Setups reagiert mein Programm. Doch leider kann das Setup nicht vollständig ausgeführt werden - bei ca. 12 Sekunden Rest-installationszeit reagiert meine Hauptanwendung nicht mehr....

Wenn ich das MSDE Setup ohne "fremde" Hilfe aufrufe funktionierts prima. Auch mit dem "normalen" Shellexecute lässt sich das MSDE Setup ganz durchführen - nur nicht mit dem ShellExecuteAndWaitW :(

Woran könnte das liegen?

Luckie 3. Nov 2006 11:39

Re: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler
 
Wie sieht denn ShellExecuteAndWaitW aus?

Ajin 3. Nov 2006 11:43

Re: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler
 
Delphi-Quellcode:
unit ShellApiEx;
(******************************************************************************)
(*************   This code is released into the PUBLIC DOMAIN  ***************)
(*************   [url]http://assarbad.net[/url] | [url]http://assarbad.org[/url]     ***************)
(******************************************************************************)
interface
uses
  Windows;

{.$DEFINE UNICODE}// Defines wether the unicode or the ansi prototype is to be used!

function ShellExecuteAndWaitA(hWnd: HWND; Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer; bWait: BOOL): HINST; stdcall;
function ShellExecuteAndWaitW(hWnd: HWND; Operation, FileName, Parameters, Directory: PWideChar; ShowCmd: Integer; bWait: BOOL): HINST; stdcall;

type
  TShellExecuteAndWait = function(hWnd: Windows.HWND;
{$IFDEF UNICODE}
    Operation, FileName, Parameters, Directory: PWideChar;
{$ELSE}
    Operation, FileName, Parameters, Directory: PAnsiChar;
{$ENDIF}
    ShowCmd: Integer; bWait: BOOL): HINST; stdcall;
var
  ShellExecuteAndWait: TShellExecuteAndWait =
{$IFDEF UNICODE}
  ShellExecuteAndWaitW;
{$ELSE}
  ShellExecuteAndWaitA;
{$ENDIF}

implementation
uses ShellApi;
(*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  GERMAN ABSTRACT (important informations below in English)

  Meine Kommentare im Quelltext sind Englisch, hier beschränke ich mich aber auf
  Deutsch:

  ShellExecute() ist weithin bekannt. Komischerweise gilt dies scheinbar
  (zumindest unter Delphianern nicht für ShellExecuteEx(). Man sieht oft
  Beispiele auf die Frage: "wie führe ich ein Programm aus und warte, bis es
  beendet ist?" - ich habe aber bisher nur Varianten mit CreateProcess gesehen,
  es gehört aber IMHO zum guten Ton auch auf ShellExecuteEx zu verweisen.

  Wieso? Nun, es ist eine erweiterte Version von ShellExecute und bietet die
  selben Vorteile (i.e. Ausführen von "Dateien" die bei einer Anwendung
  registriert sind, Benutzung von Verben ... EDIT, PRINT, OPEN ...). Außerdem
  wird eben das Handle zum neu erzeugten Prozeß zurückgeliefert, so daß man auf
  dessen Ende warten kann.

  Ansonsten kann man ShellExecuteEx auch nehmen um Informationen über den Prozeß,
  der erstellt wird, zu erhalten. Dazu möchte ich auf das Windows Platform SDK
  verweisen, da es dort eine ausführliche Beschreibung gibt und es müßig ist
  alle Varianten der Nutzung zu erklären. Die wichtigste Ist hier umgesetzt.

  Assarbad AT gmx DOT info [2003-06-22]

  PS: Ich arbeite mit den Records als Variablen, um die Abfrage ob eine
      Allozierung erfolgreich war zu vermeiden.
  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)

(*
  There are 2(3) new functions declared in this unit:
  - ShellExecuteAndWaitW(),
  - ShellExecuteAndWaitA()
  (ShellExecuteAndWait as either Unicode or Ansi version of the above functions!)

  The functions share the syntax with the standard ShellExecute(A|W) API, with
  one exception:
    - There is an additional parameter. On Delphi 4 up you could define a default
      value of "False" which would resemble the functionality of ShellExecute.
      I did not define this value to keep compatibility with Delphi 3.
      Description:
        The value is a BOOL (LongBool) which determined wether the function
        should wait for the new process to finish or not.
*)

function ShellExecuteAndWaitW(hWnd: HWND; Operation, FileName, Parameters, Directory: PWideChar; ShowCmd: Integer; bWait: BOOL): HINST; stdcall;
var
  sei: TShellExecuteInfoW;
begin
// Delete any contents of this variable (undetermined for complex types)
  ZeroMemory(@sei, sizeof(sei));
// Fill the information structure with all ShellExecute standard parameters
  with sei do
  begin
    cbSize := sizeof(sei);
    fMask := SEE_MASK_NOCLOSEPROCESS; // we want a process handle returned!
    Wnd := hWnd;
    lpVerb := Operation; // Pre-defined: edit, explore, find, open, print, nil
    lpFile := FileName;
    lpParameters := Parameters;
    lpDirectory := Directory;
    nShow := ShowCmd;
  end;
// Call ShellExecuteEx()
  if ShellExecuteExW(@sei) then
  try
    if bWait then
      WaitForSingleObject(sei.hProcess, INFINITE);
  finally
// Close references to new process! THIS ALSO NEEDS TO BE DONE IF THE PROCESS
// IS ALREADY FINISHED WITH EXECUTION!!!
    CloseHandle(sei.hProcess);
  end;
// Return a value (not actually HINST, as with ShellExecute! ... see Platform SDK)
  result := sei.hInstApp;
end;

function ShellExecuteAndWaitA(hWnd: HWND; Operation, FileName, Parameters, Directory: PAnsiChar; ShowCmd: Integer; bWait: BOOL): HINST; stdcall;
var
  sei: TShellExecuteInfoA;
begin
// Delete any contents of this variable (undetermined for complex types)
  ZeroMemory(@sei, sizeof(sei));
// Fill the information structure with all ShellExecute standard parameters
  with sei do
  begin
    cbSize := sizeof(sei);
    fMask := SEE_MASK_NOCLOSEPROCESS; // we want a process handle returned!
    Wnd := hWnd;
    lpVerb := Operation; // Pre-defined: edit, explore, find, open, print, nil
    lpFile := FileName;
    lpParameters := Parameters;
    lpDirectory := Directory;
    nShow := ShowCmd;
  end;
// Call ShellExecuteEx()
  if ShellExecuteExA(@sei) then
  try
    if bWait then
      WaitForSingleObject(sei.hProcess, INFINITE);
  finally
// Close references to new process! THIS ALSO NEEDS TO BE DONE IF THE PROCESS
// IS ALREADY FINISHED WITH EXECUTION!!!
    CloseHandle(sei.hProcess);
  end;
// Return a value (not actually HINST, as with ShellExecute! ... see Platform SDK)
  result := sei.hInstApp;
end;

end.

Ajin 6. Nov 2006 10:10

Re: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler
 
Tach Jungs!

Hat jemand eine Idee was ich bei der Benutzung dieser routine falsch mache?

zeras 28. Okt 2012 09:34

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Ist zwar schon einige Tage alt, aber ich habe auch das Problem, dass die Funktion nicht geht. Auch bei mir stürzt mein Programm ab. Wenn ich nur ShellExecute nutze, stürzt das Programm nicht ab.

Hier mein Aufruf.
Delphi-Quellcode:
ShellExecuteAndWaitW(Application.Handle, 'open', PChar('notepad.exe'), PChar(fname), Nil, SW_SHOW, True);

himitsu 28. Okt 2012 10:07

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Was heißt "stürzt ab" ?

Und in welcher Zeile, bzw. bei welchem Befehl?

zeras 28. Okt 2012 10:38

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Zitat:

Zitat von himitsu (Beitrag 1188724)
Was heißt "stürzt ab" ?

Und in welcher Zeile, bzw. bei welchem Befehl?


Ich nutze TMS Grids und dort passiert offensichtlich der Absturz. Windows meldet nur einen Fehler und will nach der Ursache suchen.
Quelltext wie folgt:

Delphi-Quellcode:
function ShellExecuteAndWaitW(hWnd: HWND; Operation, FileName, Parameters, Directory: PWideChar; ShowCmd: Integer; bWait: BOOL): HINST; stdcall;
var
  sei: TShellExecuteInfoW;
begin
// Delete any contents of this variable (undetermined for complex types)
  ZeroMemory(@sei, sizeof(sei));
// Fill the information structure with all ShellExecute standard parameters
  with sei do
  begin
    cbSize := sizeof(sei);
    fMask := SEE_MASK_NOCLOSEPROCESS; // we want a process handle returned!
    Wnd := hWnd;
    lpVerb := Operation; // Pre-defined: edit, explore, find, open, print, nil
    lpFile := FileName;
    lpParameters := Parameters;
    lpDirectory := Directory;
    nShow := ShowCmd;
  end;
// Call ShellExecuteEx()
  if ShellExecuteExW(@sei) then <<<<<<<<<<<<<hier kommt Aufruf von MsgHookProc ins AdvGrid


function MsgHookProc(Code: Integer; CurrentProcess: Cardinal; HookStruct: PCWPStruct): integer; stdcall;
begin
  ...

  Result := CallNextHookEx(MsgHook, Code, CurrentProcess, Integer(HookStruct)) <<< bei Aufruf der Funktion kommt dann die Windows Meldung
end;

himitsu 28. Okt 2012 10:45

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Hmmm, dann ist es wohl eher ein Problem bei ADV?

Versuch es mal probehalber mit einer 0, statt dem Application.Handle.

zeras 28. Okt 2012 11:03

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Zitat:

Zitat von himitsu (Beitrag 1188738)
Versuch es mal probehalber mit einer 0, statt dem Application.Handle.


So kommt auch die Windowsmeldung. Vielleicht sollte ich mal TMS auf den letzten Stand bringen?
Delphi-Quellcode:
ShellExecuteAndWait(0, 'open', PChar('notepad.exe'), PChar(fname), Nil, SW_SHOW, True);

zeras 28. Okt 2012 15:25

AW: ShellApiEx funzt aber Hauptprogramm stürzt ab (Einzeiler)
 
Zitat:

Zitat von zeras (Beitrag 1188744)


So kommt auch die Windowsmeldung. Vielleicht sollte ich mal TMS auf den letzten Stand bringen?
Delphi-Quellcode:
ShellExecuteAndWait(0, 'open', PChar('notepad.exe'), PChar(fname), Nil, SW_SHOW, True);

Nun bin ich weiter. Wenn ich das Programm nicht in der IDE starte, sondern die Exe, dann geht es. Ist doch komisch oder?
TMS habe ich noch nicht aktualisiert.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz