flat assembler
Message board for the users of flat assembler.

Index > Windows > EnumWindows not working

Author
Thread Post new topic Reply to topic
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 14 Jun 2011, 22:09
hello

I am using EnumWindows to try and get the titles of all top level windows like this:

Code:
        invoke  EnumWindows, EnumWindowsProc, 0
.
.
.
.
proc EnumWindowsProc, hWnd, lParam
        invoke  GetWindowText, [hWnd], windowTitle, 128
        or      eax, eax
        jnz     @f
        stdcall showLastError
    @@:
        mov     eax, TRUE
        ret
endp

windowTitle    db 128 dup ?
    


the EnumWindows goes through this proc, [hWnd] is always non-zero, eax is always 0 and GetLastError says the operation completed successfully¿

What am I missing?

thx for any help

Question
Post 14 Jun 2011, 22:09
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 14 Jun 2011, 22:57
Q: Are you using unicode?

Non-Unicode:
Code:

format PE GUI 4.0
entry start

include 'win32a.inc'

section '.text' code readable executable

  start:

        invoke  EnumWindows,EnumWindowsProc,0
        invoke  MessageBox,HWND_DESKTOP,_sb,_ct,MB_ICONINFORMATION or MB_OK
        invoke  ExitProcess,0

proc EnumWindowsProc, hWnd, lParam
        invoke  GetWindowText,[hWnd],_wt,128
        test    eax,eax
        jz     @f
        invoke  lstrcat,_sb,_wt
        invoke  lstrcat,_sb,_nl
    @@: mov     eax,TRUE
        ret 
endp

section '.data' data readable writeable

  _ct db "xxx",0
  _nl db 13,10,0
  _sb rb 4096 ;careful!!!
  _wt rb 128

section '.idata' import data readable writeable

  library kernel32,'KERNEL32.DLL',\
          user32,'USER32.DLL'

  include 'api\kernel32.inc'
  include 'api\user32.inc'
    
Post 14 Jun 2011, 22:57
View user's profile Send private message Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 14 Jun 2011, 23:56
thx for the reply.
don't know if I'm using unicode or not? Embarassed

Anyway, it appears I misunderstood exactly what EnumWindows returns.

Quote:
The EnumWindows function enumerates all top-level windows on the screen by passing the handle of each window, in turn, to an application-defined callback function.


I assumed this meant that if I had 8 items on the taskbar, it would return 8 handles. I wrote a little extra code to write all the handles and window titles to a file. With 8 apps on taskbar, I got 99 handles ¿¿¿
A lot of them had no titles (this is why I thought I was doing something wrong). I also got these (amongst others)

Quote:

00010058 Start
0004008C Start Menu
00010396 Graphics, Windowing and Events (GWES)
00020038 CiceroUIWndFrame
00530584 D3D9Window
001C0750 D3D9Window
00040424 D3D9Window
001702AA D3D9Window
002E051A D3D9Window


strange but, at least the real open windows were there with titles so I can continue with what I was doing Very Happy

thx again
Post 14 Jun 2011, 23:56
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 15 Jun 2011, 00:17
Unicode uses multibyte characters for languages with many chars.
In english we use ASCII characters, in other countries, sometimes not.
To make a program unicode compatible we can include "win32w.inc"
and use TCHAR data types instead of regular bytes with db
See FASM example EXAMPLES/TEMPLATE/TEMPLATE.ASM for
a basic Windows app which is unicode compatible.
Have Fun Smile
Post 15 Jun 2011, 00:17
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1638
Location: Toronto, Canada
AsmGuru62 15 Jun 2011, 00:38
You got all tasks and services enumerated, probably. When you get an HWND into your callback - check if it has an extended style of WS_EX_APPWINDOW - that will be the taskbar item most likely.
Post 15 Jun 2011, 00:38
View user's profile Send private message Send e-mail Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 15 Jun 2011, 08:38
thx bitshifter, no, I'm not using unicode then Smile

AsmGuru62: Thought your idea sounded promising but, unfortunately, it doesn't hold true. Very few apps set the WS_EX_APPWINDOW style, just search the boards for createwindowex to see how many have the ext style as 0.
It appears the only way is to cycle through the titles to find what I'm looking for.

thx again
Post 15 Jun 2011, 08:38
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 15 Jun 2011, 17:11
If oyu looking for all "normal" windows, I think the WS_VISIBLE style flag should be ok for this (and possibly WS_MINIMISED ?).

If you're looking for a specific window, it might be better to use FindWindow(Ex).
Post 15 Jun 2011, 17:11
View user's profile Send private message Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 15 Jun 2011, 19:37
thx cod, I'll check out those flags. FindWindow would be no use for my purpose as I would only know part of the windows name (title).

Just as a note for anyone using the EnumWindows function, make sure your EnumWindowsProc returns with edi unchanged or thou shalt crash 'n' burn.

update
preserve all registers except eax or EnumWindows skips some of the open windows.
Post 15 Jun 2011, 19:37
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 15 Jun 2011, 22:08
You only need to preserve ESI, EDI, EBX like any other Win32 proc should.
Post 15 Jun 2011, 22:08
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 15 Jun 2011, 22:23
Quote:
You only need to preserve ESI, EDI, EBX like any other Win32 proc should.

Also EBP (and ESP of course)
Post 15 Jun 2011, 22:23
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 15 Jun 2011, 22:26
Sounds fair enough bitshifter Very Happy

I assumed that EnumWindowsProc, being my own procedure could do as the info on it says
Quote:
Remarks

The callback function can perform any desired task.

and that windows would look after itself.
Apparently not so for a callback proc Sad

Oh well, I suppose a couple of extra lines of code to look after Bill will do no harm Laughing
Post 15 Jun 2011, 22:26
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1638
Location: Toronto, Canada
AsmGuru62 16 Jun 2011, 10:57
Any callback function must preserve ESI,EDI,EBX,EBP and DF must be 0. If your code uses STD - DF must be restored with CLD. Callback is any procedure address passed to Windows to be called back (like a structure member or as any function parameter).
Post 16 Jun 2011, 10:57
View user's profile Send private message Send e-mail Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 16 Jun 2011, 18:18
thx guru, vid & bit for the clarification Very Happy

So if I use this template for all my procs I shouldn't go far wrong Smile

Code:
proc whateverProc
            locals

            endl

        push    ebx edi esi
        .
        .
        ; whatever code
        .
        .
    .finished:
        pop     esi edi ebx
        cld
        ret
endp

    
Post 16 Jun 2011, 18:18
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 796
Location: Massachusetts, USA
bitshifter 16 Jun 2011, 18:50
Keep the template in your head and only use the parts
that are needed for each implementation thereof.
No sense in useless instructions laying around, right.
Post 16 Jun 2011, 18:50
View user's profile Send private message Reply with quote
asmMe



Joined: 14 Jun 2011
Posts: 18
asmMe 16 Jun 2011, 19:58
thx bit Smile
Post 16 Jun 2011, 19:58
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.