So, When I assemble this code, I get no errors. It flies through the build. However, when I go to execute this code, it does not display the window. I am unsure of why. I have tried invoking messageboxes at the start or WinMain, but it seems it's not getting called, because the MessageBox does not show up. However, I am actually calling the WinMain function. But it seems to not be running.

Any help would be great,
Thanks!

.386
.model flat, stdcall
option casemap :none 

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

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

WinMain PROTO :HINSTANCE, :HINSTANCE, :LPSTR, :DWORD
WinProc PROTO :HWND, :UINT, :WPARAM, :LPARAM

.data
    ClassName   db "MAIN_CLASS", 0
    MainTitle   db "A Window in Assembly!", 0
    ErrorText   db "Fatal Error, the program will now close...", 0
    ErrorTitle  db "Fatal Error", 0

.data?
    hInstance   HINSTANCE ?

.code
start:
    WinProc PROC hWnd:HWND, message:UINT, wParam:WPARAM, lParam:LPARAM
            cmp message, WM_DESTROY
            je DO_DESTROY

            cmp message, WM_CLOSE
            je DO_CLOSE

            jmp DO_DEFAULT

            DO_DESTROY:

            invoke PostQuitMessage, 0
            jmp DO_END

            DO_CLOSE:

            invoke DestroyWindow, addr hWnd
            jmp DO_END

            DO_DEFAULT:

            invoke DefWindowProc, hWnd, message, wParam, lParam
            ret
            jmp DO_STOP

            DO_END:

            xor eax, eax
            ret

            DO_STOP:

    WinProc ENDP

    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, 0
            mov  wc.lpfnWndProc, offset WinProc
            mov  wc.cbClsExtra, NULL
            mov  wc.cbWndExtra, NULL
            push hInstance
            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
            cmp eax, 0
            je SHOW_ERROR

            invoke CreateWindowEx,
                WS_EX_CLIENTEDGE,
                addr ClassName,
                addr MainTitle,
                WS_OVERLAPPEDWINDOW,
                50,
                50,
                600,
                300,
                NULL,
                NULL,
                hInst,
                NULL
            mov hWnd, eax

            cmp hWnd, NULL
            je SHOW_ERROR

            invoke ShowWindow, hWnd, CmdShow
            invoke UpdateWindow, hWnd

            message_loop:

            invoke GetMessage, addr msg, addr hWnd, 0, 0
            cmp eax, 0
            je message_loop_end
            invoke TranslateMessage, addr msg
            invoke DispatchMessage, addr msg
            jmp message_loop

            message_loop_end:

            mov eax, msg.wParam
            ret

            jmp STOP

            SHOW_ERROR:

            invoke MessageBox, NULL, addr ErrorText, addr ErrorTitle, MB_OK
            jmp RET_ZERO

            RET_ZERO:

            mov eax, 0
            ret

            STOP:
    WinMain ENDP

    invoke GetModuleHandle, NULL
    mov hInstance, eax
    invoke WinMain, hInstance, NULL, NULL, 0
    invoke ExitProcess, eax
end start

Thanks again!

You could try intercepting the WM_PAINT message. The DefWindowProc might not be doing anything.

Edited 3 Years Ago by mathematician

No, i got it to work now, But it's just not responding. I shall post the code later.

Your program starts executing at the start label and goes right to the WinProc, this won't work since you have not registered the window class yet.
Move this:

    invoke GetModuleHandle, NULL
    mov hInstance, eax
    invoke WinMain, hInstance, NULL, NULL, 0
    invoke ExitProcess, eax

from the end to right after start:

Your DestryWindow call is wrong, you are passing the address of hWnd and not the value, remove addr.
Your calling ShowWindow with CmdShow and you are passing 0 for that parameter in your call to WinMain. ShowWindow is not needed if you creat the window with WS_VISIBLE.

You also need a ret after your STOP label otherwise strange things will happen. Here is the corrected code in which the window will show and close properly.

.386
.model flat, stdcall
option casemap :none 

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

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


WinMain PROTO :HINSTANCE, :HINSTANCE, :LPSTR, :DWORD
WinProc PROTO :HWND, :UINT, :WPARAM, :LPARAM

.data
    ClassName   db "MAIN_CLASS", 0
    MainTitle   db "A Window in Assembly!", 0
    ErrorText   db "Fatal Error, the program will now close...", 0
    ErrorTitle  db "Fatal Error", 0

.data?
    hInstance   HINSTANCE ?

.code
start:
    invoke GetModuleHandle, NULL
    mov hInstance, eax
    invoke WinMain, hInstance, NULL, NULL, 0
    invoke ExitProcess, eax
    WinProc PROC hWnd:HWND, message:UINT, wParam:WPARAM, lParam:LPARAM
            cmp message, WM_DESTROY
            je DO_DESTROY

            cmp message, WM_CLOSE
            je DO_CLOSE

            jmp DO_DEFAULT

            DO_DESTROY:

            invoke PostQuitMessage, 0
            jmp DO_END

            DO_CLOSE:

            invoke DestroyWindow,  hWnd
            jmp DO_END

            DO_DEFAULT:

            invoke DefWindowProc, hWnd, message, wParam, lParam
            ret 

            DO_END:

            xor eax, eax
            ret

    WinProc ENDP

    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, 0
            mov  wc.lpfnWndProc, offset WinProc
            mov  wc.cbClsExtra, NULL
            mov  wc.cbWndExtra, NULL
            push hInstance
            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
            cmp eax, 0
            je SHOW_ERROR

            invoke CreateWindowEx,
                WS_EX_CLIENTEDGE,
                addr ClassName,
                addr MainTitle,
                WS_OVERLAPPEDWINDOW or WS_VISIBLE,
                50,
                50,
                600,
                300,
                NULL,
                NULL,
                hInst,
                NULL
            mov hWnd, eax

            cmp hWnd, NULL
            je SHOW_ERROR

;            invoke ShowWindow, hWnd, CmdShow
;            invoke UpdateWindow, hWnd

            message_loop:

            invoke GetMessage, addr msg, NULL, 0, 0
            cmp eax, 0
            je message_loop_end
            invoke TranslateMessage, addr msg
            invoke DispatchMessage, addr msg
            jmp message_loop

            message_loop_end:

            mov eax, msg.wParam
            ret

            jmp STOP

            SHOW_ERROR:

            invoke MessageBox, NULL, addr ErrorText, addr ErrorTitle, MB_OK


            RET_ZERO:

            mov eax, 0
            ret
            STOP:
            ret
    WinMain ENDP


end start
This article has been dead for over six months. Start a new discussion instead.