home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / compmgmt.cab / scdev.txt < prev    next >
Text File  |  1999-08-05  |  18KB  |  335 lines

  1. USING SC.EXE TO DEVELOP WINDOWS NT SERVICES  
  2. January 1995
  3.  
  4. (From MSDN Library, July 1996)
  5.  
  6. A little-known command-line utility, SC.EXE, can help you develop your services for Microsoft« Windows NT«. SC.EXE, which is provided in the Win32« Software Development Kit (SDK) MSTOOLS directory, implements calls to all of the Windows NT service control application programming interface (API) functions. You can set the parameters to these functions by specifying them on the command line. SC.EXE also displays service status and retrieves the values stored in the status structure fields. The utility also lets you specify the name of a remote computer so that you can call the service API functions or view the service status structures on the remote computer.
  7.  
  8. This article describes how to use the SC.EXE utility to obtain detailed information about Windows NT services. For more information about developing Windows NT services and for detailed reference information about the Windows NT Service API functions, such as ControlService and QueryServiceStatus, see the Win32 SDK documentation (Development Library, Product Documentation, SDKs).
  9.  
  10. SC.EXE is a development tool that provides more detailed and accurate information about services than the two end-user utilities that are provided with the operating system. The Services Control Panel application and the network command-line interface, NET.EXE, can tell you that a service is running, stopped, or paused. These tools are fine for completely debugged and running services when everything is going smoothly. But when things go wrong, as sometimes happens when developing new code, the information provided by these tools can be misleading.
  11.  
  12. For instance, if during the development stage your service hangs in the start-pending state, the Control Panel and NET.EXE report its state as running. If the service hangs when in the stop-pending state, NET START reports the status as running, and the Control Panel reports it as stopped. If you then attempt to start it, the Control Panel tells you the service is already running. This can be quite confusing.
  13.  
  14. SC.EXE lets you query the service status and retrieve the values stored in the status structure fields. The NET.EXE program and the Services Control Panel application don't provide the complete status for the service. The SC program, however, will tell you the exact state of the service as well as show you the last checkpoint number and wait hint. The checkpoint can then be used as a debugging tool because it provides a clear indication of how far along the initialization had progressed before the program froze.
  15.  
  16. SC.EXE also allows you to call any of the service control API functions and vary any of the parameters from the command line. This offers several advantages to the service developer. For instance, it provides a convenient way of creating or configuring the service information in the Registry and the Service Control Manager's database. The developer doesn't have to configure the service by manually creating entries in the Registry and then rebooting the machine in order to force the Service Control Manager to update its database.
  17.  
  18. As a command line program, SC.EXE can also be used to create tests for your service. You can create batch (command) files that call SC.EXE with various parameters that control the service. This is useful if you want to see how your service behaves when it is repeatedly started and stopped. If you have more than one service in your service process, you can leave one service running so that the process doesn't go away, and then repeatedly start and stop the other service while looking for evidence of memory leaks due to an incomplete cleanup.
  19. The following sections contain reference information for the commands SC, SC QC, and SC QUERY.
  20.  
  21. SC
  22.  
  23. The SC command-line utility uses the following syntax:
  24.  
  25. Syntax1
  26. sc [Servername] Command Servicename [Optionname= Optionvalue...]
  27.  
  28. Syntax2
  29. sc [Command]
  30.  
  31. Use Syntax1 to run SC.EXE. Use Syntax2 to display help information (except for the "query" command--see the "Comments" section below for more information).
  32.  
  33. Parameters
  34.  
  35. Servername
  36. Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two double backslash characters, such as "\\myserver". To run SC on the local computer, do not supply this parameter.
  37.  
  38. Command
  39. Specifies the SC command. Note that many of the SC commands require administrative privilege on the specified computer. SC supports the following commands:
  40.   
  41. Command    Description
  42.  
  43. config        Changes the configuration of a service (persistent).
  44.  
  45. continue    Sends a CONTINUE control request to a service.
  46.  
  47. control        Sends a control to a service.
  48.  
  49. create        Creates a service (adds it to the registry).
  50.  
  51. delete        Deletes a service (from the registry).
  52.  
  53. EnumDepend    Enumerates service dependencies.
  54.  
  55. GetDisplayName    Gets the DisplayName for a service.
  56.  
  57. GetKeyName    Gets the ServiceKeyName for a service.
  58.  
  59. interrogate    Sends an INTERROGATE control request to a service.
  60.  
  61. pause        Sends a PAUSE control request to a service.
  62.  
  63. qc        Queries configuration for the service. For detailed information, see the reference section, "SC QC."
  64.  
  65. query        Queries the status for a service, or enumerates the status for types of services. For detailed information, see the reference section, "SC QUERY."
  66.  
  67. start        Starts a service.
  68.  
  69. stop        Sends a STOP request to a service.
  70.  
  71. Servicename
  72. Specifies the name given to the service key in the registry. Note that this is different from the display name, which is what you see with NET START and the Services Control Panel application. SC uses the service key name as the primary identifier for the service.
  73.  
  74. Optionname
  75. The Optionname and Optionvalue parameters allow you to specify the names and values of optional command parameters. Note that there is no space between the Optionname and the equal sign. You can supply zero, one, or more optional parameters name and value pairs.
  76.  
  77. For a list of the available Optionname values, request help for the command by entering "sc Command", where Command is a valid SC command name, as listed in the Command parameter description above.
  78.  
  79. Optionvalue
  80. Specifies the value for the parameter named by Optionname. The range of valid values is often restricted for each Optionname. For a list of available values, request help for each command. 
  81.  
  82. Comments
  83.  
  84. Many of the commands require administrator privileges, so it's a good idea to make sure that you are an administrator of the machine where the development is being done.
  85.  
  86. When you enter "SC" with no parameters, SC.EXE displays help information that lists the available commands. When you enter "SC" followed by a command name, you can get specific information about that command. For example, enter "SC CREATE" to get help specific to the CREATE command. 
  87.  
  88. The exception to this syntax is "SC QUERY", which dumps the status of all services and drivers currently running in the system. For help on the QUERY command, enter "SC", and respond to the prompt, "Would you like to see help for the QUERY command?" by entering "y" for yes.
  89.  
  90. When you use the START command, you can pass arguments to the service's main function. Note that the arguments are not passed to the service process's main function.
  91.  
  92. SC CREATE
  93.  
  94. The SC CREATE command creates an entry for the service in the Registry and in the Service Control Manager's database.
  95.  
  96. Syntax1
  97. sc [Servername] create Servicename [Optionname=Optionvalue...]
  98.  
  99. Parameters
  100.  
  101. Servername
  102. Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two double backslash characters, such as "\\myserver". To run SC on the local computer, do not supply this parameter.
  103.  
  104. Servicename
  105. Specifies the name given to the service key in the Registry. Note that this is different from the display name, which is what you see with NET START and the Services Control Panel application. SC uses the service key name as the primary identifier for the service.
  106.  
  107. Optionname
  108. The Optionname and Optionvalue parameters allow you to specify the names and values of optional parameters. Note that there is no space between the Optionname and the equal sign. You can supply zero, one, or more optional parameters name and value pairs. The SC QUERY command supports the following values:
  109.   
  110. Optionname    Optionvalue
  111. Description
  112.  
  113. type=        own, share, interact, kernel, filesys    
  114. Type of service being created. Optionvalues include types used by drivers. (default = share)
  115.  
  116. start=        boot, system, auto, demand, disabled
  117. Start type for the service. Option values include types used by drivers. (default = demand)
  118.  
  119. error=        normal, severe, critical, ignore
  120. Severity of error if the service fails to start during boot. (default = normal)
  121.  
  122. binPath=    (string)    
  123. Path name to the service binary file. There is no default for this. This string must be supplied.
  124.  
  125. group=        (string)
  126. Name of group which this service is a member of. The list of groups are stored in the Registry under ServiceGroupOrder. (default = nothing)
  127.  
  128. tag=        (string)
  129. If this string is set to "yes", SC will obtain a TagId from the CreateService call. However, SC does not display the tag so it's pointless to use this. (default = nothing)
  130.  
  131. depend=        (space separated string)
  132. Names of services or groups which must start before this service.
  133.  
  134. obj=        (string)
  135. Name of account in which the service will run. For drivers, this is the NT driver object name. (default = LocalSystem)
  136.  
  137. DisplayName=    (string)    
  138. A string that can be used by user-interface programs to identify the service.
  139.  
  140. password=    (string)
  141. A password string. This is required if an account other than LocalSystem is used.
  142.  
  143. Optionvalue
  144. Specifies the value for the parameter named by Optionname. See the Optionname reference for a list of supported values. When a string is to be input, the use of empty quotes means that an empty string will be passed in.
  145.  
  146. Comments
  147.  
  148. The SC CREATE command performs the operations of the CreateService API function. 
  149.  
  150. Example 1
  151.  
  152. The following example creates a registry entry for the service named "NewService" on the computer called "\\myserver":
  153.   
  154. sc \\myserver create NewService binpath= c:\nt\system32\NewServ.exe
  155.   
  156. By default this service will be created as a WIN32_SHARE_PROCESS with a SERVICE_DEMAND_START start-type. It will not have any dependencies, and will run in the LocalSystem security context.
  157.  
  158. Example 2
  159.  
  160. The following example creates the service on the local computer as an auto-start service that runs in its own process. It has dependencies on the TDI group and on the NetBIOS service. Notice that you must add quotes around the list of space-separated dependencies.
  161.   
  162. sc create NewService binpath= c:\nt\system32\NewServ.exe type= own 
  163.  start= auto depend= "+TDI Netbios"
  164.   
  165. Example 3
  166.  
  167. The service developer can run the service in the context of the kernel debugger by temporarily changing the binary path (image path) for the service. The following example shows how to call SC to change the service configuration:
  168.   
  169. sc config NewService binpath= "ntsd -d c:\nt\system32\NewServ.exe"
  170.   
  171. This example causes the Service Control Manager to invoke NTSD.EXE with the following argument string:
  172.   
  173. "-d c:\nt\system32\NewServ.exe".
  174.   
  175. NTSD will in turn break on the debugger when it loads NEWSERV.EXE so that breakpoints can be set in the service code.
  176. SC QC
  177.  
  178. The SC QC "query configuration" command lists information about the service configuration from the QUERY_SERVICE_CONFIG structure.
  179.  
  180. Syntax1
  181. sc [Servername] qc Servicename [Buffersize]
  182.  
  183. Parameters
  184.  
  185. Servername
  186. Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two double backslash characters, such as "\\myserver". To run SC on the local computer, do not supply this parameter.
  187.  
  188. Servicename
  189. Specifies the name given to the service key in the registry. Note that this is different from the display name, which is what you see with NET START and the Services Control Panel application. SC uses the service key name as the primary identifier for the service.
  190.  
  191. Buffersize
  192. Optional. Specifies the size of the buffer in bytes. 
  193.  
  194. Comments
  195.  
  196. The SC QC command displays the contents of the QUERY_SERVICE_CONFIG structure:
  197.   
  198. Information displayed by the SC QC command    Corresponding field from the QUERY_SERVICE_CONFIG structure
  199.  
  200. TYPE            dwServiceType
  201. START_TYPE        dwStartType
  202. ERROR_CONTROL        dwErrorControl
  203. BINARY_PATH_NAME    lpBinaryPathName
  204. LOAD_ORDER_GROUP    lpLoadOrderGroup
  205. TAG            dwTagId
  206. DISPLAY_NAME        lpDisplayName
  207. DEPENDENCIES        lpDependencies
  208. SERVICE_START_NAME    lpServiceStartName
  209.  
  210. Example 1
  211.  
  212. The following example queries the configuration of the service named "NewService" that was created in Example 1 for the Create command:
  213.   
  214. sc \\myserver qc NewService
  215.   
  216. The SC utility displays the following information:
  217.   
  218. SERVICE_NAME: NewService
  219.         TYPE               : 20  WIN32_SHARE_PROCESS
  220.         START_TYPE         : 3   DEMAND_START
  221.         ERROR_CONTROL      : 1   NORMAL
  222.         BINARY_PATH_NAME   : c:\nt\system32\NewServ.exe
  223.         LOAD_ORDER_GROUP   :
  224.         TAG                : 0
  225.         DISPLAY_NAME       : NewService
  226.         DEPENDENCIES       :
  227.         SERVICE_START_NAME : LocalSystem
  228.   
  229. NewService has the ability to share a process with other services. It will not be auto-started. The binary file name is NEWSERV.EXE. This service doesn't depend on any other services, and will run in the Local System security context. Because this basically returns the results from a call to QueryServiceStatus, a more detailed explanation of these results can be obtained from the documentation on that API function.
  230.  
  231. SC QUERY
  232.  
  233. The SC QUERY command obtains information about the service.
  234.  
  235. Syntax
  236. sc [Servername] query { Servicename | Optionname= Optionvalue... }
  237.  
  238. Parameters
  239.  
  240. Servername
  241. Optional. Specifies the name of the server when you want to run the command on a remote computer. The name must start with two double backslash characters, such as "\\myserver".
  242.  
  243. Servicename
  244. Specifies the name given to the service key in the Registry. Note that this is different from the display name, which is what you see with NET START and the Services Control Panel application. SC uses the service key name as the primary identifier for the service. Note that you can supply either the Servicename or the options, but not both. When the Servicename is supplied, the other options are ignored.
  245.  
  246. Optionname
  247. The Optionname and Optionvalue parameters allow you to specify the names and values of optional parameters. Note that there is no space between the Optionname and the equal sign. You can supply zero, one, or more optional parameters name and value pairs. The SC QUERY command supports the following values:
  248.   
  249. Optionname    Optionvalue
  250. Description
  251.  
  252. type=        driver, service, all    
  253. Type of services to enumerate (default = service)
  254.  
  255. state=        active, inactive, all
  256. State of services to enumerate  (default = active)
  257.  
  258. bufsize=    (numeric value)
  259. The size in bytes of the enumeration buffer (default = 1024 bytes)
  260.  
  261. ri=        (numeric value)
  262. The resume index number at which to begin the enumeration (default = 0)
  263.  
  264. Optionvalue
  265. Specifies the value for the parameter named by Optionname. See the Optionname reference for a list of supported values.
  266.  
  267. Comments
  268.  
  269. The SC QUERY command displays the contents of the SERVICE_STATUS structure:
  270.   
  271. Information displayed by SC QUERY command    Corresponding field from the SERVICE_STATUS structure
  272.  
  273. TYPE            dwServiceType
  274. STATE            dwCurrentState, dwControlsAccepted
  275. WIN32_EXIT_CODE        dwWin32ExitCode
  276. SERVICE_EXIT_CODE    dwServiceSpecificExitCode
  277. CHECKPOINT        dwCheckPoint
  278. WAIT_HINT        dwWaitHint
  279.  
  280. Using the SC QUERY command after booting the machine will tell you whether or not an attempt was made to start this service. If the service was started successfully, the WIN32_EXIT_CODE field should contain a zero (0). If the service failed to start when an attempt was made, this field should contain an exit code provided by the service when it realized it couldn't start.
  281.  
  282. Examples
  283.  
  284. To query the status of the service, "NewService," enter:
  285.   
  286. sc query NewService
  287.   
  288. SC displays the following information:
  289.   
  290.     SERVICE_NAME: NewService
  291.         TYPE               : 20  WIN32_SHARE_PROCESS
  292.         STATE              : 1  STOPPED
  293.                            (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
  294.         WIN32_EXIT_CODE    : 1077       (0x435)
  295.         SERVICE_EXIT_CODE  : 0  (0x0)
  296.         CHECKPOINT         : 0x0
  297.         WAIT_HINT          : 0x0
  298.   
  299. Notice that there is an exit code for this service, even though it has not yet been run. Entering the Windows NT command "net helpmsg 1077" at the command line returns the following text information for error 1077:
  300.   
  301. No attempts to start the service have been made since the last boot.
  302.   
  303. Net Helpmsg can be used to display the text for most Windows NT error messages. This particular exit code indicates that this service hasn't yet been run. Although obvious in this case, this particular exit code is a useful one to look for if you are expecting your service to be auto-started or perhaps when another auto-start service has a dependency on your service. 
  304.  
  305. To enumerate status for active services and drivers, use the following command:
  306.   
  307. sc query 
  308.   
  309. To display status for the messenger service, use the following command:
  310.   
  311. sc query messenger 
  312.   
  313. To enumerate only active drivers, use the following command:
  314.   
  315. sc query type= driver
  316.   
  317. To enumerate only Win32 services, use the following command:
  318.   
  319. sc query type= service
  320.   
  321. To enumerate all services and drivers, use the following command:
  322.   
  323. sc query state= all
  324.   
  325. To enumerate with a 50 byte buffer, use the following command:
  326.   
  327. sc query bufsize= 50
  328.   
  329. To enumerate with resume index = 14, use the following command:
  330.   
  331. sc query ri= 14 
  332.   
  333. To enumerate all interactive services, use the following command:
  334.   
  335. sc query type= service type= interact