Author Topic: Menus in Windows  (Read 19166 times)

nobody

  • Guest
Menus in Windows
« on: October 30, 2004, 05:36:44 PM »
I cant get my menu working, can you tell me what is wrong?
I have 4 files:
one for the WinMain function,
one for pre-WinMain code,
one for the winproc and
a resource script

Sorry if this is too much code

WinMain.asm:

%include "win32n.inc"

global WinMain
global printError

extern RegisterClassExA
;import RegisterClassExA user32.dll
extern MessageBoxA
;import MessageBoxA user32.dll
extern CreateWindowExA
;import CreateWindowExA user32.dll
extern FormatMessageA
;import FormatMessageA kernel32.dll
extern GetLastError
;import GetLastError kernel32.dll
extern LoadCursorA
;import LoadCursorA user32.dll
extern LoadIconA
;import LoadIconA user32.dll
extern GetMessageA
;import GetMessageA user32.dll
extern TranslateMessage
;import TranslateMessage user32.dll
extern DispatchMessageA
;import DispatchMessageA user32.dll

extern messageFunc

section .data USE32
menuName db "AMENU",0
className db "AName",0
wincls:
   istruc WNDCLASSEX
      at WNDCLASSEX.cbSize,      dd WNDCLASSEX_size
      at WNDCLASSEX.style,      dd CS_HREDRAW | CS_VREDRAW
      at WNDCLASSEX.lpfnWndProc,   dd messageFunc
      at WNDCLASSEX.cbClsExtra,   dd 0
      at WNDCLASSEX.cbWndExtra,   dd 0
      at WNDCLASSEX.hInstance,   dd 0
      at WNDCLASSEX.hIcon,      dd 0
      at WNDCLASSEX.hCursor,      dd 0
      at WNDCLASSEX.hbrBackground,   dd COLOR_BACKGROUND
      at WNDCLASSEX.lpszMenuName,   dd menuName
          at WNDCLASSEX.lpszClassName,   dd className
      at WNDCLASSEX.hIconSm,      dd 0
   iend
msg:
   istruc MSG
   iend
error db "Error",0
winclserr db "Could not register windowclass",0
title db "A Window", 0
winerr db "Could not create window",0
buffer times 100 db 0
hwnd dd 0
section .code USE32

%define hInstance 4
%define hPrevInstance 8
%define cmdLine 12
%define windowStyle 16

WinMain:
push ebp
mov ebp,esp
push eax
   push dword IDC_ARROW
   push dword NULL
   call LoadCursorA
   mov [wincls+WNDCLASSEX.hCursor],eax
   test eax,eax
   jnz cursorLoaded

;call printError
   jmp end

cursorLoaded:
   push dword IDI_QUESTION
   push dword NULL
   call LoadIconA
   mov [wincls+WNDCLASSEX.hIcon],eax
   mov [wincls+WNDCLASSEX.hIconSm],eax
   test eax,eax
   jnz iconLoaded

;call printError
   jmp end

iconLoaded:

mov eax,[hInstance+ebp]
   mov [wincls+WNDCLASSEX.hInstance], eax
   push dword wincls
   call RegisterClassExA
   test eax,eax
   jnz clsRegistered

;call printError
   jmp end

clsRegistered:
   push MB_OK
   push error
   push menuName
   push dword 0
   call MessageBoxA
   push dword 0
   mov eax,[hInstance+ebp]
   push eax
   push dword 0
   push dword 0
   push dword 100
   push dword 100
   push dword 20
   push dword 20
   push dword WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_SYSMENU | WS_VISIBLE
   push dword title
   push dword className
   push dword WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE
   call CreateWindowExA
   test eax,eax
   jnz windowOpened

;call printError
   jmp end

windowOpened:

mov [hwnd],eax

messageLoop:

push dword 0
   push dword 0
   push dword 0
   push dword msg
   call GetMessageA
   test eax,eax
   jz end
   cmp eax,dword -1
   je errorMessage

push dword msg
   call TranslateMessage

push dword msg
   call DispatchMessageA
   jmp messageLoop

errorMessage:
   jmp messageLoop

end:
xor eax,eax
mov [hwnd],eax
pop eax
mov esp,ebp
pop ebp
ret 16

printError:
push ebp
mov ebp,esp
push eax

call GetLastError

push dword 0
   push dword 99
   push dword buffer
   push dword 0
   push eax
   push dword 0
   push FORMAT_MESSAGE_FROM_SYSTEM
   call FormatMessageA

push MB_OK
   push error
   push buffer
   push dword [hwnd]
   call MessageBoxA

pop eax
mov esp,ebp
pop ebp
ret

Startup.asm

%include "win32n.inc"

extern ExitProcess
;import ExitProcess kernel32.dll
extern MessageBoxA
;import MessageBoxA user32.dll
extern GetModuleHandleA
;import GetModuleHandleA kernel32.dll
extern GetCommandLineA
;import GetCommandLineA kernel32.dll

extern WinMain
;extern printError

segment .data USE32
ClassName db "SimpleWinClass",0
AppName db "Our First Window",0

segment .data? USE32
hInstance resd 1
CommandLine resd 1

segment .code USE32
..start
   push dword 0
   call GetModuleHandleA
   mov [hInstance],eax

call GetCommandLineA
   mov [CommandLine],eax

push SW_SHOWDEFAULT
   push CommandLine
   push NULL
   push hInstance
   call WinMain

push eax
call ExitProcess

MessageFunc.asm:

%include "win32n.inc"

extern PostQuitMessage
;import PostQuitMessage   user32.dll
extern DefWindowProcA
;import DefWindowProcA   user32.dll
;extern printError

global messageFunc

section .code USE32
jmp messageFunc
messageFunc:
push ebp
mov ebp,esp

;ebp=ebp
;ebp+4=retadrr
;ebp+8=hwnd
;ebp+12=uint
;ebp+16=wParam
;ebp+20=lParam
   mov eax,[ebp+12]
   cmp eax,WM_DESTROY
   je wm_destroy
   mov eax,[ebp+20]
   push eax
   mov eax,[ebp+16]
   push eax
   mov eax,[ebp+12]
   push eax
   mov eax,[ebp+8]
   push eax
   call DefWindowProcA
   jmp end
   wm_destroy:

push dword 0
   call PostQuitMessage
   mov eax,0

;jmp end
   end:
mov esp,ebp
pop ebp
ret 16

resource.rc:

AMENU MENU
BEGIN
    POPUP "File"
    BEGIN
        MENUITEM "New",100h
        MENUITEM "Open",101h
        MENUITEM "Save",102h
        MENUITEM "Save As",103h
        MENUITEM SEPARATOR
        MENUITEM "Exit",104h
    END
    POPUP "Edit"
    BEGIN
        MENUITEM SEPARATOR
    END
    POPUP "Help"
    BEGIN
        MENUITEM "About",300h
    END
END


Assembled and linked by

nasm Startup.asm -fobj
nasm WinMain.asm -fobj
nasm MessageFunc.asm -fobj
gorc /r resource.rc
alink Startup.obj WinMain.obj MessageFunc.obj -oPE -o test.exe D:\PATH\win32.lib resource.res

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Menus in Windows
« Reply #1 on: November 14, 2004, 08:01:50 AM »
One thing I see... in your WinMain, you've got "hInstance" defined as 4. You call WinMain with the handle returned from GetModuleHandle on the stack. I think you're going to find that at [ebp + 8], not [ebp + 4]...

There may be other problems... I don't know much Windows programming...

Best,
Frank