index based loading. Based on testdll.asm from MASM32\example1\DLL\ original LibMain proc

LibMain proc hInstDLL:DWORD, reason:DWORD, unused:DWORD

            szText LmTitle,"tstdll's LibMain Function"

        .if reason == DLL_PROCESS_ATTACH
            szText ATTACHPROCESS,"PROCESS_ATTACH"
            invoke MessageBox,NULL,ADDR ATTACHPROCESS,addr LmTitle,MB_OK

            return TRUE
            ; -----------------------------
            ; If error at startup, return 0
            ; System will abort loading DLL
            ; -----------------------------

        .elseif reason == DLL_PROCESS_DETACH
            szText DETACHPROCESS,"PROCESS_DETACH"
            invoke MessageBox,NULL,addr DETACHPROCESS,addr LmTitle,MB_OK

        .elseif reason == DLL_THREAD_ATTACH
            szText ATTACHTHREAD,"THREAD_ATTACH"
            invoke MessageBox,NULL,addr ATTACHTHREAD,addr LmTitle,MB_OK

        .elseif reason == DLL_THREAD_DETACH
            szText DETACHTHREAD,"THREAD_DETACH"
            invoke MessageBox,NULL,addr DETACHTHREAD,addr LmTitle,MB_OK
            
        .endif

        ret

LibMain Endp

------------------------------------
New proc superfast:
;############################################################################
;index based loading. very powerfull tecnique
;to give you an example how to handle more
;complicated code in resonse of different "reason" in Main proc of DLL
;please write this code in "classic" style and post
;I'll rewrite it with inxed based loading wich is uncompareble to
;usual switch tecniques produced by compilers. Though index based loading
;can not be apply anywhere there are lot of situations when we can
;create uninterruptive arrays. In such a case it doesn't matter
;how many cases we have - code will find the case code instantly without
;branching.

LibMain proc hInstDLL:DWORD, reason:DWORD, unused:DWORD
;DLL_PROCESS_ATTACH equ 1    
;DLL_THREAD_ATTACH equ 2    
;DLL_THREAD_DETACH equ 3    
;DLL_PROCESS_DETACH equ 0    
.data
LmTitle 	db "tstdll is LibMain Function",0
ATTACHPROCESS db "PROCESS_ATTACH",0
DETACHPROCESS db "PROCESS_DETACH",0
ATTACHTHREAD db "THREAD_ATTACH",0
DETACHTHREAD db "THREAD_DETACH",0
ALIGN 4
msgtbl 	dd offset DETACHPROCESS
	dd offset ATTACHPROCESS
	dd offset ATTACHTHREAD
	dd offset DETACHTHREAD
.code
	mov eax,reason
	mov eax,+msgtbl
	invoke MessageBox,0,eax,offset LmTitle,0
	mov eax,1
	ret
LibMain Endp
Posted on 2001-04-03 08:59:00 by The Svin
Good point. This is a form of array indexing where msgtbl is an assay of pointers and reason is the index. I have three tips/suggestions: 1) you don't need to specify offset in data declarations. Masm knows you wnat the offset msg1 dd offset message1 is the same as msg1 dd message1 ; less typeing 2) You can skip the line mov eax,+msgtbl and just say invoke MessageBox, 0, msgtbl, offset LmTitle, 0 3) For more general cases where the element size is not dword such as: tbtbl TBBUTTON 30 dup(?) ; an array of 30 TBBUTTONS each is 20 bytes you must multiply the index here by 20 - the cpu can't do this. But you can do this: imul eax, index, type tbtbl ; TYPE operator returns element size now eax points to the index'th TBBUTTON which is what we wanted. Of course index must be a dword of value less than or eual to 0ffffh - ie. hiword = 0. gfalen
Posted on 2001-04-03 12:10:00 by gfalen
push msgtbl will make code 1clocks slower, though 1 byte shorter. push memory = 2clocks and NP. push reg 1 clock and UV P. has size 1 byte
Posted on 2001-04-03 14:55:00 by The Svin