Search code examples
windowsassemblyx86masm32

x86 assembly - window does not show up yet no compile-time errors


I am trying to create a window in x86 assembly with masm32 using the CreateWindowEx API. I have gotten my code to have no compile-time errors or anything of the sort- it compiles just fine. Yet when I run the exe, nothing happens. I don't see any obvious errors, and I have practically copied the code out of Iczelion's Win32 Tutorial (Part 3 - A Simple Window). What is wrong with it?

Here is my code:

.386 
.model flat, stdcall 
option casemap :none 
WinMain proto :DWORD,:DWORD, :DWORD,:DWORD

include \masm32\include\windows.inc 
include \masm32\include\user32.inc 
include \masm32\include\kernel32.inc 
include \masm32\include\gdi32.inc 
includelib \masm32\lib\user32.lib 
includelib \masm32\lib\kernel32.lib 
includelib \masm32\lib\gdi32.lib

.data 
ClassName db "Testwin", 0
AppName db "Testing Window", 0
.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
.code 
start: 

push NULL
call GetModuleHandle
mov  hInstance,eax
call GetCommandLine
mov CommandLine, eax
push SW_SHOWDEFAULT
push CommandLine
push NULL
push hInstance
call WinMain
push eax
call ExitProcess

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE, CmdLine:LPSTR,CmdShow:DWORD
; local vars:
LOCAL wc:WNDCLASSEX 
LOCAL msg:MSG 
LOCAL hwnd:HWND

; defining the window:
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,NULL 
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 
;create the window
invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ 
       WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\ 
       CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\ 
       hInst,NULL
invoke ShowWindow,hwnd,SW_SHOWNORMAL
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
cmp uMsg, WM_DESTROY
jne _next
invoke PostQuitMessage, NULL
_next:

WndProc endp
end start 

Where have I gone wrong? I suspect it has something to do with CreateWindowEx, considering it takes 12 parameters, most of which I don't understand.

Thanks in advance.


Solution

  • I believe you have not assigned the window handle returned by CreateWindowEx to the hwnd variable.

    So add the following line after invoke CreateWindowEx and before invoke ShowWindow -

    mov hwnd, eax