flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem(error) with subclassing.

Author
Thread Post new topic Reply to topic
programmer



Joined: 05 Jun 2006
Posts: 6
programmer 05 Jun 2006, 14:18
I have subclassed an edit box. After I use GetWindowText function and move mouse over the subclassed edit box, I got an error "The memory could not be read!". Where a problem is?
Code:
format PE GUI 4.0
entry start

include 'win32ax.inc'

section '.data' data readable writeable
        
        wnd_class db 'pro_wnd',0
        
        wc WNDCLASS 0,WndProc,0,0,0,0,0,COLOR_BTNFACE+1,0,wnd_class
        msg MSG
        
        text db ?
        DefEditProc dd ?
        hButton dd ?
        hEdit dd ?
        
section '.code' code readable executable
        start:
                invoke GetModuleHandle,0
                mov [wc.hInstance],eax
                invoke LoadIcon,0,IDI_APPLICATION
                mov [wc.hIcon],eax
                invoke LoadCursor,0,IDC_ARROW
                mov [wc.hCursor],eax
                invoke RegisterClass,wc
                
                invoke CreateWindowEx,0,wnd_class,"Window",WS_VISIBLE + WS_DLGFRAME + WS_SYSMENU,100,100,300,200,0,0,[wc.hInstance],0
                
        message_loop:
                invoke GetMessage,msg,NULL,0,0
                or eax,eax
                jz exit
                invoke TranslateMessage,msg
                invoke DispatchMessage,msg
                jmp message_loop
                
        exit:
                invoke ExitProcess,[msg.wParam]
        
        proc WndProc hwnd,wmsg,wparam,lparam
                cmp [wmsg],WM_CREATE
                je wm_create
                cmp [wmsg],WM_COMMAND
                je wm_command
                cmp     [wmsg],WM_DESTROY
                je      wm_destroy
                jmp def_wndproc
                
                wm_command:
                        cmp [wparam],BN_CLICKED shl 16 + 1
                        je button_click
                        ret
                                        
                button_click:
                        invoke GetWindowTextLength,[hEdit]
                        inc eax
                        invoke GetWindowText,[hEdit],text,eax
                        invoke MessageBox,[hwnd],text,"",MB_OK
                        jmp def_wndproc
                        
                wm_create:
                        invoke CreateWindowEx,0,"BUTTON","Button",WS_CHILD + WS_VISIBLE,110,90,70,23,[hwnd],1,0,0
                        mov [hButton],eax
                        
                        invoke CreateWindowEx,0,"EDIT","text",WS_CHILD + WS_VISIBLE + WS_BORDER + ES_AUTOHSCROLL,110,60,70,22,[hwnd],0,0,0
                        mov [hEdit],eax
                                                
                        invoke SetWindowLong,[hEdit],GWL_WNDPROC,EditProc
                        mov [DefEditProc],eax
                        
                        jmp def_wndproc
                        
                wm_destroy:
                        invoke PostQuitMessage,0
                        ret
                        
                def_wndproc:
                        invoke DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
                        ret
        endp
        
        proc EditProc hwnd,wmsg,wparam,lparam
                cmp [wmsg],WM_CHAR
                je wm_char
                jmp def_editproc
                
                wm_char:
                        cmp [wparam],VK_BACK
                        jne def_editproc
                        ret
                
                def_editproc:
                        invoke CallWindowProc,[DefEditProc],[hwnd],[wmsg],[wparam],[lparam]
                        ret
        endp
        
section '.idata' import data readable writable

        library kernel,'kernel32.dll', \
                        user,'user32.dll'
        
        import kernel, GetModuleHandle, 'GetModuleHandleA', \
                                ExitProcess, 'ExitProcess'
        
        import user, MessageBox,'MessageBoxA', \
                                SetWindowText,'SetWindowTextA', \
                                GetWindowText,'GetWindowTextA', \
                                GetWindowTextLength,'GetWindowTextLengthA', \
                                SetWindowLong,'SetWindowLongA', \
                                CallWindowProc,'CallWindowProcA', \
                                DefWindowProc,'DefWindowProcA', \
                                LoadIcon,'LoadIconA', \
                                LoadCursor,'LoadCursorA', \
                                RegisterClass,'RegisterClassA', \
                                GetMessage,'GetMessageA', \
                                TranslateMessage,'TranslateMessage', \
                                DispatchMessage,'DispatchMessageA', \
                                CreateWindowEx,'CreateWindowExA', \
                                PostQuitMessage,'PostQuitMessage'
    

Please help me Rolling Eyes
Post 05 Jun 2006, 14:18
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 05 Jun 2006, 14:54
it is buffer overflow.
you initialized only one byte for text:

text db ?

when you get text from edit control your DefEditProc is overwritten.
Make text buffer long enough to contain text, for example

text db 256 dup(?)
Post 05 Jun 2006, 14:54
View user's profile Send private message Reply with quote
programmer



Joined: 05 Jun 2006
Posts: 6
programmer 05 Jun 2006, 15:09
It works! Thanks! Smile
Post 05 Jun 2006, 15:09
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.