News:

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

Add Text to window

Started by SpSofiane, February 11, 2012, 05:17:10 PM

Previous topic - Next topic

SpSofiane


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


dedndave

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

SpSofiane


SpSofiane

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

Gunner

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

~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

SpSofiane