Enumerating top level windows

Started by Vortex, December 02, 2025, 10:00:00 PM

Previous topic - Next topic

Vortex

Here is a quick example :

include     EnumWnd.inc

.data

f1          db '%s',13,10,0

.data?

buffer      db 128 dup(?)
buffer2     db 128 dup(?)

.code

start:

    invoke  EnumWindows,ADDR EnumWndProc,0
    invoke  ExitProcess,0
   

EnumWndProc PROC hwnd:DWORD,lParam:DWORD

    invoke  GetWindowText,hwnd,ADDR buffer,64
    invoke  wsprintf,ADDR buffer2,ADDR f1,ADDR buffer
    invoke  StdOut,ADDR buffer2
    mov     eax,1
    ret

EnumWndProc ENDP

END start
Code it... That's all...

TimoVJL

Empty lines should avoid too.
Also wsprintf give count of chrs to print text, so it can be used too.
WriteFile / WriteConsole works well with wsprintf.
May the source be with you

Vortex

Hi Timo,

Thanks, I will take care of it.
Code it... That's all...

Vortex

Hi Timo,

Here is the new version skipping the blank lines :
include     EnumWnd.inc

.data

f1          db '%s',13,10,0

.data?

buffer      db 128 dup(?)
buffer2     db 128 dup(?)

.code

start:

    invoke  EnumWindows,ADDR EnumWndProc,0
    invoke  ExitProcess,0
   

EnumWndProc PROC hwnd:DWORD,lParam:DWORD

    invoke  GetWindowText,hwnd,ADDR buffer,64
    invoke  wsprintf,ADDR buffer2,ADDR f1,ADDR buffer
    cmp     eax,2
    je      @f
    invoke  StdOut,ADDR buffer2
@@:
    mov     eax,1
    ret

EnumWndProc ENDP

END start
Code it... That's all...

TimoVJL

I was after something like thisinclude     EnumWnd.inc
includelib user32.lib

STD_OUTPUT_HANDLE equ -11

GetStdHandle PROTO :DWORD
WriteFile PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD

.data

f1          db '%s',13,10,0

.data?

hStdOut dd ?
buffer      db 128 dup(?)
buffer2     db 128 dup(?)

.code

start:
    invoke  GetStdHandle, STD_OUTPUT_HANDLE
    mov     hStdOut, eax
    invoke  EnumWindows,ADDR EnumWndProc,0
    invoke  ExitProcess,0
   

EnumWndProc PROC hwnd:DWORD,lParam:DWORD
    invoke  GetWindowText,hwnd,ADDR buffer,64
    invoke  wsprintf,ADDR buffer2,ADDR f1,ADDR buffer
    cmp     eax,2
    je      @f
invoke  WriteFile, hStdOut, ADDR buffer2, eax, 0, 0
@@:
    mov     eax,1
    ret

EnumWndProc ENDP

END start
May the source be with you

Vortex

Hi Timo,

Here is the new version eliminating the extra branching :

include     EnumWnd.inc

.data

f1          db '%s',13,10,0
ZeroVal     dd 0
ZeroPtr     dd OFFSET ZeroVal

.data?

hStdOut dd ?
buffer      db 128 dup(?)
buffer2     db 128 dup(?)

.code

start:

    invoke  GetStdHandle, STD_OUTPUT_HANDLE
    mov     hStdOut,eax
    invoke  EnumWindows,ADDR EnumWndProc,0
    invoke  ExitProcess,0
   

EnumWndProc PROC USES ebx hwnd:DWORD,lParam:DWORD

    mov     ebx,OFFSET buffer2
    invoke  GetWindowText,hwnd,ADDR buffer,64
    invoke  wsprintf,ebx,ADDR f1,ADDR buffer
    cmp     eax,2
    cmovz   ebx,DWORD PTR [ZeroPtr]
    invoke  WriteFile,hStdOut,ebx,eax,0,0
    mov     eax,1
    ret

EnumWndProc ENDP

END start
Code it... That's all...

TimoVJL

More fun, eliminate wsprintf and buffer2
    invoke  GetWindowText,hwnd,ADDR buffer,64
    cmp     eax,0
    je      @f
    mov     WORD PTR[buffer+eax], 0A0Dh
    add     eax, 2
    invoke  WriteFile, hStdOut, ADDR buffer, eax, 0, 0
@@:
May the source be with you

John Z

A fun fact - the maximum 'displayable' text in the title bar is 255 characters so while that length would be unusual just 64 characters might only be 1/4th of the text.  Also the text can be much longer than 255 but only 255 are displayed. ;D


John Z

Vortex

Reading Timo's and John's messages, here is the new version :

include    EnumWnd.inc

.data

ZeroVal    dd 0
ZeroPtr    dd OFFSET ZeroVal
msg        db '%s',13,10,0

.data?

buffer      db 256 dup(?)
buffer2    db 256 dup(?)

.code

start:

    invoke  EnumWindows,ADDR EnumWndProc,0
    invoke  ExitProcess,0

EnumWndProc PROC hwnd:DWORD,lParam:DWORD

    invoke  GetWindowText,hwnd,ADDR buffer,255
    mov    edx,OFFSET msg
    test    eax,eax
    cmovz  edx,DWORD PTR [ZeroPtr]
    invoke  printf,edx,ADDR buffer
    mov    eax,1
    ret

EnumWndProc ENDP

END start
Code it... That's all...