Thema: Delphi Ärger mit Shellexecute

Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.690 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Ärger mit Shellexecute

  Alt 31. Mai 2022, 19:56
Vielleicht hilft dir mein code schnippsel weiter um ans ziel zu gelangen.
Delphi-Quellcode:
type
  TExec = packed record
    Filename : string;
    PID : DWORD;
    WindowHandle : HWND;
  end;

var
  Exec: TExec;

procedure ResetExec;
begin
  Exec.Filename := '';
  Exec.PID := 0;
  Exec.WindowHandle := 0;
end;

function EnumWindowsProc(Wnd : HWND; ProcessID : Cardinal) : Boolean; stdcall;
var
  PID : Cardinal;
begin
  GetWindowThreadProcessId(Wnd, @PID);
  if ProcessID = PID then
    Exec.WindowHandle := Wnd;
  Result := True;
end;

function ExecFile(const AFilename: string): Boolean;
var
  Executable : string;
  Security : TSecurityAttributes;
  ProcessInfo: TProcessInformation;
  StartupInfo: TStartupInfo;
  dummy : HINST;
begin
  Result := False;
  ResetExec;

  if (not FileExists(AFilename)) then
    Exit;

  SetLength(Executable, MAX_PATH);
  dummy := FindExecutable(PChar(AFilename), nil, PChar(Executable));
  if dummy > 32 then
  begin
    SetLength(Executable, StrLen(PChar(Executable)));
    UniqueString(Executable);

    Security.nLength := SizeOf(TSecurityAttributes);
    Security.lpSecurityDescriptor := nil;
    Security.bInheritHandle := False;

    FillChar(StartupInfo, SizeOf(TStartupInfo), #0);
    StartupInfo.cb := SizeOf(TStartupInfo);
    StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
    StartupInfo.wShowWindow := SW_SHOWNORMAL {SW_HIDE};

    Win32Check(CreateProcess(PChar(Executable), PChar(Format('%s %s', [Executable, AFilename])), @Security, @Security, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(Executable)), StartupInfo, ProcessInfo));
    try
      WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
      Exec.Filename := Executable;
      Exec.PID := ProcessInfo.dwProcessId;
      EnumWindows(@EnumWindowsProc, LPARAM(Exec.PID));
    finally
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
      Result := True;
    end;
  end;
end;
nach dem aufruf steht in globaler variable Exec.WindowHandle was du brauchst. Ist ein Versuch Wert.
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat