Win32 Assembly – Part 2

w32

Note: Visit Win32 Assembly – Part 1 before proceeding.

Tools of trade:  Masm32 & WinAsm

Output: 

12

Code:

.386
.model flat, stdcall
option casemap:none

include windows.inc
include kernel32.inc
include user32.inc
includelib kernel32.lib
includelib user32.lib

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD

.data
MsgText db "This is hello world using dialog box",0

.data?
hInstance HINSTANCE ?

.code
Start:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
invoke ExitProcess,0



DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_COMMAND
     mov eax, wParam
 
    .if eax == 1003
        invoke SetDlgItemText,hWnd,1002,Addr MsgText
 
    .elseif eax == 1004
        invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
 
    .endif

.elseif uMsg == WM_CLOSE
     invoke EndDialog,hWnd,NULL

.else
     mov eax,FALSE
     ret

.endif
     mov eax, TRUE
     Ret
DialogProc endp

End Start

Description:

Create a window as shown in below images:

23
45678

Use Template
Use the template given in Win32 Assembly - Basics. It will look 
something like below:
1

DialogProc Proto :DWORD, :DWORD, :DWORD, :DWORD
DialogProc
It is just a pointer to the dialog box. It is also called 
CALL Back Function (CBF). CBF is a function which is called when
an event takes place.

Proto 
Tells the assembler how many parameters are needed for the 
function.

MsgText db "This is hello world using dialog box",0
Define a string of bytes. Refer, Win32 Assembly - part 1 if not
clear.

hInstance HINSTANCE ?
hInstance is the variable name of type HINSTANCE which is left
uninitialized. Instance handle is returned by Operating System using API GetModuleHandle. Instance handle is a dword value that gives the 
actual start address (imagebase) in memory of the EXE or DLL.

invoke GetModuleHandle,NULL
This api will return the instance handle of the exe or dll.
The argument to this api is long pointer to module name 
(lpModuleName). It is given NULL in our case, as if arg is NULL then
GetModuleHandle returns a handle to the file used to create the
calling process
Ref: GetModuleHandle

mov hInstance,eax
moving the instance handle in hInstance variable so that we can 
use it later.

invoke DialogBoxParam,hInstance,1001,NULL,Addr DialogProc,NULL
DialogBoxParam creates a dialog box from dialog box resource with
defined properties.

Ref: DialogBoxParam

hInstance 
It is the instance handle which we already got it.

1001 
It is the id of the dialog box.

10

NULL 
because this parameter is for handle to parent window. Since, 
there is no parent windows. Therefore, it's NULL.

Addr DialogProc
Address of the Call Back Function(CBF). In this case, it is
DialogProc, we defined earlier.

NULL 
because this parameter is for the lparam parameter of dialog
box. Leave, it for now, you will understand it later.

invoke ExitProcess,0
This is use to exit the process gracefully.

DialogProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
Defining the procedure for the DialogProc method. 

Proc
With proc directive, you can specify registers to be saved, define
parameters to the procedure and assign symbol names to the parameters

HWND,UNIT are of DWORD type.

hWnd
It is the parameter name. It will be used to store the window handle.
In simple terms, consider it as "Who sends the message?"

uMsg
It is the parameter name. It will be used to store the msg that is
send to the OS. Consider, it as "What is the message?"

wParam
It is used when additional parameter is to be passed.

lParam
It is used when additional parameter to be passed is a pointer.

.if uMsg == WM_COMMAND
IF clause as used in different languages. So, what this line conveys,
WM_COMMAND msg is send whenever an event occurs like mouse click,
button click, etc.

    mov eax, wParam
    moving the content of wParam in eax.
    wParam stores the ID of the control who sent the message.
 
    .if eax == 1003
     ID 1003 is the for "Click Me" button.
     9    
    invoke SetDlgItemText,hWnd,1002,Addr MsgText
    if "Click Me" button is clicked, then this method is invoked.
        
    SetDlgItemText
    This is used to set the title or text of a control in Dialog box
    Ref: SetDlgItemText

    hWnd
    a handle to the dialog box that contains the control.

    1002
    It is the ID of the control where text/title is to be written.
    Here, in this case, it is the ID of the textbox.
11

    Addr MsgText
    Pointer to the message text to be written in textbox

    .elseif eax == 1004
    1004 is the ID for exit button

    invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
    SendMessage
    Sends the specified message to the window.

    Ref: SendMessage

    WM_CLOSE
    This is the message to close or terminate the window

    Next two parameters are set NULL as no additional parameters to
    be passed in wParam and lParam. Therefore, they are set NULL.

    .endif

.elseif uMsg == WM_CLOSE
if the user close the window using "X" button.
 invoke EndDialog,hWnd,NULL
 EndDialog
 Destroys the modal dialog box

 hWnd
 We already discussed

 NULL
 It is the return value. 

.else
 mov eax,FALSE
 Dialog box procedure always return TRUE or FALSE. When returning
 TRUE ( i.e any other value than 0 ) then no further message
 processing will be performed and a cycle is complete.
 When returning FALSE, it will mean that default window procedure 
 will be called.

.endif
 mov eax, TRUE
 Ret
DialogProc endp
DialogProc ends here.

Note: If you have any doubt, ask in the comment section. It will help others also.