54
to "CS_HREDRAW or CS_VREDRAW", then the offset of the windows procedure is set. You will
find out what the window procedure is later, for now remember that you need the address of the
WndProc procedure, which you can get with 'offset WndProc'. cbClsExtra and cbWndExtra are not
used by us so set them to NULL.
push hInst
pop wc.hInstance
The wc.hInstance is set to the hInst parameter of WinMain. Why don't we use:
mov
wc.hInstance, hInst
? Because the mov instruction does not allow to move from one memory
location to another. With the push/pop method, the value to move is pushed on stack, and than
popped of into the destination.
mov wc.hbrBackground, COLOR_WINDOW
mov wc.lpszMenuName, NULL
mov wc.lpszClassName, OFFSET ClassName
The background color of the class is set to COLOR_WINDOW, no menu is defined (NULL), and the
lpszClassName is set to a pointer to the null-terminated classname-string: "FirstWindowClass". This
should be a unique name defined for your own application.
invoke LoadIcon, NULL, IDI_APPLICATION
mov wc.hIcon, eax
mov wc.hIconSm, eax
The window needs an icon, but because we need a handle to an icon, we use LoadIcon to load an
icon and get a handle. LoadIcon has two parameters: hInstance, and lpIconName. hInstance is the
module handle whose executable file contains the icon. lpIconName is a pointer to a string that is the
name of the icon resource or a resource ID. If you use NULL as hInstance, you can choose from some
standard icons. (Which we do because we don't have an icon resource here). hIconSm is the small
icon, you can use the same handle for it.
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
Same thing for the cursor, just NULL as hInstance, and a standard cursor type: IDC_ARROW, the
standard windows arrow.
invoke RegisterClassEx, ADDR wc
Now finally register the class using RegisterClassEx with a pointer to the WNDCLASSEX structure
wc as parameter.
<view code>
12.5 - Creating the window
Now you've registered a class you can create a window from it:
HWND CreateWindowEx(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // handle to menu, or child-window identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
44 / 53
53
);
dwExStyle and dwStyle are two parameters which determine the style of the window.
lpClassName is a pointer to your registered classname.
lpWindowName is the name of your window (this will be in the caption of your window if it has
one)
x, y, nWidth, nHeight determine the position and size of your window.
hWndParent is the handle of the window that owns the new window. Can be null if you don't have a
parent window.
hMenu is a handle of a windows menu (discussed in a later tutorial, null for now)
hInstance is the application instance handle
lpParam is an extra value you can use in your program
.data
AppName "FirstWindow",0
.code
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
CW_USEDEFAULT,400,300,NULL,NULL,\
hInst,NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
(note that the \-character makes the assembler read to the next line as if it where on the same line.)
Our code will create a new windows, with our classname we've just registered. The title will be
"FirstWindow" (AppName), the style is WS_OVERLAPPEDWINDOW, which is a style that creates
an overlapped window with a caption, system menu, resizable border and a minimize/maximize-
box. CW_USEDEFAULT as x and y position will make windows use the default position for the new
window. The (initial) size of the window is 400x300 pixels.
The return value of the function is the window handle, HWND, which is stored in the local variable
hwnd. Then the window is showed with ShowWindow. UpdateWindow ensures that the window
will be drawn.
12.6 - Message Loop
A window can communicate with your program and other windows using messages. The window
procedure (see later on in this tutorial) is called whenever a message is pending for a specific
window. Each window has a message loop or message pump. This is an endless loop that checks if
there's a message for your window, and if it is, passes the message to the dispatchmessage function.
This function will call your window procedure. The message loop and the windows procedure are
two different things!!!
WinMain proc
hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL hwnd:DWORD
LOCAL msg:MSG ;<<<NEW
........
.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
45 / 53
48
This is what the message loop looks like. The .WHILE TRUE, .ENDW loop goes on until eax is 0.
GetMessage returns 0 if it receives the message WM_QUIT, which should close the window so the
program must exit the messageloop whenever GetMessage returns 0. If it doesn't, the message is
passed to TranslateMessage (this function translates keypresses to messages) and then the message
is dispatched by windows using the DispatchMessage function. The message itself in a message loop
consists of a MSG structure (LOCAL msg:MSG is added to the procedure, which adds a local
message structure called msg). You can use this message loop in all your programs.
12.7 - Window Procedure
Messages will be send to the window procedure. A window procedure should always look like this:
WndProc PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD
.code
WndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
mov eax, uMsg
.IF eax==XXXX
.ELSEIF eax==XXXX
.ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
.ENDIF
ret
WndProc endp
The window procedure should always have 4 parameters:
hWnd contains the window handle.
uMsg is the message
wParam is the first parameter for the message (message specific)
lParam is the second parameter for the message (message specific)
Messages that the window does not process should be passed to DefWindowProc, which takes care
of the processing. An example window procedure:
WndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
mov eax, uMsg
.IF eax==WM_CREATE
invoke MessageBox, NULL, ADDR AppName, ADDR AppName, NULL
.ELSEIF eax==WM_DESTROY
invoke PostQuitMessage, NULL
.ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
.ENDIF
ret
WndProc endp
This code displays the name of the application when the window is initialized. Also note that I've
added the processing of the WM_DESTROY message. This message is sent if the window should
close. The application should react to it with a PostQuitMessage.
Now take a look at the final code:
<view code>
[top]
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
46 / 53
10
Win32Asm Tutorial
prev14- N/A
14.0 - N/A
Sorry, this tutorial is not available yet. This tutorial is still under construction. Please check again
later!
[top]
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
47 / 53
16
Win32Asm Tutorial
prevToolsnext
Tools
This section describes the use of various tools. Please select a tool:
Assembler
Masm
Linker
Link
Resource compiler
brc32
rc
[top]
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
48 / 53
34
Win32Asm Tutorial
prevTools\Masmnext
Masm
Masm is the assembler I use. Others are tasm and nasm but I prefer masm myself (see why). The use
of tasm and nasm (and other assemblers) is not discussed here.
Usage
Masm is ml.exe. The version I use is "Macro Assembler Version 6.14.8444". Syntax:
ML [ /options ] filelist [ /link linkoptions ]
Here are some important options:
/c
assemble without linking
You will use this option most of the time as you will be using
an external linker like link.exe to link your files.
/coff
generate COFF format object file
This is the generic file format for the microsoft linker.
/Fo<file>
name object file
Can be used if you want to output an object file with an other
name than your source file
/G<c|d|z>
Use Pascal, C, or Stdcall calls
Select the default calling convention for your procedures.
/Zi
Add symbolic debug info
Use this option if you want to use a debugger.
/I<name>
Set include path
Defines your default include path
[top]
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
49 / 53
42
Win32Asm Tutorial
prevTools\Linknext
Link
Link is the standard linker of microsoft.
Usage
Link is link.exe. The version I use is "Incremental Linker Version 5.12.8078". Syntax:
LINK [options] [files] [@commandfile]
Here are some important options:
/DEBUG
Debug
This will create debug information. Use this option when
you want to use a debugger.
/DEBUGTYPE:CV|COFF
Debugtype: codeview / coff
Selects the output format of the debug info. This depends
on your debugger. Softice and the visual c++ debugger
both can handle CV (codeview)
/DEF:filename
DEF file
Specifies the definition file (.def). Used with dll's to
export functions.
/DLL
DLL
Outputs a dynamic link library instead of an executable.
/LIBPATH:path
Libpath
Specifies the path of your library files (*.lib).
/I<name>
Set include path
Defines your default include path.
/OUT:filename
Out:filename
Can override the default output filename.
/SUBSYSTEM:{...}
Subsystem
Selects the OS the program should run on:
NATIVE|WINDOWS|CONSOLE|WINDOWSCE|POSIX
[top]
Win32Asm Tutorial
Converted by Atop CHM to PDF Converter free version!
http://www.chmconverter.com
50 / 53
Documents you may be interested
Documents you may be interested