Entwickler-Ecke

Windows API - TargetWindow wird nicht gefunden


Kri - Sa 12.04.08 14:56
Titel: TargetWindow wird nicht gefunden
Hallo zusammen,

ich habe mir diesen Quelltext genommen:


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:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
procedure PostKeyExHWND(hWindow: HWnd; key: Word; const shift: TShiftState;
  specialkey: Boolean);
{************************************************************
 * Procedure PostKeyEx
 *
 * Parameters:
 *  hWindow: target window to be send the keystroke
 *  key    : virtual keycode of the key to send. For printable
 *           keys this is simply the ANSI code (Ord(character)).
 *  shift  : state of the modifier keys. This is a set, so you
 *           can set several of these keys (shift, control, alt,
 *           mouse buttons) in tandem. The TShiftState type is
 *           declared in the Classes Unit.
 *  specialkey: normally this should be False. Set it to True to
 *           specify a key on the numeric keypad, for example.
 *           If this parameter is true, bit 24 of the lparam for
 *           the posted WM_KEY* messages will be set.
 * Description:
 *  This procedure sets up Windows key state array to correctly
 *  reflect the requested pattern of modifier keys and then posts
 *  a WM_KEYDOWN/WM_KEYUP message pair to the target window. Then
 *  Application.ProcessMessages is called to process the messages
 *  before the keyboard state is restored.
 * Error Conditions:
 *  May fail due to lack of memory for the two key state buffers.
 *  Will raise an exception in this case.
 * NOTE:
 *  Setting the keyboard state will not work across applications
 *  running in different memory spaces on Win32 unless AttachThreadInput
 *  is used to connect to the target thread first.
 *Created: 02/21/96 16:39:00 by P. Below
 ************************************************************}

type
  TBuffers = array [0..1] of TKeyboardState;
var
  pKeyBuffers: ^TBuffers;
  lParam: LongInt;
begin
  (* check if the target window exists *)
  if IsWindow(hWindow) then
  begin
    (* set local variables to default values *)
    pKeyBuffers := nil;
    lParam := MakeLong(0, MapVirtualKey(key, 0));

    (* modify lparam if special key requested *)
    if specialkey then
      lParam := lParam or $1000000;

    (* allocate space for the key state buffers *)
    New(pKeyBuffers);
    try
      (* Fill buffer 1 with current state so we can later restore it.
         Null out buffer 0 to get a "no key pressed" state. *)

      GetKeyboardState(pKeyBuffers^[1]);
      FillChar(pKeyBuffers^[0], SizeOf(TKeyboardState), 0);

      (* set the requested modifier keys to "down" state in the buffer*)
      if ssShift in shift then
        pKeyBuffers^[0][VK_SHIFT] := $80;
      if ssAlt in shift then
      begin
        (* Alt needs special treatment since a bit in lparam needs also be set *)
        pKeyBuffers^[0][VK_MENU] := $80;
        lParam := lParam or $20000000;
      end;
      if ssCtrl in shift then
        pKeyBuffers^[0][VK_CONTROL] := $80;
      if ssLeft in shift then
        pKeyBuffers^[0][VK_LBUTTON] := $80;
      if ssRight in shift then
        pKeyBuffers^[0][VK_RBUTTON] := $80;
      if ssMiddle in shift then
        pKeyBuffers^[0][VK_MBUTTON] := $80;

      (* make out new key state array the active key state map *)
      SetKeyboardState(pKeyBuffers^[0]);
      (* post the key messages *)
      if ssAlt in Shift then
      begin
        PostMessage(hWindow, WM_SYSKEYDOWN, key, lParam);
        PostMessage(hWindow, WM_SYSKEYUP, key, lParam or $C0000000);
      end
      else
      begin
        PostMessage(hWindow, WM_KEYDOWN, key, lParam);
        PostMessage(hWindow, WM_KEYUP, key, lParam or $C0000000);
      end;
      (* process the messages *)
      Application.ProcessMessages;

      (* restore the old key state map *)
      SetKeyboardState(pKeyBuffers^[1]);
    finally
      (* free the memory for the key state buffers *)
      if pKeyBuffers <> nil then
        Dispose(pKeyBuffers);
    end; { If }
  end;
end; { PostKeyEx }


und


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var
  targetWnd: HWND;
begin
  targetWnd := FindWindow('notepad', nil)
    if targetWnd <> 0 then
    begin
      PostKeyExHWND(targetWnd, Ord('A'), [], False);
  end;
end;


Dann öffne ich das Notepad, aber es passiert nicht?
Ich denke, dass mein targetwnd falsch ist.
Weiss einer Rat?

Viele liebe Grüße
Kri


Xion - Sa 12.04.08 15:07
Titel: Re: TargetWindow wird nicht gefunden
user profile iconKri hat folgendes geschrieben:


Delphi-Quelltext
1:
  targetWnd := FindWindow('notepad', nil)                    



leider heißt das Fenster scheinbar "Neu Textdokument.txt - Editor" Du musst also alle offenen Fenster durchgehen, den Namen auslesen und gucken, ob " - Editor" am ende ist


Kri - Sa 12.04.08 15:16

Das Fenster heisst "Neu Texdokument - Editor" (ohne txt);

Habe nun ausprobiert ohne Erfolg:

notepad
notepad.exe
Neu Textdokument.txt
Neu Textdokument.txt - Editor
Neu Textdokument - Editor


Xion - So 13.04.08 10:57

hmm, nicht das irgendwas andres nicht geht...hast du mal nen breakpoint nach targetWnd <>0 gemacht?


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
var
  targetWnd: HWND;
begin
  targetWnd := FindWindow('notepad', nil)
    if targetWnd <> 0 then
    begin
      PostKeyExHWND(targetWnd, Ord('A'), [], False);
    end;
end;



user profile iconKri hat folgendes geschrieben:
Das Fenster heisst "Neu Texdokument - Editor" (ohne txt);

:gruebel: bei mir ist da ein .txt


Kri - So 13.04.08 11:37

Hi,

hab sogar die einzelnen "Window-Titles" ausgelesen, bei mir ist da kein txt..
Ich habe das Programm man aus Jux in Counterstrike getestet, da funktioniert es..
Aber im Notepad nicht..
Was meinst du mit Breakpoint?

Viele liebe Grüße
Kri


Xion - So 13.04.08 12:49

user profile iconKri hat folgendes geschrieben:

Was meinst du mit Breakpoint?


wenn du vor der Zeile (wo die Nummern stehen) klickst, wird die entsprechende Zeile farblich markiert. Läuft das Programm irgendwann mal da durch, hält es an und du siehst, was so los ist => wenn du die von mir markierte Zeile als Breakpoint setzt und das Programm nie anhält, dann findet er das Fenster nicht, anders allerdings funktioniert irgendwas andres nicht.


Kri - So 13.04.08 13:26

Hi,

ok das habe ich gemacht, es gab keinen Fehler (ist so ein grüner Haken durch)..

Viele liebe Grüße
Kri


Xion - So 13.04.08 15:01

user profile iconKri hat folgendes geschrieben:

(ist so ein grüner Haken durch)


ein Bild sagt mehr als tausend Worte ^^

Anmerkung: Aus dem ganzen habe ich gesehen, dass er das Fenster findet (als 'notepad'). Es muss also was an deiner Procdure PostKeyExHWND nicht stimmen.

//Edit: ich vermute mal, du musst den Wert an das Textfeld schicken, nicht an Notepad selbst


Martok - So 13.04.08 15:14

Hm nachdem ich das jetzt 2 Tage beobachtet habe, verrate ich euch doch mal die Lösung :P Nein Spaß, ich hab mir das auch eben erst richtig angesehen.

Die Fensterliste vom Simon Reinhard ist wirklich praktisch. Und zwar ist das Eingabefeld im Notepad das Control, an das die Nachricht gehen müsste. Also:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
  targetWnd := FindWindow('Notepad', nil);
  if targetWnd <> 0 then
    begin
      targetWnd:= GetWindow(targetWnd,GW_CHILD);
      PostKeyExHWND(targetWnd, Ord('A'), [], False);
  end;

Im Findwindow das war übrigens richtig: der erste Parameter ist die Fensterklasse, und die ist tatsächlich 'Notepad'. Davon hole ich mir das erste Unterfenster. Nachricht hin, und fertig ;)


Kri - So 13.04.08 20:15

Hi,

oh Wunder es geht endlich.

Vielen Dank an dieser Stelle!

Viele liebe Grüße
Kri