Autor Beitrag
bms
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 735

Win XP
Delphi 3, Delphi 6 PE, Delphi 2005 PE
BeitragVerfasst: Fr 04.02.05 01:59 
Kann mir einer sagen wie ich ein Programm starten kann und seine Handle erfahre.

Ich will nichts mit FindWindow usw. Das ist mir zu unsicher. Ich will mehrere gleiche Programme starten und sie geziehlt wieder beenden könne. Dazu brauche ich das Handle.

ShellExecute bietet hier nichts und bei CreateProcess finde ich auch nichts richtiges.

_________________
VORSICHT: Ich behersche seit heute die Fünf-Finger-Handflächen-Herz-Explodier-Schlag-Kampf-Technik >:(
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Fr 04.02.05 07:21 
Zitat:
Kann mir einer sagen wie ich ein Programm starten kann und seine Handle erfahre.

Welches Handle meinst du? Fensterhandle oder Prozesshandle?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
function RunProcess(const FileName: String): HWND;

  function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
  begin
    TList(lParam).Add(Pointer(hWnd));
    Result := True;
  end;

var
  SI: TStartupInfo;
  PI: TProcessInformation;
  List: TList;
  ProcessId: DWORD;
  I: Integer;
begin

  Result := 0;
  List := TList.Create;
  try
    FillChar(SI, SizeOf(TStartupInfo), 0);
    SI.cb := SizeOf(TStartupInfo);
    if CreateProcess(nil, PChar(FileName), nil, nil, False, 0, nil, nil, SI, PI) then
    begin
      WaitForInputIdle(PI.hProcess, INFINITE);
      if EnumWindows(@EnumWindowsProc, Longint(List)) then
      begin
        for I := 0 to List.Count - 1 do
        begin
          GetWindowThreadProcessId(Longint(List.Items[I]), ProcessId);
          if PI.dwProcessId = ProcessId then
          begin
            Result := Longint(List.Items[I]);
            Break;
          end;
        end;
      end;
      CloseHandle(PI.hProcess);
      CloseHandle(PI.hThread);
    end;
  finally
    List.Free;
  end;

end;

_________________
Ciao, Sprint.