home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / LordLucifer / win32asm / files / win32asm.exe / Win32ASM / Win32DLL / Win32DLL.asm < prev    next >
Encoding:
Assembly Source File  |  1997-11-10  |  6.5 KB  |  204 lines

  1.                 Page 60,190
  2.                 TITLE Win32DLL - Example Win32 DLL.
  3.  
  4.  
  5. ; CREATED:       26 Apr 97 by Philippe Auphelle
  6.  
  7. ; MODIFIED:      dd mmm yy by Philippe Auphelle
  8.  
  9. ; ============================================================================
  10. ; Yes, it can be done:
  11. ; Sample Win32 DLL.
  12. ; ============================================================================
  13.  
  14.                 .386
  15.                 .MODEL FLAT,STDCALL
  16.  
  17.                 .SALL
  18.                 .NOLIST
  19.  
  20.  
  21. UniCode         = 0                     ;No Unicode, just plain ASCII.
  22.  
  23.  
  24.                 INCLUDE INSTR32.MAC
  25.                 INCLUDE WIN32INC.EQU
  26.                 INCLUDE KERNEL32.EQU
  27.                 .LIST
  28.  
  29. DoDisplay       PROTO Message:DWORD,       ;Because INVOKE doesn't handle
  30.                       MessageLength:DWORD  ;forward references (boo).
  31.  
  32.                 SUBTITLE Data & Macros
  33.                 PAGE
  34.                 .CONST
  35.  
  36. Display         MACRO String
  37.                 INVOKE DoDisplay,
  38.                          OFFSET String,
  39.                          SIZEOF String
  40.                 ENDM
  41.  
  42. ; Byte data. Always Keep separate from DWORD data to preserve alignment
  43. ; and save some "hole" bytes.
  44.  
  45. DPAString       BYTE 'Got DLL_PROCESS_ATTACH',CR,LF
  46. DPDString       BYTE 'Got DLL_PROCESS_DETACH',CR,LF
  47. DTAString       BYTE 'Got DLL_THREAD_ATTACH',CR,LF
  48. DTDString       BYTE 'Got DLL_THREAD_DETACH',CR,LF
  49. DXXString       BYTE 'Got garbage call?!',CR,LF
  50.  
  51. FooString       BYTE 'Hi! Foo here!',CR,LF
  52. BarString       BYTE 'Hi! Bar here!',CR,LF
  53.  
  54.                 .DATA
  55.  
  56. hStdOut         DWORD 0                 ;Console output handle.
  57.  
  58.                 SUBTITLE DLLEntryPoint routines.
  59.                 PAGE
  60.                 .CODE
  61.  
  62. ; ============================================================================
  63. ; DLL EntryPoint functions.
  64. ; ============================================================================
  65. ;
  66. ; This code is called at process attach, process detach, thread attach and
  67. ; thread detach. The call occurs in the context of the attaching (detaching)
  68. ; process (thread). The reason for the call is specified in the fdwReason
  69. ; parameter.
  70. ; DLLEntryPoint must return TRUE if it's happy and FALSE if something failed.
  71. ; This will be passed back to the caller of the LoadLibrary function.
  72.  
  73. ; When a Process Detach occurs, the DLLEntryPoint is called only once in
  74. ; the context of the process (rather than once for each existing thread of
  75. ; the process).
  76. ; DLLENtryPoint must be declared at link time as the entry point of the DLL
  77. ; (-entry when using MS Link).
  78. ; The fdwReason parm indicates the nature of the call.
  79.  
  80. ; In the VC++ world, this more or less corresponds to the DllMain() function.
  81. ; The difference is that DLLMain, if there is one in the DLL code, is
  82. ; actually called by the compiler runtime (itself invoked by the linker
  83. ; -entry parm).
  84. ; When using MS VC, it seems that the runtime entry point is
  85. ; defined as _DLLMainCRTStartup$(DLLENTRY), but don't ask me more, as I
  86. ; don't know (and don't wish to)!
  87.  
  88. ; Parameter hinstDLL identifies a handle to the DLL. According to MS docs,
  89. ; "this handle can be used in subsequent calls to the GetModuleFileName
  90. ; function and other module functions".
  91.  
  92. DLLEntryPoint PROC STDCALL PUBLIC USES EBX EDI ESI,
  93.                 hinstDLL:DWORD,
  94.                 fdwReason:DWORD,
  95.                 lpReserved:DWORD
  96.  
  97.     ;Case EAX of:
  98.  
  99.     MOV EAX,fdwReason                   ;Get fdwReason in EAX:
  100.     CMP EAX,DLL_PROCESS_ATTACH
  101.     JE DLL_EP_ProcessAttach
  102.     CMP EAX,DLL_PROCESS_DETACH
  103.     JE DLL_EP_ProcessDetach
  104.     CMP EAX,DLL_THREAD_ATTACH
  105.     JE DLL_EP_ThreadAttach
  106.     CMP EAX,DLL_THREAD_DETACH
  107.     JE DLL_EP_ThreadDetach
  108.  
  109.     Display DXXString                   ;Don't know wotitis,
  110.     MOV EAX,FALSE                       ;just return
  111.     RET                                 ;error condition (Owell).
  112.  
  113.  
  114. ; Process-level calls (normally get one of each, but detach sometimes doesn't
  115. ; happen).
  116.  
  117. ; DLL attached to process. Happens at initial DLL load.
  118.  
  119. DLL_EP_ProcessAttach:
  120.     INVOKE GetStdHandle,                ;Initialize console handle.
  121.         STD_OUTPUT_HANDLE               ;Get handle for screen,
  122.                                         ;(might want to test retcode here...)
  123.     MOV hStdOut,EAX                     ;remember it.
  124.     Display DPAString
  125.     MOV EAX,TRUE
  126.     RET
  127.  
  128.  
  129. ; DLL detached from process. Happens at unload (but not always!)
  130.  
  131. DLL_EP_ProcessDetach:
  132.     Display DPDString                   ;Display message.
  133.     MOV EAX,TRUE
  134.     RET
  135.  
  136.  
  137. ; Thread-dependant instanciation.
  138. ; A new thread started.
  139.  
  140. DLL_EP_ThreadAttach:
  141.     Display DTAString                   ;Display message.
  142.     MOV EAX,TRUE
  143.     RET
  144.  
  145.  
  146. ; A thread just ended (in a clean way).
  147.  
  148. DLL_EP_ThreadDetach:
  149.     Display DTDString                   ;Display message.
  150.     MOV EAX,TRUE
  151.     RET
  152.  
  153.     UnusedParm lpReserved               ;Get rid of MASM warning.
  154.     UnusedParm hinstDLL                 ;Get rid of MASM warning.
  155.  
  156. DLLEntryPoint ENDP
  157.  
  158.  
  159.     SUBTITLE DLL functions.
  160.     PAGE
  161. ; ============================================================================
  162. ; DLL functions.
  163. ; They can be anything you like...
  164. ; ============================================================================
  165.  
  166.  
  167. Foo PROC PUBLIC
  168.     Display FooString                   ;Display who we are.
  169.     RET
  170. Foo ENDP
  171.  
  172.  
  173. Bar PROC PUBLIC
  174.     Display BarString                   ;Display who we are.
  175.     RET
  176. Bar ENDP
  177.  
  178.     SUBTITLE General purpose routines.
  179.     PAGE
  180. ; ============================================================================
  181. ; General purpose  routines.
  182. ; ============================================================================
  183.  
  184. ; Display a console message.
  185. ; Note that in the following, we must use ADDR rather than OFFSET:
  186. ; MASM doesn't take OFFSET to LOCALs.
  187.  
  188. DoDisplay PROC Message:DWORD,
  189.                MessageLength:DWORD
  190.  
  191.           LOCAL BWritten
  192.  
  193.     INVOKE WriteFile,                   ;Rollup one line.
  194.         hStdOut,                        ;File handle for screen,
  195.         Message,                        ;address of string,
  196.         MessageLength,                  ;length of string,
  197.         ADDR BWritten,                  ;location to return bytes written,
  198.         0                               ;No overlapped mode.
  199.     MOV EAX,BWritten                    ;Return # of bytes actually written
  200.     RET                                 ;in EAX.
  201. DoDisplay ENDP
  202.  
  203.     END DLLEntryPoint
  204.