home *** CD-ROM | disk | FTP | other *** search
- Page 60,190
- TITLE Win32DLL - Example Win32 DLL.
-
-
- ; CREATED: 26 Apr 97 by Philippe Auphelle
-
- ; MODIFIED: dd mmm yy by Philippe Auphelle
-
- ; ============================================================================
- ; Yes, it can be done:
- ; Sample Win32 DLL.
- ; ============================================================================
-
- .386
- .MODEL FLAT,STDCALL
-
- .SALL
- .NOLIST
-
-
- UniCode = 0 ;No Unicode, just plain ASCII.
-
-
- INCLUDE INSTR32.MAC
- INCLUDE WIN32INC.EQU
- INCLUDE KERNEL32.EQU
- .LIST
-
- DoDisplay PROTO Message:DWORD, ;Because INVOKE doesn't handle
- MessageLength:DWORD ;forward references (boo).
-
- SUBTITLE Data & Macros
- PAGE
- .CONST
-
- Display MACRO String
- INVOKE DoDisplay,
- OFFSET String,
- SIZEOF String
- ENDM
-
- ; Byte data. Always Keep separate from DWORD data to preserve alignment
- ; and save some "hole" bytes.
-
- DPAString BYTE 'Got DLL_PROCESS_ATTACH',CR,LF
- DPDString BYTE 'Got DLL_PROCESS_DETACH',CR,LF
- DTAString BYTE 'Got DLL_THREAD_ATTACH',CR,LF
- DTDString BYTE 'Got DLL_THREAD_DETACH',CR,LF
- DXXString BYTE 'Got garbage call?!',CR,LF
-
- FooString BYTE 'Hi! Foo here!',CR,LF
- BarString BYTE 'Hi! Bar here!',CR,LF
-
- .DATA
-
- hStdOut DWORD 0 ;Console output handle.
-
- SUBTITLE DLLEntryPoint routines.
- PAGE
- .CODE
-
- ; ============================================================================
- ; DLL EntryPoint functions.
- ; ============================================================================
- ;
- ; This code is called at process attach, process detach, thread attach and
- ; thread detach. The call occurs in the context of the attaching (detaching)
- ; process (thread). The reason for the call is specified in the fdwReason
- ; parameter.
- ; DLLEntryPoint must return TRUE if it's happy and FALSE if something failed.
- ; This will be passed back to the caller of the LoadLibrary function.
-
- ; When a Process Detach occurs, the DLLEntryPoint is called only once in
- ; the context of the process (rather than once for each existing thread of
- ; the process).
- ; DLLENtryPoint must be declared at link time as the entry point of the DLL
- ; (-entry when using MS Link).
- ; The fdwReason parm indicates the nature of the call.
-
- ; In the VC++ world, this more or less corresponds to the DllMain() function.
- ; The difference is that DLLMain, if there is one in the DLL code, is
- ; actually called by the compiler runtime (itself invoked by the linker
- ; -entry parm).
- ; When using MS VC, it seems that the runtime entry point is
- ; defined as _DLLMainCRTStartup$(DLLENTRY), but don't ask me more, as I
- ; don't know (and don't wish to)!
-
- ; Parameter hinstDLL identifies a handle to the DLL. According to MS docs,
- ; "this handle can be used in subsequent calls to the GetModuleFileName
- ; function and other module functions".
-
- DLLEntryPoint PROC STDCALL PUBLIC USES EBX EDI ESI,
- hinstDLL:DWORD,
- fdwReason:DWORD,
- lpReserved:DWORD
-
- ;Case EAX of:
-
- MOV EAX,fdwReason ;Get fdwReason in EAX:
- CMP EAX,DLL_PROCESS_ATTACH
- JE DLL_EP_ProcessAttach
- CMP EAX,DLL_PROCESS_DETACH
- JE DLL_EP_ProcessDetach
- CMP EAX,DLL_THREAD_ATTACH
- JE DLL_EP_ThreadAttach
- CMP EAX,DLL_THREAD_DETACH
- JE DLL_EP_ThreadDetach
-
- Display DXXString ;Don't know wotitis,
- MOV EAX,FALSE ;just return
- RET ;error condition (Owell).
-
-
- ; Process-level calls (normally get one of each, but detach sometimes doesn't
- ; happen).
-
- ; DLL attached to process. Happens at initial DLL load.
-
- DLL_EP_ProcessAttach:
- INVOKE GetStdHandle, ;Initialize console handle.
- STD_OUTPUT_HANDLE ;Get handle for screen,
- ;(might want to test retcode here...)
- MOV hStdOut,EAX ;remember it.
- Display DPAString
- MOV EAX,TRUE
- RET
-
-
- ; DLL detached from process. Happens at unload (but not always!)
-
- DLL_EP_ProcessDetach:
- Display DPDString ;Display message.
- MOV EAX,TRUE
- RET
-
-
- ; Thread-dependant instanciation.
- ; A new thread started.
-
- DLL_EP_ThreadAttach:
- Display DTAString ;Display message.
- MOV EAX,TRUE
- RET
-
-
- ; A thread just ended (in a clean way).
-
- DLL_EP_ThreadDetach:
- Display DTDString ;Display message.
- MOV EAX,TRUE
- RET
-
- UnusedParm lpReserved ;Get rid of MASM warning.
- UnusedParm hinstDLL ;Get rid of MASM warning.
-
- DLLEntryPoint ENDP
-
-
- SUBTITLE DLL functions.
- PAGE
- ; ============================================================================
- ; DLL functions.
- ; They can be anything you like...
- ; ============================================================================
-
-
- Foo PROC PUBLIC
- Display FooString ;Display who we are.
- RET
- Foo ENDP
-
-
- Bar PROC PUBLIC
- Display BarString ;Display who we are.
- RET
- Bar ENDP
-
- SUBTITLE General purpose routines.
- PAGE
- ; ============================================================================
- ; General purpose routines.
- ; ============================================================================
-
- ; Display a console message.
- ; Note that in the following, we must use ADDR rather than OFFSET:
- ; MASM doesn't take OFFSET to LOCALs.
-
- DoDisplay PROC Message:DWORD,
- MessageLength:DWORD
-
- LOCAL BWritten
-
- INVOKE WriteFile, ;Rollup one line.
- hStdOut, ;File handle for screen,
- Message, ;address of string,
- MessageLength, ;length of string,
- ADDR BWritten, ;location to return bytes written,
- 0 ;No overlapped mode.
- MOV EAX,BWritten ;Return # of bytes actually written
- RET ;in EAX.
- DoDisplay ENDP
-
- END DLLEntryPoint
-