The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: SpSofiane on February 11, 2012, 05:17:10 PM

Title: Add Text to window
Post by: SpSofiane on February 11, 2012, 05:17:10 PM

AddDlgItemText Proc hWin:DWORD,idControl:DWORD,lpsz:DWORD
   LOCAL lpszText:DWORD
   LOCAL TextLenght:DWORD
   invoke SendDlgItemMessage,hWin,idControl,WM_GETTEXTLENGTH,0,0
   add eax,4          ;+4 for 0h
   mov TextLenght,eax
   invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,TextLenght
   mov lpszText,eax
   invoke GetDlgItemText,hWin,idControl,lpszText,TextLenght
   invoke lstrcat,lpszText,lpsz
   Invoke SetDlgItemText,hWin,idControl,eax
   invoke GlobalFree,lpszText
   xor eax,eax
   ret
AddDlgItemText endp

error : memory canot be read after 3 uses

Title: Re: Add Text to window
Post by: dedndave on February 11, 2012, 05:54:41 PM
i don't see where the length of the string parm "lpsz" is added to the allocated buffer size
you add 4, but not the legth of lpsz
if you are adding one character at a time, that might explain why it fails on number 3   :P
Title: Re: Add Text to window
Post by: SpSofiane on February 11, 2012, 06:48:48 PM
ok thank you :U :thumbu
Title: Re: Add Text to window
Post by: SpSofiane on February 11, 2012, 11:54:43 PM
i corrected it & it work 100%
:dance:


AddDlgItemText Proc hWin:DWORD,idControl:DWORD,lpsz:DWORD
   LOCAL lpszText:DWORD
   LOCAL TextLenght:DWORD
   invoke SendDlgItemMessage,hWin,idControl,WM_GETTEXTLENGTH,0,0
   inc eax         ;+1 for 0h  <=== is it necessary ?
   mov TextLenght,eax
   invoke lstrlen,lpsz
   add TextLenght,eax
   invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,TextLenght
   mov lpszText,eax
   invoke GetDlgItemText,hWin,idControl,lpszText,TextLenght
   invoke lstrcat,lpszText,lpsz
   Invoke SetDlgItemText,hWin,idControl,eax
   invoke GlobalFree,lpszText
   xor eax,eax
   ret
AddDlgItemText endp
Title: Re: Add Text to window
Post by: Gunner on February 12, 2012, 12:24:38 AM
Is it necessary?  Well that is up to you.  WM_GETTEXTLENGTH returns the length of the text NOT including the NULL terminator.  Since you are creating a buffer to hold a zero terminated string, then yes, the inc eax is necessary.

We could get rid of the TextLength buffer by doing this:
invoke  SendDlgItemMessage,hWin,idControl,WM_GETTEXTLENGTH,0,0
inc     eax         ;+1 for 0h  <=== is it necessary ?
push    eax     ;mov TextLenght,eax
invoke  lstrlen, lpsz
pop     edx
add     eax, edx ;add TextLenght,eax
invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,eax

Title: Re: Add Text to window
Post by: SpSofiane on February 12, 2012, 01:05:42 PM
thank you  Gunner :bg