News:

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

CHILD WINDOW PROBLEM

Started by win_32_found, May 03, 2011, 02:27:58 PM

Previous topic - Next topic

win_32_found

I AM TRYING BASIC GUI STUFF in masm32....i tried to make a  EditBox (child window)...my code is assembled  without any errors  when i link it and try to run  then it just exits ..

Here is code

.386
.model flat ,stdcall
option casemap :none


include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc


includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib


WinMain proto :DWORD,:DWORD,:DWORD,:DWORD



.data
ClassName db "SimpleWinClass",0
AppName  db "Our First Window",0
MenuName db "FirstMenu",0   

open db " SIMPLE I/O FILE OPEN ",0
save db " SIMPLE SAVE",0
EXIT db "SIMPLE EXIT FROM FIRST TAB ",0

openm db "FIle will be Mapped",0
savem db "FIle will be unmapped ",0
exits db "File will UNMAPPED AND EXIT",0

EditClass db "edit",0


.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hwndEdit HWND ?
buffer db 512 dup(?)

.const
IDM_OPEN equ 1
IDM_SAVE equ 2
IDM_EXIT equ 3

IDM_OPENM equ 4
IDM_SAVEM equ 5
IDM_EXITM equ 6

EditID equ 11

.code

start :

    invoke GetModuleHandle, NULL
    mov    hInstance,eax
    invoke GetCommandLine
    mov CommandLine,eax
    invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess,eax


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG
    LOCAL hwnd:HWND
    mov   wc.cbSize,SIZEOF WNDCLASSEX
    mov   wc.style, CS_HREDRAW or CS_VREDRAW
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra,NULL
    mov   wc.cbWndExtra,NULL
    push  hInst
    pop   wc.hInstance
    mov   wc.hbrBackground,COLOR_WINDOW+1
    mov   wc.lpszMenuName,OFFSET MenuName       
    mov   wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov   wc.hIcon,eax
    mov   wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov   wc.hCursor,eax
    invoke RegisterClassEx, addr wc


    invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,600,NULL,NULL,hInst,NULL
   
mov   hwnd,eax
    invoke ShowWindow, hwnd,SW_SHOWNORMAL
    invoke UpdateWindow, hwnd
    .WHILE TRUE
                invoke GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
                invoke DispatchMessage, ADDR msg
    .ENDW   
   
mov     eax,msg.wParam
    ret
WinMain endp


WndProc proc hWnd:HWND,uMSG:UINT,wParam:WPARAM,lParam:LPARAM


      .IF uMSG==WM_DESTROY
      invoke PostQuitMessage,NULL

       .ELSEIF uMSG==WM_CREATE

                invoke CreateWindowEx,NULL,addr EditClass,NULL,WS_CHILD or WS_VISIBLE ,50,35,200,25,hWnd,EditID,hInstance,NULL
                mov hwndEdit,eax
                invoke SetFocus,hwndEdit


       .ELSEIF uMSG==WM_COMMAND
          mov eax,wParam
  .IF ax==IDM_OPEN
      invoke MessageBox,NULL,addr open ,OFFSET AppName,MB_OK
.ELSEIF ax==IDM_SAVE
   invoke MessageBox ,NULL,addr save ,OFFSET AppName,MB_OK
   
.ELSEIF ax==IDM_EXIT
    invoke MessageBox ,NULL,addr EXIT ,OFFSET AppName,MB_OK
invoke PostQuitMessage,NULL

  .ELSEIF ax==IDM_OPENM
      invoke MessageBox ,NULL ,addr openm,OFFSET AppName,MB_OK
   .ELSEIF ax==IDM_SAVEM
      invoke MessageBox ,NULL ,addr savem ,OFFSET AppName,MB_OK
 
.ELSEIF ax==IDM_EXITM
   invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
   invoke PostQuitMessage,NULL



                          .ELSE
     invoke DestroyWindow,hWnd

.ENDIF


.ELSE
    invoke DefWindowProc,hWnd,uMSG,wParam,lParam
    ret
        .ENDIF


                xor   eax,eax
               ret
               WndProc endp
   
   end start


Can anyone tell me where i am wrong

dedndave

may not be the only problem, but i do not see TranslateMessage in the message loop   :bg

    .WHILE TRUE
                invoke GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
                invoke TranslateMessage, ADDR msg
                invoke DispatchMessage, ADDR msg
    .ENDW

dedndave

                          .ELSE
     invoke DestroyWindow,hWnd

.ENDIF


.ELSE
    invoke DefWindowProc,hWnd,uMSG,wParam,lParam
    ret
        .ENDIF

i hate if/then/else structures - lol
but, i think your else is DestroyWindow most of the time   :P

Tight_Coder_Ex

Your problem is in WM_CREATE and I'm not sure why Setting Focus to the edit window is returning null as CreateWindowEx does return a handle.

I've removed the 3 lines in WM_CREATE and that results in the window being displayed without menu of course as I don't have resource file, so I compiled it without.

RuiLoureiro

Quote from: dedndave on May 03, 2011, 02:54:59 PM
i hate if/then/else structures - lol

    You may use this:

       SWITCH  uMSG
       case    WM_DESTROY


       case    WM_CREATE


       case    WM_COMMAND


       default

            invoke DefWindowProc,hWnd,uMSG,wParam,lParam
      ret
       ENDSW

RuiLoureiro

Replace this:
 
.ELSEIF ax==IDM_EXITM
   invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
   invoke PostQuitMessage,NULL
                          .ELSE
     invoke DestroyWindow,hWnd
.ENDIF


By this:

  .ELSEIF ax==IDM_EXITM
   invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
   invoke PostQuitMessage,NULL
                           ;  .ELSE
 ;invoke DestroyWindow,hWnd
  .ENDIF


and you get the window
;---------
You need this too

    .WHILE TRUE
                invoke GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
               
                invoke TranslateMessage, ADDR msg
                invoke DispatchMessage, ADDR msg
    .ENDW   

dedndave

 :P
it ain't pretty, but.....
.386
.model flat ,stdcall
option casemap :none


include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc


includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib


WinMain proto :DWORD,:DWORD,:DWORD,:DWORD



.data
ClassName db "SimpleWinClass",0
AppName  db "Our First Window",0
MenuName db "FirstMenu",0   

open db " SIMPLE I/O FILE OPEN ",0
save db " SIMPLE SAVE",0
EXIT db "SIMPLE EXIT FROM FIRST TAB ",0

openm db "FIle will be Mapped",0
savem db "FIle will be unmapped ",0
exits db "File will UNMAPPED AND EXIT",0

EditClass db "edit",0


.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hwndEdit HWND ?
hWin HWND ?
buffer db 512 dup(?)

.const
IDM_OPEN equ 1
IDM_SAVE equ 2
IDM_EXIT equ 3

IDM_OPENM equ 4
IDM_SAVEM equ 5
IDM_EXITM equ 6

EditID equ 11

.code

start :

    invoke GetModuleHandle, NULL
    mov    hInstance,eax
    invoke GetCommandLine
    mov CommandLine,eax
    invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess,eax


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG

    mov   wc.cbSize,SIZEOF WNDCLASSEX
    mov   wc.style, CS_HREDRAW or CS_VREDRAW
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra,NULL
    mov   wc.cbWndExtra,NULL
    push  hInst
    pop   wc.hInstance
    mov   wc.hbrBackground,COLOR_WINDOW+1
    mov   wc.lpszMenuName,OFFSET MenuName       
    mov   wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov   wc.hIcon,eax
    mov   wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov   wc.hCursor,eax
    invoke RegisterClassEx, addr wc


    invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,600,NULL,NULL,hInst,NULL
   
mov   hWin,eax
    invoke ShowWindow, eax,SW_SHOWNORMAL
    invoke UpdateWindow, hWin

mLoop0:
                invoke GetMessage, ADDR msg,NULL,0,0
neg eax
shr eax,1
jz exit_main
                invoke TranslateMessage, ADDR msg
                invoke DispatchMessage, ADDR msg
jmp mLoop0

exit_main:
mov     eax,msg.wParam
    ret
WinMain endp


WndProc proc hWnd:HWND,uMSG:UINT,wParam:WPARAM,lParam:LPARAM

mov ecx,uMSG
cmp ecx,WM_DESTROY
jnz WndPr0

      invoke PostQuitMessage,NULL
      jmp WndPr3

WndPr0:
cmp ecx,WM_CLOSE
jnz WndPr1

     invoke DestroyWindow,hWnd
     jmp WndPr3

WndPr1:
cmp ecx,WM_CREATE
jnz WndPr2

                invoke CreateWindowEx,NULL,addr EditClass,NULL,WS_CHILD or WS_VISIBLE ,50,35,200,25,hWnd,EditID,hInstance,NULL
                mov hwndEdit,eax
                invoke SetFocus,hwndEdit
                jmp WndPr3

WndPr2:
cmp ecx,WM_COMMAND
jnz WndPr4
          movzx eax,word ptr wParam

cmp eax,IDM_OPEN
jnz wmCmd0

      invoke MessageBox,NULL,addr open ,OFFSET AppName,MB_OK
      jmp WndPr3

wmCmd0:
cmp eax,IDM_SAVE
jnz wmCmd1

   invoke MessageBox ,NULL,addr save ,OFFSET AppName,MB_OK
      jmp WndPr3

wmCmd1:
cmp eax,IDM_EXIT
jnz wmCmd2

    invoke MessageBox ,NULL,addr EXIT ,OFFSET AppName,MB_OK
INVOKE  SendMessage,hWnd,WM_SYSCOMMAND,SC_CLOSE,NULL
      jmp WndPr3

wmCmd2:
cmp eax,IDM_OPENM
jnz wmCmd3

      invoke MessageBox ,NULL ,addr openm,OFFSET AppName,MB_OK
      jmp WndPr3

wmCmd3:
cmp eax,IDM_SAVEM
jnz wmCmd4

      invoke MessageBox ,NULL ,addr savem ,OFFSET AppName,MB_OK
      jmp WndPr3

wmCmd4:
cmp eax,IDM_EXITM
jnz WndPr4

   invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
INVOKE  SendMessage,hWnd,WM_SYSCOMMAND,SC_CLOSE,NULL

WndPr3:
                xor   eax,eax
               ret

WndPr4:
    invoke DefWindowProc,hWnd,uMSG,wParam,lParam
    ret

WndProc endp
   
   end start

RuiLoureiro

Another version with CASE

.386
.model flat ,stdcall
option casemap :none


include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

        include \masm32\macros\macros.asm

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib


WinMain proto :DWORD,:DWORD,:DWORD,:DWORD


.data
ClassName db "SimpleWinClass",0
AppName  db "Our First Window",0
MenuName db "FirstMenu",0   

open db " SIMPLE I/O FILE OPEN ",0
save db " SIMPLE SAVE",0
EXIT db "SIMPLE EXIT FROM FIRST TAB ",0

openm db "FIle will be Mapped",0
savem db "FIle will be unmapped ",0
exits db "File will UNMAPPED AND EXIT",0

EditClass db "edit",0


.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hwndEdit HWND ?
buffer db 512 dup(?)

.const
IDM_OPEN equ 1
IDM_SAVE equ 2
IDM_EXIT equ 3

IDM_OPENM equ 4
IDM_SAVEM equ 5
IDM_EXITM equ 6

EditID equ 11

.code

start :

    invoke GetModuleHandle, NULL
    mov    hInstance,eax
    invoke GetCommandLine
    mov CommandLine,eax
    invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
    invoke ExitProcess,eax


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG
    LOCAL hwnd:HWND
    mov   wc.cbSize,SIZEOF WNDCLASSEX
    mov   wc.style, CS_HREDRAW or CS_VREDRAW
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra,NULL
    mov   wc.cbWndExtra,NULL
    push  hInst
    pop   wc.hInstance
    mov   wc.hbrBackground,COLOR_WINDOW+1
    mov   wc.lpszMenuName,OFFSET MenuName       
    mov   wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov   wc.hIcon,eax
    mov   wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov   wc.hCursor,eax
    invoke RegisterClassEx, addr wc


    invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,600,NULL,NULL,hInst,NULL
   
mov   hwnd,eax
    invoke ShowWindow, hwnd,SW_SHOWNORMAL
    invoke UpdateWindow, hwnd
    .WHILE TRUE
                invoke GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
               
                invoke TranslateMessage, ADDR msg
                invoke DispatchMessage, ADDR msg
    .ENDW   
   
mov     eax,msg.wParam
    ret
WinMain endp


WndProc proc hWnd:HWND,uMSG:UINT,wParam:WPARAM,lParam:LPARAM

       SWITCH  uMSG
       case    WM_DESTROY

            invoke PostQuitMessage,NULL

        case    WM_CREATE

                invoke CreateWindowEx,NULL,addr EditClass,NULL,WS_CHILD or WS_VISIBLE ,50,35,200,25,hWnd,EditID,hInstance,NULL
                mov hwndEdit,eax
                invoke SetFocus,hwndEdit

        case    WM_COMMAND

                    switch  wParam
                    case    IDM_OPEN
      invoke MessageBox,NULL,addr open ,OFFSET AppName,MB_OK

                    case    IDM_SAVE
invoke MessageBox ,NULL,addr save ,OFFSET AppName,MB_OK
   
                    case    IDM_EXIT
invoke MessageBox ,NULL,addr EXIT ,OFFSET AppName,MB_OK
invoke PostQuitMessage,NULL

                    case    IDM_OPENM
invoke MessageBox ,NULL ,addr openm,OFFSET AppName,MB_OK

                    case    IDM_SAVEM
invoke MessageBox ,NULL ,addr savem ,OFFSET AppName,MB_OK
 
                    case    IDM_EXITM
invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
      invoke PostQuitMessage,NULL
                     ;default
                    endsw

       default

            invoke DefWindowProc,hWnd,uMSG,wParam,lParam
ret
       ENDSW
            xor   eax,eax
            ret
WndProc endp    
   end start

dedndave

he's trying to work through Iczelion's tutorial, and we are throwing him all kinds of curve balls   :P

a few important issues...

1) when you select "Exit" from a menu, you should:
INVOKE  SendMessage,hWnd,WM_SYSCOMMAND,SC_CLOSE,NULL
xor eax,eax
ret

instead of this...
invoke PostQuitMessage,NULL
xor eax,eax
ret


2) you should handle WM_CLOSE with:
invoke DestroyWindow,hWnd
xor eax,eax
ret


3) the message loop needs:
invoke TranslateMessage, ADDR msg

i don't understand why all the menu commands are repeated twice
IDM_SAVE and IDM_SAVEM, for example
probably not necessary

MichaelW

win_32_found,

The missing TranslateMessage call in your message loop will not prevent the window from opening, but it will effectively disable keyboard input to the edit control. The primary problem I found is your menu handling code. I had to comment all of the uMSG==WM_COMMAND code out before I could get the window to open, or at least open and stay open. Instead of using a class menu, I think you should just create or load a menu (from a resource) when your main window is created. And I would add a WS_BORDER style to the edit control to make it readily visible.

And if you replace:

.386
.model flat ,stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib


With:

include \masm32\include\masm32rt.inc

Then you will have access to all of the commonly used MASM32 components. And if you link your app as a console app, in addition to the window you will have a console. You will then be able to use the print macro to conveniently display diagnostic messages to console, and use the console to close the app if for some reason you can't close the window.

eschew obfuscation

win_32_found

First of all thanks to all   their replies...i  Never expected response like that...

I got my code working ..BY  removing

.ELSE

invoke DestroyWindow,hWnd

@RuiLoureiro  Realy Like Ur case approach  :U

@dedndave  Yeah i am trying Iczelion tutes...Actually i made these  IDM_SAVE for simple FILE I/O and IDM_SAVEM for MEMORY MAPPED FILES.


Also can  anyone suggest me a Debugger (LIKE GDB IN LINUX )

dedndave

that's ok - add another menu item
but, you also have IDM_EXIT and IDM_EXITM   :bg

RuiLoureiro

win_32_found,  :U
               When i want to close the window
               i use this:
                          invoke SendMessage, hWnd, WM_CLOSE, 0, 0

               Note that when uMSG=WM_DESTROY or uMSG=WM_CLOSE
               the prog exits here:

                xor   eax,eax
                ret


WndProc proc hWnd:HWND,uMSG:UINT,wParam:WPARAM,lParam:LPARAM

       SWITCH  uMSG
       case    WM_DESTROY

            invoke PostQuitMessage, NULL

       case    WM_CLOSE
       
            ; Do something here before CLOSE if you want

            invoke    DestroyWindow,  hWnd      ; send uMSG=WM_DESTROY

        case    WM_CREATE

                invoke CreateWindowEx,NULL,addr EditClass,NULL,WS_CHILD or WS_VISIBLE ,50,35,200,25,hWnd,EditID,hInstance,NULL
                mov hwndEdit,eax
                invoke SetFocus,hwndEdit

        case    WM_COMMAND

                    switch  wParam
                    case    IDM_OPEN
      invoke MessageBox,NULL,addr open ,OFFSET AppName,MB_OK

                    case    IDM_SAVE
invoke MessageBox ,NULL,addr save ,OFFSET AppName,MB_OK
   
                    case    IDM_EXIT
invoke MessageBox ,NULL,addr EXIT ,OFFSET AppName,MB_OK
                        invoke SendMessage, hWnd, WM_CLOSE, 0, 0
;invoke PostQuitMessage,NULL

                    case    IDM_OPENM
invoke MessageBox ,NULL ,addr openm,OFFSET AppName,MB_OK

                    case    IDM_SAVEM
invoke MessageBox ,NULL ,addr savem ,OFFSET AppName,MB_OK
 
                    case    IDM_EXITM
invoke MessageBox ,NULL,addr exits ,offset AppName,MB_OK
                        invoke SendMessage, hWnd, WM_CLOSE, 0, 0
      ;invoke PostQuitMessage,NULL
                     ;default
                    endsw
       default

            invoke DefWindowProc,hWnd,uMSG,wParam,lParam
ret
       ENDSW
            xor   eax,eax
            ret
WndProc endp    
   end start

RuiLoureiro

Quote from: win_32_found on May 03, 2011, 04:45:43 PM
Also can  anyone suggest me a Debugger (LIKE GDB IN LINUX )
OllyDbg search for it here