flat assembler
Message board for the users of flat assembler.

Index > Windows > PostMessage sending different letter depending on window?

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Azu,

invoke SendMessage eax, WM_*,… to be exact. The problem is that most windows don't have title or unique window class. Here is my little attempt:
Code:
        include "WIN32AX.INC"

;;; Diablo player name dialog windows' hierarchy:
;;; Main window (class: "DIABLO"; title: "DIABLO")
;;;     owned popup dialog (class: "SDlgDialog"; title: "")
;;;         owned popup dialog (class: "SDlgDialog"; title: "")
;;;             child custom edit control (class: "DIABLOEDIT"; title: <player name>)

        .code
start:  invoke  FindWindow, dword [esp], "DIABLO"
; enumerate top-level Diablo windows to find second-level dialog
        invoke  EnumThreadWindows, <invoke GetWindowThreadProcessId, eax, NULL>, SetDiabloPlayerName, eax
        ret

proc SetDiabloPlayerName, hwnd, hwndMain
; check that hwnd owner's owner is Diablo's main window
        invoke  GetWindow, [hwnd], GW_OWNER
        test    eax, eax
        jz      .continue
        invoke  GetWindow, eax, GW_OWNER
        cmp     eax, [hwndMain]
        je      .found
.continue:
; not our dialog, continue enumeration
        mov     eax, TRUE
        ret
.found:
        invoke  FindWindowEx, [hwnd], NULL, "DIABLOEDIT", NULL
        mov     [hwnd], eax
        invoke  SendMessage, eax, WM_SETTEXT, 0, "bald"; WM_SETTEXT is synchronous
        invoke  PostMessage, [hwnd], WM_CHAR, ('r'), 0
        invoke  PostMessage, [hwnd], WM_KEYDOWN, VK_RETURN, 0x1C0000
        invoke  PostMessage, [hwnd], WM_KEYUP, VK_RETURN, 0xC09C0000
; stop enumeration
        xor     eax, eax
        ret
endp

        .end    start    
As you can see, it's not easy to find that sneaky child custom edit control. OllyDbg or Microsoft Spy++ could be useful.

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 16 Jan 2009, 12:08
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
baldr wrote:
The problem is that most windows don't have title or unique window class.
Really? The problem I'm coming across is that I can't figure out how to send a keystroke to a window without activating it.

Finding and putting in the name of the window isn't my problem, whoever uses my program will do that.


I am just trying to find a way to send key strokes to windows/programs (without activating them). Any ideas?? Attempting to manually reverse engineer every single possible program in existence and put custom code in them for my program like I did with the notepad example is obviously out of the question, it was just an example..
Post 16 Jan 2009, 15:37
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Azu,

Again: most windows don't have title and/or unique class name.

If you can't find window, you can't send/post something to it. Active or not. If you can, do as you wish.

keybd_event()/SendInput() simulate keystrokes, they go to the system-wide message queue, then (through the thread message queue) to the window that has focus.

Inactive windows can't have focus. This is by design.

May be hooks can help to track which window lost focus when program's main window was deactivated… I'll try to concoct something in that way.

There was at least one game that pause itself when I Alt-Tabbed from it.

BTW, I was wrong about DispatchMessage() -- it's innocent (or at least appears to be). Message given by GetMessage() is already directed to particular window (unless it's from PostThreadMessage()).

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 16 Jan 2009, 19:39
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Sorry what I meant was, there is no problem finding the name of the window. It shows up in the task bar. The problem is that although this seems to work for all games, which a lot of applications the GetDlgItem is needed, and I would have to put custom code for every application that exists, since the value for GetDlgItem is different for each one it seems.

So I am trying to ask if anybody has a clue how to get around this.

Sorry if I didn't explain that clearly enough the first dozen times. I'm bad at asking questions I guess =/
Post 17 Jan 2009, 02:31
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Azu,

Let's make it clear. There are two distinct types of windows: overlapped/popup and child. Former can be owned by another overlapped/popup window, latter should have parent overlapped/popup/child window. That is the hierarchy.

You need some language to describe owner/owned and parent/child relations between target window and [one of] top-level window[s] to find it run-time.

Alternatively, you may employ WH_CALLWNDPROC hook with SetWindowHookEx() to track which window had lost focus when process became inactive (look for WM_KILLFOCUS).

Any of that methods will give you handle to window to SendMessage() to.

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 17 Jan 2009, 20:37
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
I'm not having any problems getting the handles of the windows. I'm having problems sending keystrokes to them without activating them and without making custom code for each one.
Post 18 Jan 2009, 02:50
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Azu,

You don't have problems with getting handle to window that will get focus upon activation? I mean, when window is still inactive?
Post 18 Jan 2009, 11:43
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
baldr wrote:
Azu,

You don't have problems with getting handle to window that will get focus upon activation? I mean, when window is still inactive?


Actually I'm not even sure anymore. I'm starting to think what I'm saying is just all wrong.


Just go look at the two examples in the first post on the thread yourself okay because I think I am getting the words for this wrong and I'm confusing everyone and myself trying to explain it.

Basically without the invoke GetDlgItem,eax,0x0000000F line it doesn't work in notepad.. with it it doesn't work in WoW.. (very simple example, this kind of problem applies to pretty much all programs I try it with not just these two).
Post 19 Jan 2009, 07:54
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
Azu: In my experience with sending keystrokes I found that each program has it's own way of dealing with keyboard input. You will have to customise your code for each program you want to inject into.

When activating windows you generally only need to activate the parent and Windows (with a capital W) will then also activate the child windows that are on top of the parent. It is sometimes the case that you cannot easily distinguish between child windows by window name or class name and thus you may not be able to figure out which window is receiving the keyboard strokes. For simple programs like Notepad that never open more than one child window your task is easy, but other programs may not be so friendly. Some programs don't even run the message loop so trying to send WM_* messages into those programs will have no effect at all. Some programs run the message loop but ignore all keystrokes and use other methods to detect which keys are pressed, so again, putting keystrokes into those programs will have no effect at all. Do you see the problem?
Post 19 Jan 2009, 10:45
View user's profile Send private message Visit poster's website Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
revolution wrote:
Azu: In my experience with sending keystrokes I found that each program has it's own way of dealing with keyboard input. You will have to customise your code for each program you want to inject into.

When activating windows you generally only need to activate the parent and Windows (with a capital W) will then also activate the child windows that are on top of the parent. It is sometimes the case that you cannot easily distinguish between child windows by window name or class name and thus you may not be able to figure out which window is receiving the keyboard strokes. For simple programs like Notepad that never open more than one child window your task is easy, but other programs may not be so friendly. Some programs don't even run the message loop so trying to send WM_* messages into those programs will have no effect at all. Some programs run the message loop but ignore all keystrokes and use other methods to detect which keys are pressed, so again, putting keystrokes into those programs will have no effect at all. Do you see the problem?
Thank you. So I need to stop using PostMessage/SendMessage, and find something else to use instead, basically.
Since Windows knows which windows to activate, I think it is possible. I'm just not sure how. I don't even know which function to use now.. Confused
Post 19 Jan 2009, 20:17
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
Azu wrote:
I don't even know which function to use now.
It's not a matter of choosing the right function. It is a matter of finding out what each program uses to detect keystrokes and using the appropriate methods to simulate key presses. Note: I said use "the appropriate methods" and not use the appropriate function, the difference is that some programs require complex things to be done to get the simulation working. Sometimes using one single function will not be sufficient.
Post 19 Jan 2009, 20:26
View user's profile Send private message Visit poster's website Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
revolution wrote:
Azu wrote:
I don't even know which function to use now.
It's not a matter of choosing the right function. It is a matter of finding out what each program uses to detect keystrokes and using the appropriate methods to simulate key presses. Note: I said use "the appropriate methods" and not use the appropriate function, the difference is that some programs require complex things to be done to get the simulation working. Sometimes using one single function will not be sufficient.
Thanks. I thought it was possible since the OS can do it (the setwinactive function always gets it right somehow) and some third party macro programs do it. Guess I was wrong. Crying or Very sad



P.S. one more question.

If there is something I have to do per program to find out what method or whatever to use, why can't a function be made to do that instead of me donig it? I don't understand =/
Post 24 Jan 2009, 16:05
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17270
Location: In your JS exploiting you and your system
revolution
Azu wrote:
If there is something I have to do per program to find out what method or whatever to use, why can't a function be made to do that instead of me donig it? I don't understand =/
Yes, there is some things you need to do. You need to see how a particular program is detecting keystrokes. For many common non-game programs simply using keybd_event, or injecting WM_CHAR messages will work. But some programs don't use those functions to get keystrokes.

Windows has a few ways to indicate to programs when keys are pressed (and released). So unless your injection code covers all the possibilities then some programs may fall through the holes and not see your injected keystrokes.
Post 24 Jan 2009, 16:14
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
revolution,

He may dig deeper and use KM code to inject Wink proper bit values in system structures, but it definitely will take some time…

Azu,

I'll try to intercept WM_KILLFOCUS and present a sample later.

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 24 Jan 2009, 21:50
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.