;==============================================================================
;	    Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE	310
;==============================================================================

;==============================================================================
; ==> DLL example source code !
;------------------------------------------------------------------------------
; Thanks to Arnulfo for the idea to write this ASM / DLL example.
; For a better COMPATIBILITY I break my rule and use proc and endp statements.
; If you encounter any ERROR please email me.
; OK, let's go :
; A DLL source file seems like a standard ASM file.
; Like in a standard ASM file you must use Assembler directives, include all
; necessary *.inc and *.lib files, declare all API functions, define .const,
; declare .data, define .data? and at last but not least write the .code.
; It seems, like in a Window procedure, that Windows gives you some Parameter
; on the stack !
; Next we check the reason for calling the DLL which is not necessary but
; included for a better understanding.
; After that point our functions resist. You can add more if you want.
; Don't forget to add the function name to the *.def file. 
; In this example we also use 2 parameter given from calling !
; Look to the end of this file how to create the DLL, Library and Include 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 functins 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
;------------------------------------------------------------------------------
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
MB10_Titel 		db "Message Box",0	;message box name
DllLoad			db "DLL is loaded",0	;message box text
DllUnload		db "DLL is unloaded",0	;message box text
CreateThread		db "Thread created",0	;message box text
DestroyThread		db "Thread destroyed",0	;message box text

;==============================================================================
; .Data?	= the data? area starts here, not defined and not fixed
;------------------------------------------------------------------------------
.data?

;##############################################################################
;==============================================================================
; .CODE	= our code area starts here	DllEntry = label of DLL program code
; It is called like a subroutine, so ... , we use the proc / endp statement ...
;------------------------------------------------------------------------------
.Code
DllEntry proc hinstDLL:DWORD,fdwReason:DWORD,lpvReserved:DWORD	;start of proc.
;==============================================================================
; Check reason for calling this DLL, not required for your amusement only ...
;------------------------------------------------------------------------------
cmp	fdwReason,0h			;DLL_PROCESS_DETACH = 0h
jne	reason_1h			;
;------------------------------------------------------------------------------
; API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h				;uType, style, 0=MB_OK Button
push	OFFSET MB10_Titel		;lpCaption,pointer to title text
push	OFFSET DllUnload		;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA			;- API Function -
jmp	Dll_Exit			;
reason_1h:				;
cmp	fdwReason,1h			;DLL_PROCESS_ATTACH = 1h
jne	reason_2h			;
;------------------------------------------------------------------------------
; API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h				;uType, style, 0=MB_OK Button
push	OFFSET MB10_Titel		;lpCaption,pointer to title text
push	OFFSET DllLoad			;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA			;- API Function -
jmp	Dll_Exit			;
reason_2h:				;
cmp	fdwReason,2h			;DLL_THREAD_ATTACH = 2h
jne	reason_3h			;
;------------------------------------------------------------------------------
; API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h				;uType, style, 0=MB_OK Button
push	OFFSET MB10_Titel		;lpCaption,pointer to title text
push	OFFSET CreateThread		;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA			;- API Function -
jmp	Dll_Exit			;
reason_3h:				;
cmp	fdwReason,3h			;DLL_THREAD_DETACH = 3h
jne	Dll_Exit			;
;------------------------------------------------------------------------------
; API "MessageBoxA" creates a message box, we can only click OK
;------------------------------------------------------------------------------
push	0h				;uType, style, 0=MB_OK Button
push	OFFSET MB10_Titel		;lpCaption,pointer to title text
push	OFFSET DestroyThread		;lpText,pointer to text message box
push	0h				;handle of owner window 0=no owner
call	MessageBoxA			;- API Function -

Dll_Exit:
mov	eax,1h				;set eax to true=1h, do not remove this
ret					;return to calling program !
DllEntry endp				;end of procedure

;==============================================================================
; At this place our functions resist ! Increase your knowledge and add more ...
; It is called like a subroutine, so ... , we use the proc / endp statement ...
;------------------------------------------------------------------------------
Dll_Test01 proc	para1:DWORD, para2:DWORD;start of procedure, with 2 parameter
;------------------------------------------------------------------------------
; API "MessageBoxA" creates another message box, we can only click OK
;------------------------------------------------------------------------------
push	0h             			;uType, style, 0=MB_OK Button
push	para2				;pointer message box titel (Parameter)
push	para1				;pointer message box text (Parameter)
push	0h				;handle of owner window 0=no owner
call	MessageBoxA       		;- API Function -
ret					;return to calling program !
Dll_Test01 endp				;end of procedure

;==============================================================================
; end DllEntry = end of our program code
;------------------------------------------------------------------------------
END DllEntry				;end of our program code, entry point
;##############################################################################

;==============================================================================
; To create the DLL file use this commands with your Microsoft Assembler/Linker
;------------------------------------------------------------------------------
; ml.exe /c /coff td__310.asm					;asm command
; link.exe /dll /def:td__310.def /subsystem:windows td__310.obj	;link command
; l2inca.exe /m td__310.lib					;lib to inc
;==============================================================================