![]() |
Programm starten ohne Shellexecute
Laut PSDK dient Shellexecute nicht dazu eine fremde Anwendung zu starten, sondern
Zitat:
Also um zum Beispiel eine Textdatei zu drucken ruft man Shellexecute mit dem Verb 'print' und der entsprechenden Datei auf. Das man damit auch fremde Anwendungen starten kann ist ein Abfallprodukt und ist eher als Mißbrauch zu sehen. Wenn man es richtig machen will, sollte man CreateProcess benutzen. Ich habe dazu mal eine kleine Funktion gebastelt. Der Funktion übergibt man den Pfad mit der Anwendung, ein Flag, welcher bestimmt, ob gewartet werden soll oder nicht bis die gestartetet Anwendung beendet ist und eine Variable vom Typ Cardinal, die dann die ProzessID enthält. [edit=Daniel G] himitsu hat ![]() [/edit]
Delphi-Quellcode:
[edit=fkerber]Neu abgespeichert wg. Code-Highlighting. Mfg, fkerber[/edit]
function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PCardinal): Longword;
var StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; begin FillChar(StartupInfo, SizeOf(StartupInfo), #0); StartupInfo.cb := SizeOf(StartupInfo); StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK; StartupInfo.wShowWindow := ShowCmd; if not CreateProcess(nil, @Filename[1], nil, nil, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then Result := WAIT_FAILED else begin try if not wait then begin if ProcID <> nil then ProcID^ := ProcessInfo.dwProcessId; Result := S_OK; exit; end; WaitForSingleObject(ProcessInfo.hProcess, INFINITE); GetExitCodeProcess(ProcessInfo.hProcess, Result); finally CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); end; end; end; |
was muß man den für Procid einsetzten?
|
Eine Ganzahl Variable.
Delphi-Quellcode:
[edit=fkerber]Neu abgespeichert wg. Code-Highlighting. Mfg, fkerber[/edit]
procedure TForm1.Button1Click(Sender: TObject);
var ProcID: Cardinal; begin if OpenDialog1.Execute then RunProcess(OpenDialog1.FileName, SW_MINIMIZE, TRUE, ProcID); Messagebox(0, 'fertig', @OpenDialog1.Filename[1], 0); end; |
Re: Programm starten ohne Shellexecute
sh17 und winkel79 stellen in diesem
![]()
Delphi-Quellcode:
Win32IsUnicode steht dabei für ein globales
function ExecAndWait(Filename, Params: Widestring; WindowState: word = SW_SHOWNORMAL): boolean;
var ShExecInfoW: SHELLEXECUTEINFOW; ShExecInfoA: SHELLEXECUTEINFOA; r : Cardinal; begin Result := false; if Filename = '' then exit; if not WideFileExists(FileName) then exit; if Win32IsUnicode then begin ZeroMemory(@ShExecInfoW, SizeOf(ShExecInfoW)); ShExecInfoW.Wnd := GetForegroundWindow; ShExecInfoW.cbSize := sizeof(SHELLEXECUTEINFOW); ShExecInfoW.fMask := SEE_MASK_NOCLOSEPROCESS; ShExecInfoW.lpVerb := 'open'; ShExecInfoW.lpFile := PWideChar(Filename); ShExecInfoW.lpParameters := PWideChar(Params); ShExecInfoW.lpDirectory := PWideChar(WideExtractFileDir(Filename)); ShExecInfoW.nShow := WindowState; Result := ShellExecuteExW(@ShExecInfoW); end else begin ZeroMemory(@ShExecInfoA, SizeOf(ShExecInfoA)); ShExecInfoA.Wnd := GetForegroundWindow; ShExecInfoA.cbSize := sizeof(SHELLEXECUTEINFOA); ShExecInfoA.fMask := SEE_MASK_NOCLOSEPROCESS; ShExecInfoA.lpVerb := 'open'; ShExecInfoA.lpFile := PChar(AnsiString(Filename)); ShExecInfoA.lpParameters := PChar(AnsiString(Params)); ShExecInfoA.lpDirectory := PChar(AnsiString(WideExtractFileDir(Filename))); ShExecInfoA.nShow := WindowState; Result := ShellExecuteExA(@ShExecInfoA); end; try if Result then begin if Win32IsUnicode then r := WaitForSingleObject(ShExecInfoW.hProcess, INFINITE) else r := WaitForSingleObject(ShExecInfoA.hProcess, INFINITE); end; finally if Win32IsUnicode then CloseHandle(ShExecInfoW.hProcess) else CloseHandle(ShExecInfoA.hProcess); end; end;
Delphi-Quellcode:
Weitere Infos gibt es hier:
(Win32Platform = VER_PLATFORM_WIN32_NT)
![]() ![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:01 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