;==============================================================================
;	    Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE	312
;==============================================================================

;==============================================================================
; ==> ASM example calling a function inside a DLL directly ( explicit ) !
;------------------------------------------------------------------------------
; Thanks to Arnulfo for the idea to write this ASM / DLL example.
; If you encounter any ERROR please email me.
; OK, let's go :
; Because we are focussed to learn the DLL stuff here is no Main Window.
; This source code is like a standard asm file.
; API GetModuleHandleA gets our program ID.
; API LoadLibraryA loads our created DLL into memory.
; We check if an ERROR occured while loading and react with a message box.
; API GetProcAddress get the address of the specified function in the DLL.
; We use this pointer to call the function in the DLL.
; For testing purpose we also push two parameter !
; API FreeLibrary unmaps the modul from address space of the calling process.
; API=ExitProcess terminates our program.
; Look to the end of this file how to create the EXE file.

;==============================================================================
; 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 functions resist you want use, set correct path !
;------------------------------------------------------------------------------
include \masm32\include\windows.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;------------------------------------------------------------------------------
GetModuleHandleA	PROTO :DWORD
LoadLibraryA		PROTO :DWORD
GetProcAddress		PROTO :DWORD,:DWORD
FreeLibrary 		PROTO :DWORD
ExitProcess		PROTO :DWORD
MessageBoxA		PROTO :DWORD,:DWORD,:DWORD,:DWORD

;==============================================================================
; .const 	= the constants area starts here, constants are defined & fixed
;------------------------------------------------------------------------------
.const

;==============================================================================
; .Data		= the data area starts here, datas are defined but not fixed
;------------------------------------------------------------------------------
.Data
Dll_Function_Parameter1	db "inside a DLL, the pointer to this text",13,10;
			db "is given to the DLL as a parameter.",0;
Dll_Function_Parameter2	db "Message Box inside DLL",0;
Library_Name		db "td__310.dll",0	;filename of the library
Function_Name		db "Dll_Test01",0	;function name inside library
MB1Titel 		db "Message Box",0	;message box name
DLL_error		db "DLL not found",0	;can not find/load DLL
Function_error		db "Function not found",0;can't find/load function
hInstance		dd 0			;our program handle
hLibrary		dd 0			;our library handle
fPointer		dd 0			;pointer to choosen function

;==============================================================================
; .Data?	= the data? area starts here, not defined and not fixed
;------------------------------------------------------------------------------
.data?

;==============================================================================
; .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

;==============================================================================
; API LoadLibraryA maps the specified exe or dll module into the address space
; of the calling process.
;------------------------------------------------------------------------------
push	OFFSET Library_Name		;lpLibFileName, pointer filename module
call	LoadLibraryA			;- API Function -
cmp	eax,0h				;check if return value 0h=ERROR
jne	Library_OK			;if no error goto LABEL
;------------------------------------------------------------------------------
; On ERROR API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h             			;uType, style, 0=MB_OK Button
push	OFFSET MB1Titel			;lpCaption,pointer to title text
push	OFFSET DLL_error             	;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA       		;- API Function -
jmp	ExitPrg				;library not loaded, error on lib call
					;library not loaded, exit program (!)

Library_OK:				;LABEL
mov	hLibrary,eax			;store handle of library in variable
;------------------------------------------------------------------------------
; API "GetProcAddress" gets the address of the specified function
;------------------------------------------------------------------------------
push	OFFSET Function_Name		;pProcName, name of function
push	hLibrary			;hModule, handle to DLL module
call	GetProcAddress			;- API Function -
cmp	eax,0h				;check if return value 0h=ERROR
jne	Function_OK			;if no error goto LABEL
;------------------------------------------------------------------------------
; On ERROR API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h             			;uType, style, 0=MB_OK Button
push	OFFSET MB1Titel			;lpCaption,pointer to title text
push	OFFSET Function_error		;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA       		;- API Function -
jmp	FreeLib				;library loaded, error on function call
					;free library (!) before exit (!)

Function_OK:				;LABEL
mov	fPointer,eax			;store given pointer to the function
push	OFFSET Dll_Function_Parameter2	;let's test if we can throw a parameter 
push	OFFSET Dll_Function_Parameter1	;let's test if we can throw a parameter 
call	[fPointer]			;call function inside the DLL !

;------------------------------------------------------------------------------
; API FreeLibrary unmaps the modul from address space of the calling process
; Free the library if loaded (!) even if the called function not exist (!)
;------------------------------------------------------------------------------
FreeLib:				;
push	hLibrary			;hLibModule, handle loaded lib. module
call	FreeLibrary			;- API Function -

;==============================================================================
; Next we terminate our program (API=ExitProcess)
;------------------------------------------------------------------------------
ExitPrg:
push	hInstance			;push our programm handle to exit
call	ExitProcess			;- API Function -

;==============================================================================
; 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__312.asm					;asm command
; link.exe /subsystem:windows td__312.obj			;link command
;==============================================================================