home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Iczelion / beepserv.txt < prev    next >
Text File  |  2000-05-25  |  7KB  |  188 lines

  1.  
  2. TUTORIAL FOR ASM WINDOWS NT SERVICE:
  3.  
  4.      Many people are familiar with device drivers and their
  5. advantage at being autostarted by the system and getting
  6. Ring 0 privilege.  But with that privilege comes complexity
  7. in both the planning and development phase.  Microsoft's
  8. tendency to be less than forthcoming with information makes
  9. the task all the harder.  By contrast many of us are
  10. familiar with programming at Ring 3 and the documentation of
  11. the Win32 API is extensive.  Wouldn't it be nice to combine
  12. the characteristics of a driver with the familiarity of
  13. regular Ring 3 Apps.  Services are Microsoft's attempt to
  14. create such a beast.  They gain the advantage of being
  15. autostarted by the system while keeping the familiarity of
  16. normal Ring 3 programming.
  17.  
  18.      NOTE: I am restricting this discussion to NT because
  19. services under Win95/98 are an afterthought.  The support
  20. consists fo a registry entry under (
  21. HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersio
  22. n\RunServices ) and one API function RegisterServiceProcess.
  23. Creating a Win95 version of a service is left as an exercise
  24. for the reader.
  25.  
  26. INSTALLING THE SERVICE:
  27.  
  28. I have included a program to install and remove a
  29. service from the SCM Database.  A step which is necessary
  30. before a service can be run.
  31.  
  32. To install the service:
  33.    tool /i beepserv "ASM Beepservice" path\beepserv.exe
  34.    Then goto the service applet of the Control panel and
  35. find the ASM Beepservice click on it and it should start.
  36.  
  37. To remove the service:
  38.    tool /r beepserv
  39.  
  40.  
  41. INTRO:
  42.  
  43.      Services are simply normal programs with two threads.
  44. One thread is the worker which performs whatever actions you
  45. desire.  The second is there to communicate with the OS,
  46. which will inform it when to Start, Stop, Pause,
  47. Initialize, and Terminate.  Just like any other program a
  48. Service has an entry point here named start but it could be
  49. the infamous main.
  50.  
  51.  
  52. ;********************************************************
  53. .code
  54. start:
  55.    ;Register with the SCM
  56.    mov  sTable.lpServiceProc, offset ServiceMain
  57.    LOAD sTable.lpServiceName, offset SERVICE_NAME
  58.    INVOKE StartServiceCtrlDispatcher, ADDR sTable
  59.  
  60.    .IF eax == 0
  61.       INVOKE ErrorHandler, ADDR ERROR_MESSAGE
  62.    .ENDIF
  63.  
  64.    INVOKE ExitProcess, eax
  65.  
  66. ;********************************************************
  67.  
  68.  
  69. This snippet of code does nothing but call
  70. StartServiceCtrlDispatcher and Exit.  So where is the
  71. Service?  Hidden behind StartServiceCtrlDispatcher.
  72. Basically start calls this function and does not
  73. return until the System tells the Service to terminate or
  74. there is a fatal error within the Service and it is
  75. terminated by the SCM.  When the SCM receives this call it
  76. registers the Service with name SERVICE_NAME and associates
  77. it with the function ServiceMain.  The SCM then calls the
  78. ServiceMain function (the service's entry point).
  79.  
  80.  
  81. SERVICEMAIN:
  82.  
  83. The ServiceMain function does a number of things and here it
  84. is in stripped down form.
  85.  
  86. ;********************************************************
  87. ServiceMain proc argc:DWORD, argv:DWORD
  88.    LOCAL success:BOOL
  89.    LOCAL temp:DWORD
  90.  
  91.    ;immediately call Registration function
  92.    INVOKE RegisterServiceCtrlHandler, ADDR SERVICE_NAME,  CtrlHandler
  93.  
  94.    ;Notify SCM of progress
  95.    INVOKE SendStatus, SERVICE_START_PENDING, NO_ERROR, 0, 1, 5000
  96.  
  97.    ;create the termination event
  98.    INVOKE CreateEvent, 0, TRUE, FALSE, 0
  99.  
  100.    ;Notify SCM of progress
  101.    INVOKE SendStatus, SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000
  102.  
  103.    ;Notify SCM of progress
  104.    INVOKE SendStatus, SERVICE_START_PENDING, NO_ERROR, 0, 3, 5000
  105.  
  106.    ;Start the service itself
  107.    call Init
  108.  
  109.    ;Notify SCM of progress
  110.    INVOKE SendStatus, SERVICE_RUNNING, NO_ERROR, 0, 0, 0
  111.  
  112.    ;Wait for stop signal, and then terminate
  113.    INVOKE WaitForSingleObject, evTerminate, INFINITE
  114.  
  115.    push 0
  116.    call terminate
  117.    ret
  118. ServiceMain endp
  119. ;********************************************************
  120.  
  121. This function is responsible for registration and
  122. initialization.  The first thing it does is register the
  123. Service Control Handler.  This is essentially a dispatch
  124. routine which receives and responds to request by the SCM to
  125. start, stop, pause, terminate, and tell me a little about
  126. yourself.  The next thing you see is a call to SendStatus
  127. (many calls to it actually).  All this function does is tell
  128. the SCM that the service is still running, is step n in its
  129. initialization, informs the SCM of what the status of the
  130. service is, and that it expects to send its status again
  131. within so many milliseconds ( 2000-5000 in the example above
  132. ).  The next thing that is done is to create an Event.  The
  133. purpose the Event is to prevent the ServiceMain from
  134. terminating (Notice the WaitForSingleObject call ) until the
  135. Event gets set.  The Event gets set in the Stop function
  136. which is called by the CtrlHandler.
  137.  
  138.  
  139. CTRLHANDLER:
  140.  
  141. The next function to examine is the CtrlHandler function.
  142. The CtrlHandler function is the interface to the SCM and
  143. behaves just like the familiar Message handling procedure in
  144. Windows.  The CtrlHandler function is under some
  145. restrictions as to how it can behave and how long it has to
  146. respond to the SCM.  Here are the various notes and rules.
  147.  
  148. 1) Must accept and process the SERVICE_CONTROL_INTERROGATE
  149. control code.
  150.  
  151. 2) Process messages in less than 30 seconds.
  152.  
  153. 3) After receiving the SERVICE_CONTROL_SHUTDOWN control code
  154. the service has 20 seconds ( see 5 below ) before the system
  155. shuts down.
  156.  
  157. 4) Services continue to run even after the Restart Dialog
  158. box appears, but there is no system.  Hmmmm walk lightly
  159. carry and exception at this point.
  160.  
  161. 5) There is a registry key under
  162. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control with a
  163. value WaitToKillServiceTimeout which specifies how long a
  164. service has after receiving the SERVICE_CONTROL_SHUTDOWN
  165. control code the default is 20 sec.
  166.  
  167. Other than the above rules the CtrlHandler is also
  168. responsible for performing any actions necessary to respond
  169. to the control codes sent to it.
  170.  
  171.  
  172. NOTES:
  173.  
  174.      Strangely by default services run under their own desktop.
  175. This is controled through the SERVICE_INTERACTIVE_PROCESS
  176. flag to the dwServiceType parameter of the CreateService
  177. function.  Unless this flag is set the service cannot
  178. interact with the users desktop.  This means no GUI no
  179. dialog boxes.  Only the MessageBox function will work and
  180. only with the MB_SERVICE_NOTIFICATION flag set.  This can be
  181. a source of great frustration (personal experience here).
  182.  
  183.      That about sums it up.  Enjoy having this useful
  184. technique at your disposal.
  185.                               
  186.                               
  187.                          Cynical Pinnacle
  188.