Entwickler-Ecke

Sonstiges (Delphi) - Handle anhand von Dateiname eines Programmes ermitteln


n-regen - Mi 23.07.08 15:43
Titel: Handle anhand von Dateiname eines Programmes ermitteln
Hallo!

Die in diesem Tipp [http://www.delphi-treff.de/tipps/system/tipp/525] beschriebene Funktion, um anhand des EXE-Dateinamens eines Programmes sein Handle zu erhalten erzeugt bei mir eine Zugriffsverletzung.
Woran kann das liegen?
Die einzige Idee, die ich habe, ist, dass vielleicht bei EnumWindows(@MyEnumWindowProc, ProcessHandle); dahin geschrieben wird, wo nichts hin geschrieben werden darf.
Aber wenn man die Zeile auskommentiert, hängt sich das Programm auf.

Nils


Delete - Mi 23.07.08 16:48

Warum fragst du nicht den Autor der Funktion bzw. fragst im Forum von der Seite nach?


n-regen - Mi 23.07.08 16:52

Der Autor ist nicht genannt und Delphi-Forum.de ist immer noch meine erste Anlaufstelle, falls ich Fragen zu Delphi habe, auf die ich keine Antwort finde.


Philipp_Reitter - Mi 23.07.08 19:04

hast du das probiert:

Zitat:

Wenn die Funktion GetWindowThreadProcessId unter älteren Delphi-Versionen einen Fehler verursacht, muss folgender Code ergänzt werden:


Delphi-Quelltext
1:
2:
function GetWindowThreadProcessId(hWnd: HWND; var dwProcessId: DWORD): DWORD;
stdcall; external 'user32.dll' name 'GetWindowThreadProcessId'


und sonst debuggen uns suchen wo der fehler herkommt...


n-regen - Do 24.07.08 14:26

Hab ich schon versucht. Mein aktueller Quellcode ist folgender (ein Button auf dem Form, sonst nichts):

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:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TLHelp32, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

function GetWindowThreadProcessId(hWnd: HWND; var dwProcessId: DWORD): DWORD;
stdcall; external 'user32.dll' name 'GetWindowThreadProcessId';

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  WindowHandle: THandle;

function MyEnumWindowProc(AHandle: THandle; LParam: LongWord): boolean;
stdcall;
var
  ProcessID: THandle;
begin
  ProcessID := 0;
  GetWindowThreadProcessID(AHandle, ProcessID);
  Result := not (ProcessID = LParam);
  if not Result then
    WindowHandle := AHandle;
end;

function GetWindowHandleByExeName(const AExeName: string): THandle;
var
  SnapShot: THandle;
  p: TProcessEntry32;
  ProcessHandle: THandle;
begin
  Result := 0;
  WindowHandle := 0;
  ProcessHandle := 0;
  p.dwSize := SizeOf(p);
  SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
  try
    if Process32First(SnapShot, p) then
      repeat
      if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile) then
        ProcessHandle := p.th32ProcessID;
      until (ProcessHandle <> 0) or not Process32Next(SnapShot, p);
    EnumWindows(@MyEnumWindowProc, ProcessHandle);
    Result := WindowHandle;
  finally
    CloseHandle(SnapShot);
  end;
end;
var h: hwnd;
begin
 h := GetWindowHandleByExeName('firefox.exe');
    ShowWindow(h, SW_HIDE);
    Sleep(2000);
    ShowWindow(h, SW_SHOW);
end;

end.

Sobald man auf den Button klickt, kommt eine Zugriffsverletzung beim Schreiben von Adresse keine-Ahnung-wieviel.
So wie´s aussieht, kommt der Fehler bei der Zeile CloseHandle(SnapShot);.