![]() |
Programm öffnen und Handle erhalten
Hallo,
wie man ein Programm öffnet weiss ich ja:
Delphi-Quellcode:
procedure OpenFile(FileName: string);
var c: array[0..800] of Char; begin StrPCopy(c, FileName); ShellExecute(Application.Handle, 'open', c, nil, nil, SW_NORMAL); end; Nur wie bekomm ich nun das Handle des geöffneten Programmes zurück? |
Re: Programm öffnen und Handle erhalten
Schau Dir mal CreateProcess oder ShellExecuteEx an.
|
Re: Programm öffnen und Handle erhalten
Delphi-Quellcode:
...
var ProcID : Cardinal; WND : HWnd; ... function RunProcess(FileName: string; ShowCmd: DWORD; wait: Boolean; ProcID: PDWORD): Longword; var StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; ExitInfo : PExitThreadDebugInfo; 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 if wait = FALSE then begin if ProcID <> nil then ProcID^ := ProcessInfo.dwProcessId; exit; end; WaitForSingleObject(ProcessInfo.hProcess, INFINITE); GetExitCodeProcess(ProcessInfo.hProcess, Result); end; if ProcessInfo.hProcess <> 0 then CloseHandle(ProcessInfo.hProcess); if ProcessInfo.hThread <> 0 then CloseHandle(ProcessInfo.hThread); end;
Delphi-Quellcode:
function GetWndHandle(ProcessId: DWORD): HWND;
var List: TList; I: Integer; PID: DWORD; begin Result := 0; List := TList.Create; try EnumWindows(@EnumWindowsProc, LPARAM(List)); for I := 0 to List.Count - 1 do if GetWindowThreadProcessId(HWND(List.Items[I]), @PID) <> 0 then if PID = ProcessId then begin Result := HWND(List.Items[I]); Break; end; finally List.Free; end; end;
Delphi-Quellcode:
Aufruf z.B.
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin //bei Win95/98 evt problematisch TList(lParam).Add(Pointer(hWnd)); Result := True; end;
Delphi-Quellcode:
Falls du im Nachhinein nochmal Fenster einfangen musst, gibt es eigendlich eine bessere GetWndHandle Funktion, die etwas genauer arbeitet...
...
RunProcess('C:\programm.exe', SW_SHOW, FALSE, @ProcID); repeat WND := GetWndHandle(ProcID);//warten bis gestartet bzw gefunden über ProcID application.ProcessMessages; sleep(500); until (WND <> 0);//or stopSearch; hier ggf einen Timer mitlaufen lassen, der nach x sek die Suche beendet if (WND <> 0) then begin ...dein Code...was mit Handle zu machen ist wie z.B. BringWindowToTop(WND); end; Gruß, bluescreen25 |
Re: Programm öffnen und Handle erhalten
Zitat:
|
Re: Programm öffnen und Handle erhalten
Zitat:
Für meine Zwecke hatte das Vorteile. Gruß bluescreen25 |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:57 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 by Thomas Breitkreuz