News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Finding window handle.

Started by realcr, December 24, 2004, 04:35:08 PM

Previous topic - Next topic

realcr

How can I find a window's handle by its title?

bar.

Ramon Sala

Hi realcr,

Use API function 'FindWindow'. Here is how it works:


The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows.

HWND FindWindow( LPCTSTR lpClassName,
// pointer to class name

LPCTSTR lpWindowName
// pointer to window name

);
 


Parameters
lpClassName

Points to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to the GlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpClassName; the high-order word must be zero.

lpWindowName

Points to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.

Return Values
If the function succeeds, the return value is the handle to the window that has the specified class name and window name.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.


Regards,

Ramon
Greetings from Catalonia

Vortex

Hi realcr,

Here is an autotyping demo using FindWindow:
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
msg         db 'Hello my friend!',13,10
            db 'This is an autotyping example. :)',0
app         db 'notepad.exe',0
wndclass    db 'Notepad',0
childclass  db 'Edit',0

.data?
handle      dd ?

.code
start:

    invoke  WinExec,ADDR app,SW_SHOW
    invoke  FindWindow,ADDR wndclass,0
    invoke  FindWindowEx,eax,0,ADDR childclass,0
    mov     handle,eax
    mov     edx,OFFSET msg
@@:
    xor     eax,eax
    mov     al,byte PTR [edx]
    or      al,al
    jz      @f
    push    edx
    invoke  SendMessage,handle,WM_CHAR,eax,0
    invoke  Sleep,200
    pop     edx
    inc edx
    jmp @b
@@:
    invoke  ExitProcess,0

END start

realcr

Tnx for your help Ramon and Sala... I finally got it working.
I also heard there is a way to get a window's handle using the mouse location or something similar. Can you tell me the name of the function which does that? I can information about it myself after I know the name...

thank you ,
bar.


Ramon Sala

Hi realcr,

As John said, the API function is WindowFromPoint. Here is how it works:

The WindowFromPoint function retrieves the handle of the window that contains the specified point.

HWND WindowFromPoint( POINT Point
// structure with point

);
 

Parameters
Point

Specifies a POINT structure that defines the point to be checked.

Return Values
If the function succeeds, the return value is the handle of the window that contains the point. If no window exists at the given point, the return value is NULL.



Regards,

Ramon
Greetings from Catalonia

realcr


Robert Collins

In the below snippit of the above sample posted by Vortex how does the @@: work and how does the assembler know that jz @f is the instruction at @@: invoke ExitProcess and that jmp  @b is at @@: xor eax,eax?


  '
  '
  '
  '
.code
start:

    invoke  WinExec, ADDR app, SW_SHOW
    invoke  FindWindow, ADDR wndclass, 0
    invoke  FindWindowEx, eax, 0, ADDR childclass, 0
    mov     handle, eax
    mov     edx, OFFSET msg
@@:
    xor     eax, eax <------------------------------------------+
    mov     al, byte PTR [edx]                                  |
    or      al, al                                              |
    jz      @f     -------------------------------------+       |
    push    edx                                         |       |
    invoke  SendMessage, handle, WM_CHAR, eax, 0        |       |
    invoke  Sleep, 100 ;200                             |       |
    pop     edx                                         |       |
    inc     edx                                         |       |
    jmp     @b -------------------------------------------------+
@@:                                                     |
    invoke  ExitProcess, 0 <----------------------------+

end start
   

Jimg

Robert-

Don't you have the masm32.hlp file?  It comes with Masm32.  Directly from this help file-

QuoteLocal Code Labels

  Syntax:   [instruction] @F
                .
                .
                .
            @@: [statement]
                .
                .
                .
                [instruction] @B

  Description:

     The @@: label defines a local code label, which is in effect until
     the next instance of @@:. The @F and @B operands can be used in
     conditional and unconditional jump statements to jump to the next

     and previous @@: label respectively.

     The @@: label is useful for defining a nearby jump point where a
     full label is not appropriate.

  Example:

           cmp     ax, 18h
           jg      @F
           -                  ;Less than or equal
           -
           -
     @@:                      ;Greater than


           mov     cx, 640    ;Set count

     @@:
           -                  ;Loop statements
           -
           -

           loop    @B         ;Loop back
                                    -o-