Einzelnen Beitrag anzeigen

heidsch

Registriert seit: 20. Jan 2005
3 Beiträge
 
#1

HandleByExeName und GetWindowText bringt "Default IME&q

  Alt 29. Sep 2008, 17:13
EDIT: Bitte den 3.post lesen....




hallo leute,

ich komm hier einfach nicht weiter. ich habe eine funktion gefunden,
welche mir das handle aus dem namen der exe ermittelt.
nur leider bekomme ich dann einen AccessViolation-Fehler. sieht einer
von euch den fehler?

Delphi-Quellcode:
unit main;

interface

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

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

var
  Form1: TForm1;
  WindowHandle: THandle;
  ProcessHandle: THandle;
  h:HWnd;

implementation

{$R *.dfm}

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;
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);
    GetParent(WindowHandle);
    Result := WindowHandle;
  finally
    CloseHandle(SnapShot);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var myBuf: array[0..250] of Char;
begin
WindowHandle := GetWindowHandleByExeName('putty.exe');
GetWindowText(WindowHandle, myBuf, sizeof(myBuf));
StatusBar1.SimpleText:=myBuf;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
self.Left:=round((screen.Width/2)-(self.Width/2));
self.Top:=0;
end;

end.
  Mit Zitat antworten Zitat