Vous êtes sur la page 1sur 13

td_win32asm_004.

asm
;==============================================================================
;
Test Department's WINDOWS 32 BIT x86 ASSEMBLY TUTORIAL'S
004
;==============================================================================
;==============================================================================
; ==> Part 004 : allocating memory and adding a file menu for I/O operation
;-----------------------------------------------------------------------------; In this tut 004 we add a file menu to our program to load and save a file.
; Please notice that we do not change the file, we only load and save it.
; We must include another library for the I/O API dialog ( comdlg32.lib )
;==============================================================================
; Assembler directives
;-----------------------------------------------------------------------------.386
; specifies the processor our program want run on
.Model Flat ,StdCall
; always the same for Win95 (32 Bit)
option casemap:none
; case sensitive !!!
;==============================================================================
; Include all files where API functins resist you want use
; You must set the correct path to the include and library files
;-----------------------------------------------------------------------------include D:\Masm32\include\windows.inc
includelib kernel32.lib
includelib user32.lib
includelib winmm.lib
includelib gdi32.lib
includelib comdlg32.lib
;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
; GetModulHandle = example of an API function
; PROTO
= one or more parameter must pushed to the stack before call
; :DWORD
= the parameter, in this case doubleword (32 Bit)
;-----------------------------------------------------------------------------GetModuleHandleA
PROTO :DWORD
LoadIconA
PROTO :DWORD,:DWORD
LoadCursorA
PROTO :DWORD,:DWORD
RegisterClassExA
PROTO :DWORD
CreateWindowExA
PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ShowWindow
PROTO :DWORD,:DWORD
UpdateWindow
PROTO :DWORD
GetMessageA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
TranslateMessage
PROTO :DWORD
DispatchMessageA
PROTO :DWORD
PostQuitMessage
PROTO :DWORD
DefWindowProcA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
ExitProcess
PROTO :DWORD
PlaySoundA
PROTO :DWORD,:DWORD,:DWORD
MessageBoxA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
DestroyWindow
PROTO :DWORD
BeginPaint
PROTO :DWORD,:DWORD
Page 1

EndPaint
LoadBitmapA
CreateCompatibleDC
SelectObject
BitBlt

PROTO
PROTO
PROTO
PROTO
PROTO

DeleteDC
DeleteObject
GetOpenFileNameA
GetSaveFileNameA
CreateFileA
GetFileSize
ReadFile
WriteFile
CloseHandle
GlobalAlloc
GlobalFree

PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO

td_win32asm_004.asm
:DWORD,:DWORD
:DWORD,:DWORD
:DWORD
:DWORD,:DWORD
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
:DWORD,:DWORD,:DWORD
:DWORD
:DWORD
:DWORD
:DWORD
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
:DWORD,:DWORD
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
:DWORD
:DWORD,:DWORD
:DWORD

;==============================================================================
; .const
= the constants area starts here,constants are defined & fixed
; example_const = the constant name
; equ
= the value for this constant name follows
; 0Ah
= the value in hexadezimal
;-----------------------------------------------------------------------------.const
example_const
equ 0Ah
;not used, example only
; - Parameter MAIN WINDOW CallBack Procedure ( API=RegisterClassExA ) WP1_CallBack
equ [ebp+4]
;return address
WP1_hWnd
equ [ebp+8]
;handle of window who receives message
WP1_uMsg
equ [ebp+12]
;the message number
WP1_wParam
equ [ebp+16]
;extra info about the message
WP1_lParam
equ [ebp+20]
;extra info about the message
;==============================================================================
; .Data = the data area starts here, datas are defined but not fixed
; db
= databyte
1 Byte (8 Bit)
; dw
= dataword
2 Byte (16 Bit)
; dd
= doubleword
4 Byte (32 Bit)
; Textstrings are databyte and must be terminated by ,0
; ,13,10 means control and line feed
; db 41h dup (0) = reserved 41 hexadezimal databytes initialized to zero
;-----------------------------------------------------------------------------.Data
example_text
db "First row",13,10
;not used,example only
db "Second row",0
;
IconName
db "TDIcon",0
;icon name in rc file
MenuName
db "TDMenu",0
;menu name in rc file
ClassName
db "TDWinClass",0
;name of windows class
WindowName
db "Test Department",0
;window name titel bar
WaveName
db "TDWave",0
;wave name resource file
MB1Titel
db "Realy Exit ?",0
;message box name
MB1Text
db "Your choice ...",0
;message box text
PictureName
db "TDBitmap",0
;picture name in rc file
Page 2

FileFilter
LoadFileName
LoadFileNameT
SaveFileName
SaveFileNameT
LoadFileHandle
LoadFileSize
SaveFileHandle
SaveFileSize
LoadMem
FileReturn

db
db
db
db
db
dd
dd
dd
dd
dd
dd

td_win32asm_004.asm
"All Files",0,"*.*",0,0 ;file filter
104h dup(0),0
;buffer for load filename
"LOAD File, choose the filename",0;titel
104h dup(0),0
;buffer for save filename
"SAVE File, choose the filename",0;titel
0h
;handle of load file
0h
;size of load file
0h
;handle of save file
0h
;size of save file
0h
;pointer to alloc memory
0h
;for read/write file

align 4
; - WndClassEx Structure ( API=RegisterClassExA ) cbSize
dd 0h
;size in bytes of this structure
style
dd 0h
;window style
lpfnWndProc
dd 0h
;address of user proc function
cbclsExtra
dd 0h
;extra bytes to allocate set to 0
cbWndExtra
dd 0h
;extra bytes class directive, rc file
hInstance
dd 0h
;program handle(API=GetModuleHandleA)
hIcon
dd 0h
;handle of icon (API=LoadIconA)
hcursor
dd 0h
;handle of cursor (API=LoadCursor)
hbrBackground
dd 2h
;background color, 0=transparent
lpszMenuName
dd 0h
;name of menu class in resource file
lpszClassName
dd 0h
;name of windows this window class
hIconSm
dd 0h
;iconhandle 0=search in resource file
align 4
; - Msg Structure ( API=GetMessageA ) - member POINT = POINT structure
hWnd
dd 0h
;handle of window who receives message
message
dd 0h
;the message number
wParam
dd 0h
;extra info about the message
lParam
dd 0h
;extra info about the message
time
dd 0h
;time the message was posted
xpt
dd 0h
;cursor x-position, POINT struc
ypt
dd 0h
;cursor x-position, POINT struc
align 4
; - Paint Structure ( API=BeginPaint ) - member rcPaint = RECT structure
hdc
dd 0h
;id, display DC used for painting
fErase
dd 0h
;must background must be erased
rcPaint_left
dd 0h
;RECT,x-coordinate upper-left corner
rcPaint_top
dd 0h
;RECT,y-coordinate upper-left corner
rcPaint_right
dd 0h
;RECT,x-coordinate lower-right corner
rcPaint_bottom
dd 0h
;RECT,y-coordinate lower-right corner
fRestore
dd 0h
;reserved, used by Windows
fIncUpdate
dd 0h
;reserved, used by Windows
rgbReserved
db 20h dup (0) ;reserved, used by Windows
align 4
; - BitBlt Parameter ( API=BitBlt ) hdcDest
dd 0h
nXDest
dd 0h

;handle of dest. device context


;x=dest. rect. up-left corner
Page 3

nYDest
nWidth
nHeight
hdcSrc
nXSrc
nYSrc
dwRop

dd
dd
dd
dd
dd
dd
dd

align 4
; - OPENFILENAME Structure
lStructSize
dd
hWndOwner
dd
hInstance1
dd
lpstrFilter
dd
lpstrCustomFilter
dd
nMaxCustFilter
dd
nFilterIndex
dd
lpstrFile
dd
nMaxFile
dd
lpstrFileTitle
dd
nMaxFileTitle
dd
lpstrInitialDir
dd
lpstrTitle
dd
Flags
dd
nFileOffset
dw
nFileExtension
dw
lpstrDefExt
dd
lCustData
dd
lpfnHook
dd
lpTemplateName
dd

0h
0h
0h
0h
0h
0h
0h

td_win32asm_004.asm
;y=dest. rect. up-left corner
;width of destination rectangle
;height of destination rectangle
;handle of source device context
;x=src. rec. up-left corner
;y=src. rec. up-left corner
;raster operation code

( API=GetOpenFileNameA / GetOpenSaveFileNameA ) 0h
;the length in bytes of structure
0h
;handle of window that owns dialog box
0h
;handle, our program handle
0h
;pointer to string + filter
0h
;pointer to user defined filter
0h
;size bytes/characters of above
0h
;filter index of lpstrFilter
0h
;pointer initialize filename, 0=no
0h
;size bytes/characters of above
0h
;pointer title of the selected file
0h
;size of the lpstrFileTitle buffer
0h
;pointer init file dir,0=default
0h
;pointer title of the box
0h
;the dialog box creation flags
0h
;offset filename in string lpstrFile
0h
;offset extension in string lpstrFile
0h
;pointer to default extension of file
0h
;?
0h
;used if flag is OFN_ENABLETEMPLATE
0h
;used if flag is OFN_ENABLETEMPLATE

;==============================================================================
; .Data? = the data? area starts here, not defined and not fixed
;-----------------------------------------------------------------------------.data?
example_data?
dd ?
;not used, example only
;==============================================================================
; .CODE = our code area starts here
Main = label of our program code
;-----------------------------------------------------------------------------.Code
Main:
;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
;-----------------------------------------------------------------------------push
0h
;lpModuleHandle, 0=get program handle
call
GetModuleHandleA
;- API Function mov
hInstance,eax
;return value in eax=handle of program
;==============================================================================
; The API function "RegisterClassExA" registers a window class
; This API needs a "WNDCLASSEX" structure so we fill it with correct values
Page 4

td_win32asm_004.asm
;-----------------------------------------------------------------------------mov
cbSize,30h
;size in bytes of WNDCLASSEX structure
mov
style,3h
;window style
mov
lpfnWndProc,OFFSET WP1
;address of user lpfnWndProc function
mov
cbclsExtra,0h
;extra bytes to allocate set to 0
mov
cbWndExtra,0h
;class directive in rc file
mov
hbrBackground,2h
;background,1=background(parameter+1)
mov
lpszMenuName,OFFSET MenuName
;menu name in resource file
mov
lpszClassName,OFFSET ClassName ;name of windows class
mov
hIconSm,0h
;iconhandle 0=search in rc file
;-----------------------------------------------------------------------------; API "LoadIconA" loads an icon defined in the resource file and store the
; handle in the "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
OFFSET IconName
;icon-string or icon resource id
push
hInstance
;our program handle
call
LoadIconA
;- API Function mov
hIcon,eax
;handle of newly loaded icon
;-----------------------------------------------------------------------------; API "LoadCursorA" loads a default system cursor, in this case we must set
; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
; Then we push the cursor handle to "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
32512
;lpCursorName,default value in dezimal
push
0h
;hInstance, 0=default system cursor
call
LoadCursorA
;- API Function mov
hcursor,eax
;handle of the cursor
;-----------------------------------------------------------------------------; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx"
;-----------------------------------------------------------------------------push
OFFSET cbSize
;pointer to WNDCLASSEX structure
call
RegisterClassExA
;- API Function ;==============================================================================
; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an
; extended style. The return value in EAX is the handle of the new window.
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
0h
;hMenu, handle window menu 0=class menu
push
0h
;hWndParent, handle parent window 0=no
push
000000F8h
;intnHeight, window height pixel
push
0000020Ah
;intnWidth, window width pixel
push
000000A0h
;inty, vertical position window
push
000000B0h
;intx, horizontal position window
push
04CA0000h
;dwStyle, 0=no sysmenu/close buttons
push
OFFSET WindowName
;lpWindowName, pointer to window name
push
OFFSET ClassName
;lpClassName, pointer to class name
push
0300h
;dwExStyle, extra window style 0=no
call
CreateWindowExA
;- API Function mov
hWnd,eax
;hwnd,return value=handle of window
;==============================================================================
Page 5

td_win32asm_004.asm
; API "ShowWindow" function sets the specified window's show state.
;-----------------------------------------------------------------------------push
1h
;nCmdShow, show state 1=SW_SHOWNORMAL
push
hWnd
;hwnd, handle of window
call
ShowWindow
;- API Function ;==============================================================================
; API "UpdateWindow" updates the area of the specified window by sending a
; WM_PAINT message to the window if the window's update region is not empty.
;-----------------------------------------------------------------------------push
hWnd
;hwnd, handle of window
call
UpdateWindow
;- API Function ;==============================================================================
; API "PlaySoundA" plays a wave sound, the value 40005h means the sound is
; inside the resource file.
;-----------------------------------------------------------------------------push
40005h
;SND_RESOURCE+SND_ASYNC
push
hInstance
;handle of our program
push
OFFSET WaveName
;wave name in rc file
call
PlaySoundA
;- API Function ;==============================================================================
; API "GetMessageA" retrieves a message & places it in the specified structure.
;-----------------------------------------------------------------------------LoopGetMessage:
push
0h
;wMsgFilterMax, highest message value
push
0h
;wMsgFilterMin, lowest message value
push
0h
;hWnd, handle of window who gets msg.
push
OFFSET hWnd
;lpMsg, pointer to MSG structure
call
GetMessageA
;- API Function cmp
eax,0h
;check if return value=0 (exit)
je
ExitPrg
;if return value is 0 goto LABEL
;==============================================================================
; API "TranslateMessage" translates key code into ASCII character messages
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
TranslateMessage
;- API Function - keyboard code
;==============================================================================
; API "DispatchMessageA" function dispatches a message to a window procedure.
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
DispatchMessageA
;- API Function jmp
LoopGetMessage
;check for message again, goto LABEL
;==============================================================================
; Next we terminate our program (API=ExitProcess)
;-----------------------------------------------------------------------------ExitPrg:
push
hInstance
;push our programm handle to exit
call
ExitProcess
;- API Function Page 6

td_win32asm_004.asm
;##############################################################################
; This is the Window Procedure lpfnWndProc (API=RegisterClassExA) for this
; registered window.
; The WindowProc function is an application-defined callback function that
; processes messages sent to a window.
; Here our code for checking the receiving messages resist.
; In the future it is the main work for us to react to the recieved messages.
; It is also a good idea to PUSHAD all register, because than we are free to
; use all register in this window procedure.
; Before we leave this subroutine we must POPAD them back.
;-----------------------------------------------------------------------------WP1:
push
ebp
;create stack frame
mov
ebp,esp
;
pushad
;push all register to the stack
mov
eax,WP1_uMsg
;move the message number in eax
;==============================================================================
; WM_Destroy (value=2h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_02h:
cmp
eax,2h
;check if value=2h (WM_DESTROY)
jne
WP1_uMsg_0Fh
;if not 2h go to LABEL
call
My_CleanSystem
;- SubRoutine ;-----------------------------------------------------------------------------; API "PostQuitMessage" indicates to Windows a request to terminate
;-----------------------------------------------------------------------------push
0h
;nExitCode, exit code=wParam
call
PostQuitMessage
;- API Function popad
;pop all register back from stack
xor
eax,eax
;set eax to 0 to exit our program
mov
esp,ebp
;delete stack frame
pop
ebp
;
ret
10h
;return and clear stack
;==============================================================================
; WM_PAINT (value=0Fh) message, used to repaint the window area
;-----------------------------------------------------------------------------WP1_uMsg_0Fh:
cmp
eax,0Fh
;check if WM_PAINT message recieved
jne
WP1_uMsg_111h
;if not goto label
;-----------------------------------------------------------------------------; API "BeginPaint" prepares the specified window for painting and fills a
; PAINTSTRUCT structure with information about the painting
;-----------------------------------------------------------------------------push
OFFSET hdc
;lpPaint, pointer to PAINTSTRUCT
push
WP1_hWnd
;hwnd, handle of the window
call
BeginPaint
;- API Function mov
hdcDest,eax
;handle, display device (DC)=dest.
;-----------------------------------------------------------------------------; API "LoadBitmapA" loads the specified bitmap resource from our module's
; executable file.
Page 7

td_win32asm_004.asm
;-----------------------------------------------------------------------------push
OFFSET PictureName
;lpBitmapName,pic name in rc file
push
hInstance
;handle, id of our programm
call
LoadBitmapA
;- API Function push
eax
;hObject, PUSH handle of bitmap
;-----------------------------------------------------------------------------; API "CreateCompatibleDC" creates a memory device context (DC) compatible
; with the specified device.
;-----------------------------------------------------------------------------push
hdcDest
;hdc, display device (DC)=dest.
call
CreateCompatibleDC
;- API Function mov
hdcSrc,eax
;handle, memory device (DC)=src.
;-----------------------------------------------------------------------------; API "SelectObject" selects an object into the specified device context
;-----------------------------------------------------------------------------pop
eax
;hObject, POP handle of bitmap
push
eax
;
push
eax
;hObject, PUSH handle of bitmap
push
hdcSrc
;hdc, id memory device (DC)=src.
call
SelectObject
;- API Function ;-----------------------------------------------------------------------------; API "BitBlt" performs a bit-block transfer of the color data corresponding
; to a rectangle of pixels from the specified source device context into a
; destination device context.
;-----------------------------------------------------------------------------push
00CC0020h
;dwRop, raster operation code
push
0h
;nYSrc, y=src. rect. up-left corner
push
0h
;nXSrc, x=src. rect. up-left corner
push
hdcSrc
;hdcSrc, handle of source device
push
0C8h
;nHeight, height of dest. rect.
push
200h
;nWidth, width of dest. rect.
push
0h
;nYDest, y=dest. rect. up-left corner
push
0h
;nXDest, x=dest. rect. up-left corner
push
hdcDest
;hdcDest, handle destination device
call
BitBlt
;- API Function ;-----------------------------------------------------------------------------; API "DeleteDC" deletes the specified device context (DC)
;-----------------------------------------------------------------------------push
hdcSrc
;handle, memory device (DC)
call
DeleteDC
;- API Function ;-----------------------------------------------------------------------------; API "DeleteObject" deletes the bitmap, freeing all system resources
; associated with the object.
; After the object is deleted, the specified handle is no longer valid.
;-----------------------------------------------------------------------------pop
eax
;hObject, POP handle of bitmap
push
eax
;
call
DeleteObject
;- API Function ;-----------------------------------------------------------------------------; API "EndPaint" marks the end of painting in the given window.
; This function is required for each call to the BeginPaint function, but only
; after painting is complete.
;-----------------------------------------------------------------------------Page 8

push
push
call
jmp

OFFSET hdc
WP1_hWnd
EndPaint
WP1_return

td_win32asm_004.asm
;lpPaint, pointer PAINTSTRUCT
;hwnd, handle of the window
;- API Function -

;==============================================================================
; WM_COMMAND (value=111h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_111h:
;WM_COMMAND message, value=111h
cmp
eax,111h
;check if WM_COMMAND message recieved
jne
WP1_uMsg_112h
;if not goto label
;==============================================================================
; Check for extra message information, "&Exit" item in menu bar
;-----------------------------------------------------------------------------WP1_wParam_01h:
mov
eax,WP1_wParam
;extra info about the message
cmp
ax,1h
;ID of "&Exit" item in rc file
jne
WP1_wParam_02h
;if not 1h goto LABEL
;-----------------------------------------------------------------------------; API "MessageBoxA" creates a message box, we can choose if we want exit prg.
;-----------------------------------------------------------------------------push
4h
;uType, style, 4=MB_YESNO Button
push
OFFSET MB1Titel
;lpCaption,pointer to title text
push
OFFSET MB1Text
;lpText,pointer to text message box
push
WP1_hWnd
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function cmp
eax,6h
;if return value=6h (IDYES) then exit
jne
WP1_return
;if return value=7h (IDNO) goto LABEL
;-----------------------------------------------------------------------------; API "DestroyWindow" function destroys the given window
;-----------------------------------------------------------------------------push
WP1_hWnd
;hwnd, handle of window to destroy
call
DestroyWindow
;- API Function jmp
WP1_return
;==============================================================================
; Check for extra message information, "&Copy" item in menu bar
;-----------------------------------------------------------------------------WP1_wParam_02h:
cmp
ax,2h
;ID of "&Copy" item in rc file
jne
WP1_return
;if not 2h goto LABEL
;-----------------------------------------------------------------------------; API "GetOpenFileName" creates a system-defined dialog box that enables the
; user to select a file to open.
;-----------------------------------------------------------------------------mov
lStructSize,4Ch
;length in bytes of structure
mov
eax,WP1_hWnd
mov
hWndOwner,eax
;id window that owns dialog box
mov
eax,hInstance
mov
hInstance1,eax
;handle, our program id
mov
lpstrFilter,OFFSET FileFilter
;pointer to string + filter, 0= no
mov
lpstrCustomFilter,0h
;pointer to user defined filter
Page 9

td_win32asm_004.asm
mov
nMaxCustFilter,0h
;size bytes/characters of above
mov
nFilterIndex,0h
;filter index of lpstrFilter
mov
lpstrFile,OFFSET LoadFileName
;pointer initialize filename, 0=no
mov
nMaxFile,104h
;max size bytes/characters of above
mov
lpstrFileTitle,0h
;pointer title of the selected file
mov
nMaxFileTitle,0h
;size of the lpstrFileTitle buffer
mov
lpstrInitialDir,0h
;pointer init file dir,0=default
mov
lpstrTitle,OFFSET LoadFileNameT ;pointer title of the box
mov
Flags,00281804h
;the dialog box creation flags
mov
nFileOffset,0h
;offset filename in string lpstrFile
mov
nFileExtension,0h
;offset extension in string lpstrFile
mov
lCustData,0h
;?
mov
lpfnHook,0h
;? used if flag is OFN_ENABLETEMPLATE
mov
lpTemplateName,0h
;? used if flag is OFN_ENABLETEMPLATE
mov
lpstrDefExt,0h
;pointer default extension file, 0=no
push
OFFSET lStructSize
;pointer to OPENFILENAME Structure
call
GetOpenFileNameA
;- API Function cmp
eax,0h
;error ?
je
WP1_return
;-----------------------------------------------------------------------------; API "GetSaveFileName" creates a system-defined dialog box that enables the
; user to select a file to save.
;-----------------------------------------------------------------------------mov
lStructSize,4Ch
;length in bytes of structure
mov
eax,WP1_hWnd
mov
hWndOwner,eax
;id window that owns dialog box
mov
eax,hInstance
mov
hInstance1,eax
;handle, our program id
mov
lpstrFilter,OFFSET FileFilter
;pointer to string + filter, 0= no
mov
lpstrCustomFilter,0h
;pointer to user defined filter
mov
nMaxCustFilter,0h
;size bytes/characters of above
mov
nFilterIndex,0h
;filter index of lpstrFilter
mov
lpstrFile,OFFSET SaveFileName
;pointer initialize filename, 0=no
mov
nMaxFile,104h
;max size bytes/characters of above
mov
lpstrFileTitle,0h
;pointer title of the selected file
mov
nMaxFileTitle,0h
;size of the lpstrFileTitle buffer
mov
lpstrInitialDir,0h
;pointer init file dir,0=default
mov
lpstrTitle,OFFSET SaveFileNameT ;pointer title of the box
mov
Flags,00281804h
;the dialog box creation flags
mov
nFileOffset,0h
;offset filename in string lpstrFile
mov
nFileExtension,0h
;offset extension in string lpstrFile
mov
lCustData,0h
;?
mov
lpfnHook,0h
;? used if flag is OFN_ENABLETEMPLATE
mov
lpTemplateName,0h
;? used if flag is OFN_ENABLETEMPLATE
mov
lpstrDefExt,0h
;pointer default extension file, 0=no
push
OFFSET lStructSize
;pointer to OPENFILENAME Structure
call
GetSaveFileNameA
;- API Function cmp
eax,0h
;error ?
je
WP1_return
;-----------------------------------------------------------------------------; API "CreateFileA" creates or opens a file.
; It returns a handle that can be used to access the object.
;-----------------------------------------------------------------------------Page 10

td_win32asm_004.asm
push
0h
;hTemplateFile,
push
80h
;dwFlagsAndAttributes, 80h=normal
push
3h
;dwCreationDistribution,OPEN_EXISTING
push
0h
;lpSecurityAttributes,
push
0h
;dwShareMode,
push
80000000h
;dwDesiredAccess,GENERIC_READ
push
OFFSET LoadFileName
;lpFileName,pointer to filename
call
CreateFileA
;- API Function mov
LoadFileHandle,eax
;store handle in variable
;-----------------------------------------------------------------------------; API "GetFileSize" retrieves the size, in bytes, of the specified file.
;-----------------------------------------------------------------------------push
0h
;lpFileSizeHigh, highword file size
push
LoadFileHandle
;hFile, handle of file to get size of
call
GetFileSize
;- API Function mov
LoadFileSize,eax
;store size in variable
;-----------------------------------------------------------------------------; API "GlobalAlloc" allocates the specified number of bytes
;-----------------------------------------------------------------------------push
LoadFileSize
;dwBytes,number of bytes to allocate
push
0h
;uFlags,alloc attributes 0=GMEM_FIXED
call
GlobalAlloc
;- API Function mov
LoadMem,eax
;store memory pointer in variable
;-----------------------------------------------------------------------------; API "ReadFile" reads data from a file, starting at the position file pointer
;-----------------------------------------------------------------------------push
0h
;lpOverlapped,address data structure
push
OFFSET FileReturn
;lpNumberOfBytesRead,readed bytes
push
LoadFileSize
;nNumberOfBytesToRead,bytes to read
push
LoadMem
;lpBuffer,address buffer receives data
push
LoadFileHandle
;hFile, handle of file to read
call
ReadFile
;- API Function ;-----------------------------------------------------------------------------; API "CloseHandle" closes an open object handle.
;-----------------------------------------------------------------------------push
LoadFileHandle
;hObject, handle of object to close
call
CloseHandle
;- API Function ;-----------------------------------------------------------------------------; API "CreateFileA" creates or opens a file.
; It returns a handle that can be used to access the object.
;-----------------------------------------------------------------------------push
0h
;hTemplateFile,
push
80h
;dwFlagsAndAttributes, 80h=normal
push
2h
;dwCreationDistribution,CREATE_ALWAYS
push
0h
;lpSecurityAttributes,
push
0h
;dwShareMode,
push
40000000h
;dwDesiredAccess,GENERIC_WRITE
push
OFFSET SaveFileName
;lpFileName,pointer to filename
call
CreateFileA
;- API Function mov
SaveFileHandle,eax
;store handle in variable
;-----------------------------------------------------------------------------; API "WriteFile" writes data to a file
;-----------------------------------------------------------------------------Page 11

td_win32asm_004.asm
push
0h
;lpOverlapped,structure overlapped I/O
push
OFFSET FileReturn
;lpNumberOfBytesWritten,
push
LoadFileSize
;nNumberOfBytesToWrite, bytes to write
push
LoadMem
;lpBuffer, address data write to file
push
SaveFileHandle
;hFile, handle of file to write to
call
WriteFile
;- API Function ;-----------------------------------------------------------------------------; API "CloseHandle" closes an open object handle.
;-----------------------------------------------------------------------------push
SaveFileHandle
;hObject, handle of object to close
call
CloseHandle
;- API Function ;-----------------------------------------------------------------------------; API "GlobalFree" closes an open object handle.
;-----------------------------------------------------------------------------push
LoadMem
;hMem, handle global memory object
call
GlobalFree
;- API Function jmp
WP1_return
;==============================================================================
; WM_SYSCOMMAND (value=112h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_112h:
cmp
eax,112h
;check if WM_COMMAND message recieved
jne
WP1_return
;if not goto label
mov
eax,WP1_wParam
;extra info about the message
cmp
eax,0F060h
;SC_CLOSE=0F060h received ?
jne
WP1_return
;
call
My_CleanSystem
;- SubRoutine jmp
WP1_return
;==============================================================================
; API "DefWindowProcA" calls the window procedure to provide default processing
; for any window messages that an application does not process.
; This function ensures that every message is processed.
; It is called with the same parameters received by the window procedure.
;-----------------------------------------------------------------------------WP1_return:
popad
;pop all register from stack
push
WP1_lParam
;extra info about the message
push
WP1_wParam
;extra info about the message
push
WP1_uMsg
;the message number
push
WP1_hWnd
;handle of window who receives message
call
DefWindowProcA
;- API Function mov
esp,ebp
;delete stack frame
pop
ebp
;
ret
10h
;return and clear stack
;##############################################################################
;******************************************************************************
; My own subroutine(s) for a compacter code resist here ...
;-----------------------------------------------------------------------------My_CleanSystem:
Page 12

td_win32asm_004.asm
ret
My_MessageBox:
;-----------------------------------------------------------------------------; API "MessageBoxA" creates a message box
;-----------------------------------------------------------------------------push
0h
;uType, style, 0=MB_OK Button
push
OFFSET MB1Titel
;lpCaption,pointer to title text
push
OFFSET MB1Text
;lpText,pointer to text message box
push
WP1_hWnd
;handle of owner window 0=no owner
call
MessageBoxA
;- API Function ret
;return value IDOK=1h
;******************************************************************************
;==============================================================================
; end Main = end of our program code
;-----------------------------------------------------------------------------end Main
;end of our program code, entry point
;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;-----------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_004.asm
;asm command
; rc.exe /v rsrc.rc
;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td_win32asm_004.obj rsrc.obj
;link command
;==============================================================================

Page 13

Vous aimerez peut-être aussi