Einzelnen Beitrag anzeigen

bluescreen25

Registriert seit: 27. Nov 2005
Ort: Geldern
229 Beiträge
 
Delphi 7 Enterprise
 
#3

Re: Programm öffnen und Handle erhalten

  Alt 11. Nov 2007, 21:12
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:
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
 begin //bei Win95/98 evt problematisch
  TList(lParam).Add(Pointer(hWnd));
  Result := True;
 end;
Aufruf z.B.

Delphi-Quellcode:
...
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;
Falls du im Nachhinein nochmal Fenster einfangen musst, gibt es eigendlich eine bessere GetWndHandle Funktion, die etwas genauer arbeitet...


Gruß, bluescreen25
...und ich dachte, Delphi ist ein Programmgenerator mit nur einem Button......tzzz
  Mit Zitat antworten Zitat