News:

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

Do we have WinGetClass in MASM ?

Started by hfheatherfox07, April 11, 2012, 10:52:24 PM

Previous topic - Next topic

dedndave

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633494%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633493%28v=vs.85%29.aspx

EnumChildProc PROTO :HWND,LPARAM
;
;this makes it happen, captain
;
        INVOKE  EnumChildWindows,HWND_DESKTOP,EnumChildProc,NULL
;
;replace the NULL above if you like
;you can pass a value to the EnumChildProc through lParam
;
;
;
EnumChildProc PROC hwnd:HWND,lParam:LPARAM

;the OS calls this routine once with the hwnd of each window
;that is a child of the desktop (i.e., all windows)

;if you exit with EAX = 1, enumeration continues until all child windows are done
;if you exit with EAX = 0, enumeration stops

        ret

EnumChildProc ENDP


if you want top-level windows only, use EnumWindows instead of EnumChildWindows - very similar

        INVOKE  EnumWindows,EnumProc,NULL

hfheatherfox07

and how do I get the class name to display in a textbox or an edit widow doing this?

dedndave

you can do whatever testing or filtering you like inside the callback proc
for example, i might only be interested in butttons
so - i might do that by getting the classname for each hwnd and doing a case-insensitive compare against "button",0
(actually easier by looking at the style bits)

try this code...
        INCLUDE \masm32\include\masm32rt.inc

EnumChildProc PROTO :HWND,:LPARAM

;-----------------------------------------------------

        .CODE

;let's make it happen, captain

start:  INVOKE  EnumChildWindows,HWND_DESKTOP,EnumChildProc,NULL
        INVOKE  ExitProcess,0

;-----------------------------------------------------

EnumChildProc PROC hwnd:HWND,lParam:LPARAM

        LOCAL   szBuf[256]:BYTE

        INVOKE  GetClassName,hwnd,addr szBuf,sizeof szBuf
        INVOKE  MessageBox,0,addr szBuf,0,0
        ret

EnumChildProc ENDP

;-----------------------------------------------------

        END     start

hfheatherfox07

Thank you I will give it a shot Tonight

dedndave

this version will be more fun   :P

       INCLUDE \masm32\include\masm32rt.inc

EnumChildProc PROTO :HWND,:LPARAM

;-----------------------------------------------------

       .CODE

;let's make it happen, captain

start:  INVOKE  EnumChildWindows,HWND_DESKTOP,EnumChildProc,NULL
        INVOKE  ExitProcess,0

;-----------------------------------------------------

EnumChildProc PROC hwnd:HWND,lParam:LPARAM

        LOCAL   szBuf1[256]:BYTE
        LOCAL   szBuf2[256]:BYTE

        INVOKE  GetWindowText,hwnd,addr szBuf1,sizeof szBuf1
        INVOKE  GetClassName,hwnd,addr szBuf2,sizeof szBuf2
        lea     edx,szBuf1
        INVOKE  MessageBox,0,addr szBuf2,edx,MB_OKCANCEL
        and     eax,1
        ret

EnumChildProc ENDP

;-----------------------------------------------------

       END     start


notice that when you press "Ok" on MessageBox, it exits with EAX = 1
when you press "Cancel", it exits with EAX = 2
we AND that result with 1
so, we get either 0 or 1

hutch--

If you know the class name OR the text on the title bar whats wrong with using,


HWND FindWindow(
    LPCTSTR lpClassName, // pointer to class name
    LPCTSTR lpWindowName // pointer to window name
   );
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

hfheatherfox07

 :U
Quote from: hutch-- on April 12, 2012, 04:27:09 AM
If you know the class name OR the text on the title bar whats wrong with using,


HWND FindWindow(
    LPCTSTR lpClassName, // pointer to class name
    LPCTSTR lpWindowName // pointer to window name
   );


That what this was all about ... finding the "lpClassName" and "lpWindowName"  of a window so I can use "FindWindow".....

by the way it worked great dedndave    :U  I noticed that windows that did not have an assigned lpClassName by default I got "#32770" as the lpClassName Wierd?

dedndave

that is the pre-defined system class for dialog boxes
or - i should say, "one of the" - there are several, depending on the type of dialog
ms finally ran out of words - lol - so they gave it a number   :P

hfheatherfox07

Quote from: dedndave on April 12, 2012, 04:40:45 PM
that is the pre-defined system class for dialog boxes

Oh....

I am assuming ( I did not have time with this yet ) that If I want a specific window's info  for example "targetwindow.exe"

that instead of INVOKE  EnumChildWindows,HWND_DESKTOP,EnumChildProc,NULL

I would use

.data
appName  db "targetwindow.exe"

INVOKE  EnumChildWindows,addr appName,EnumChildProc,NULL


In any case I love the way you did this dedndave

hutch--

There is a toy in the masm32 examples that enumerates running applications. This method gives you the classname if it is in fact unique.

Have a look at \masm32\examples\enumerate\...........

There are a few techniques for enumerating different things.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

"targetwindow.exe" is not going to work, unless it happens to be the caption
that is a process name - more accurately, a program file name

Hutch...
that little tool that Iczelion has in tutorial #24 looks like fun, too   :P
http://www.masm32.com/board/index.php?topic=18679.msg157788#msg157788

i generally use ms Spy++ for such things

hfheatherfox07

Quote from: dedndave on April 12, 2012, 04:53:28 PM
"targetwindow.exe" is not going to work, unless it happens to be the caption
that is a process name - more accurately, a program file name

Hutch...
that little tool that Iczelion has in tutorial #24 looks like fun, too   :P
http://www.masm32.com/board/index.php?topic=18679.msg157788#msg157788

i generally use ms Spy++ for such things

that is what I meant  , a program file name like notepad.exe for example .....
I have a few examples of ways to hook into programs ...but then you would get the handle of the window and would be no scene in using "Findwindow"

dedndave

i am guessing you are only interested in top-level windows
no need to go through all the child windows

so - you'd use EnumWindows
for each hwnd, call GetWindowThreadProcessId
that will get you the PID
call OpenProcess with the SYNCHRONIZE access flag
that will get you a handle to the process
call GetModuleFileNameEx to get the filename
use CloseHandle to close the process handle when you are done

dedndave

ok - i had to use GetProcessImageFileName instead of GetModuleFileNameEx
not sure why that is - the other should work, too

oh well - give this a play...