The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: xandaz on December 19, 2010, 02:19:56 AM

Title: EnumWindows with window handles dump gives errors!!!
Post by: xandaz on December 19, 2010, 02:19:56 AM
    Hey guys.... why does this return error? check it out...
.data
WindowHandles dd 50000 dup(0) ; huge number to prevent overflow
lpWindowHandles dd offset WindowHandles
.code
....
invoke EnumWindows,addr EnumProc,NULL
....
EnumProc PROC hWnd:DWORD,Arg:DWORD

mov edi,lpWindowHandles
mov eax,hWnd
stosd
cld
invoke GetWindowText,eax,addr WindowName
invoke SendMessage,hListbox,LB_ADDSTRING,NULL,addr WindowName
mov lpWindowHandles,edi
mov eax,TRUE
ret

EnumProc endp


Why is this not working? Someone ?  :lol
Thanks and bye
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: Gunner on December 19, 2010, 02:35:19 AM
What kind of errors?  You are NOT preserving edi!
EnumProc PROC uses edi hWnd:DWORD,Arg:DWORD
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: dedndave on December 19, 2010, 11:11:11 AM
Rob is right
but, i noticed something that was a little odd
stosd
cld


you shouldn't have to CLD - it should already be cleared
and, if you do have to CLD, you might want to do it before the STOSD  :P
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: xandaz on December 19, 2010, 11:52:34 AM
 yes ded. i kinda made that mistake but only here. But why do i need to preserve edi? when i do the mov edi,lpWindowHandles and lpWindowHandles,edi i don't think there's any danger. is there? let me try that out.
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: oex on December 19, 2010, 12:21:59 PM


Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: xandaz on December 19, 2010, 12:22:37 PM
   its working fine now. thanks
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: xandaz on December 19, 2010, 01:06:06 PM
   works fine with pushes, but, still... why do i need to preserce edi? I load it in the beggining of EnumProc and deload it in the end. What difference does it make to push and pop? Someone pls clarify me. Thanks
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: MichaelW on December 19, 2010, 01:29:15 PM
You need to preserve EDI because EnumProc is a callback. Since Windows is calling it, it needs to follow the register-preservation conventions.
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: Gunner on December 19, 2010, 02:33:08 PM
re read my post above....  you don't need to uses push/pop...  instead put USES EDI after the word proc... easier then using push reg and remembering to pop it before leaving the proc
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: xandaz on December 19, 2010, 03:45:25 PM
    Oh ok. Since windows is calling the callback function and it may be useing edi, you preserve it to prevent errors. Thanks. That was kinda hard to understand. I'm such a dumbo. Thanks guys and byebye. :)
Title: Re: EnumWindows with window handles dump gives errors!!!
Post by: Gunner on December 19, 2010, 04:13:05 PM
Well, anytime you use esi, edi ebx in a proc you must save their contents, especially in a callback procedure... it is proper programming etiquette  :bg

open up the help file masm32/help/asmintro.chm and read the section called Register Preservation Convention