How To Create a User-Defined Service or How to create a Windows service by using Sc.exe


The Windows NT Resource Kit provides two utilities that allow you to create a Windows NT user-defined service for Windows NT applications and some 16-bit applications (but not for batch files).
Instrsrv.exe installs and removes system services from Windows NT and Srvany.exe allows any Windows NT application to run as a service.

 

To create a Windows NT user-defined service, perform the following steps:

  1. At a MS-DOS command prompt(running CMD.EXE), type the following command:
    path\INSTSRV.EXE My Service path\SRVANY.EXE
    where path is the drive and directory of the Windows NT Resource Kit (i.e., C:\RESKIT) and My Service is the name of the service you are creating.
    Example:

    C:\Program Files\Resource Kit\Instsrv.exe Notepad C:\Program Files\Resource Kit\Srvany.exe

    NOTE: To verify that the service was created correctly, check the registry to verify that the ImagePath value under

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\service name

    is set to point to SRVANY.EXE. If this is not set correctly, the service will stop shortly after it starts and return an Event ID 7000 “The service name failed to start.”
    WARNING: Using Registry Editor incorrectly can cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that problems resulting from the incorrect use of Registry Editor can be solved. Use Registry Editor at your own risk.
    For information about how to edit the registry, view the “Changing Keys And Values” online Help topic or the “Add and Delete Information in the Registry” and “Edit Registry Data” online Help topics in Registry Editor.
    NOTE: You should back up the registry before you edit it.

  2. Run Registry Editor (Regedt32.exe)and locate the following subkey:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<My Service>
  3. From the Edit menu, click Add Key. Type the following and click OK:
    Key Name: Parameters
    Class : <leave blank>
  4. Select the Parameters key.
  5. From the Edit menu, click Add Value. Type the following and click OK:
    Value Name: Application
    Data Type : REG_SZ
    String : <path>\<application.ext>
    where <path>\<application.ext> is the drive and full path to the application executable including the extension (i.e., C:\WinNT\Notepad.exe)
  6. Close Registry Editor.

By default, a newly created service it configured to run Automatically when the system is restarted. To change this setting to Manual, run the Services applet from Control Panel and change the Startup value to Manual. A service set to Manual can be started in one of several ways:

– From the Services applet in Control Panel
– From a MS-DOS command prompt, type the following:
NET START <My Service>
– Use the Sc.exe utility from the Resource Kit. Type the following from a MS-DOS command prompt:
<path>\Sc.exe start <My Service>
where <path> is the drive and directory of the Windows NT Resource Kit (i.e., C:\Reskit).

For more information on installing and removing a user-defined service, please see the Srvany.wri document provided with the Windows NT Resource Kit utilities (i.e., C:\Reskit\Srvany.wri). This document can also be found on the Windows NT Resource Kit CD in the Common\Config directory.

 

 

How to create a Windows service by using Sc.exe

To remotely create and start a service from the command line, you can use the SC tool (Sc.exe) included in the Resource Kit.
Although you can use the Netsvc.exe and Instsrv.exe tools to start a service on a remote computer, these tools do not give you the ability to remotely create a service.
Note The Srvinstw.exe tool is the graphical user interface (GUI) version used to create remote services. Srvinstw.exe is not a command-line tool.

 

You can use Sc.exe to help develop services for Windows. Sc.exe, which is provided in the Resource Kit, implements calls to all of the Windows 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 tool 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.
Sc.exe also allows you to call any of the service control API functions and vary any of the parameters from the command line. The advantage to this is that it provides a convenient way to create or configure the service information in the registry and the Service Control Manager database. You do not have to configure the service by manually creating entries in the registry and then restarting the computer to force the Service Control Manager to update its database.
Sc.exe uses the following syntax:
Syntax1 (use Syntax1 to run Sc.exe)

sc [Servername] Command Servicename [Optionname= Optionvalue…]

Syntax2 (use Syntax2 to display help information, except for the query command)

sc [Command]

Parameters
  • Servername
    Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two backslash (\) characters (for example, \\myserver). To run Sc.exe on the local computer, do not supply this parameter.
  • Command
    Specifies the sc command. Note that many of the sc commands require administrative privileges on the specified computer. Sc.exe supports the following commands:

    Config
    Changes the configuration of a service (persistent).
    Continue
    Sends a Continue control request to a service.
    Control
    Sends a control to a service.
    Create
    Creates a service (adds it to the registry).
    Delete
    Deletes a service (from the registry).
    EnumDepend
    Enumerates service dependencies.
    GetDisplayName
    Obtains the DisplayName for a service.
    GetKeyName
    Obtains the ServiceKeyName for a service.
    Interrogate
    Sends an Interrogate control request to a service.
    Pause
    Sends a Pause control request to a service.
    qc
    Queries configuration for the service. For detailed information, see the reference section, “SC QC.”
    Query
    Queries the status for a service, or enumerates the status for types of services. For detailed information, see the reference section, “SC QUERY.”
    Start
    Starts a service
    Stop
    Sends a Stop request to a service.

  • Servicename
    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 command and the Services tool in Control Panel. Sc.exe uses the service key name as the primary identifier for the service.
  • Optionname
    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 none, one, or more optional parameters name and value pairs.
  • Optionvalue
    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.

Many of the commands require Administrator privileges. Make sure that you are an administrator of the computer where the development is being performed.
The sc create command creates an entry for the service in the registry and in the Service Control Manager database.
Syntax

sc [Servername] create Servicename [Optionname= Optionvalue…

Parameters
  • Servername
    Optional. Specifies the name of the server when you want to run the commands on a remote computer. The name must start with two backslash characters (for example, \\myserver). To run SC on the local computer, do not supply this parameter.
  • Servicename
    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 tool in Control Panel. Sc.exe uses the service key name as the primary identifier for the service.
  • Optionname
    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 none, one, or more optional parameters name and value pairs. The sc query command supports the following values:OptionnameOptionvalue

    type=own, share, interact, kernel, filesys
    Type of service you want to create. Optionvalues include types used by drivers.
    (default = share)
    start=boot, system, auto, demand, disabled
    Start type for the service. Option values include types used by drivers.
    (default = demand)
    error=normal, severe, critical, ignore
    Severity of error if the service does not start during boot.
    (default = normal)
    binPath=(string)
    Path name to the service binary file. There is no default for this parameter. This string must be supplied.
    group=(string)
    Name of group which this service is a member of. The list of groups are stored in the registry under ServiceGroupOrder.
    (default = nothing)
    tag=(string)
    If this string is set to “yes,” Sc.exe obtains a TagId from the CreateService call, however, Sc.exe does not display the tag.
    (default = nothing)
    depend=(space separated string)
    Names of services or groups that must start before this service.
    obj=(string)
    Name of account in which the service runs. For drivers, this is the Windows driver object name.
    (default = LocalSystem)
    DisplayName=(string)
    A string that can be used by user-interface programs to identify the service.
    password=(string)
    A password string. This is required if an account other than LocalSystem is used.

Optionvalue
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 is passed in. Note that there is a space between OptionValue and the equal sign.
Note The sc create command performs the operations of the CreateService API function.
The following example creates a registry entry for the service named “NewService” on the computer called \\remotecomputer:

sc \\remotecomputer create newservice binpath= c:\nt\system32\newserv.exe

Note In Windows 2000, Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008, the binpath is located at C:\Windows\System32\Newserv.exe.
This service is created as a WIN32_SHARE_PROCESS with a SERVICE_DEMAND_START start-type by default. It does not have any dependencies, and runs in the LocalSystem security context.
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.

sc create newservice binpath= c:\nt\system32\newserv.exe type= own start= auto depend= “+tdi netbios”

Note In Windows 2000, Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008, the binpath is located at C:\Windows\System32\Newserv.exe.
For further details about the Sc.exe tool, please see the Sc-dev.txt document in the Resource Kit. This document describes Sc.exe in greater detail.

 

At the time of this posting, there is no Windows Server 2008 Resource Kit Tools, so get the “srvany.exe ” from the “Windows Server 2003 Resource Kit Tools ” and copy it to a suitable location on your Win2008 server (e.g. C:\Windows\System32\ ).
Use “sc ” to create a new service that launches “srvany ” (e.g. sc create MyService binPath= C:\Windows\System32\srvany.exe DisplayName= “My Custom Service” )
Using RegEdit : create a “Parameters ” key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Paramaters\ )
Using RegEdit : within the newly created “Parameters ” key , create a string value called “Application ” and enter the full path to the application you are wanting to run as a service. (No quotes required.)

And that should do it.

Note : some applications store their settings in a user hive, not under HKEY_LOCAL_MACHINE, so you will need to run the application to configure it, and then configure the service to run as that user.

 

 

Ví dụ: chạy CMD

gõ lệnh: sc create “VCE Testing System” start= auto binpath= ”c:\blabla\VCE Testing\server.exe”

 

 

Sau khi chạy lệnh SC trên, bạn cần tạo đăng ký trên WIndows Registry Editor Version

 

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VCE Testing System\Paramaters]

“Application”=”c:\\blabla\\VCE Testing\\server.exe”

 

Hoặc cách 2: dùng các tool phần mềm miễn phí

ví dụ: http://tools.sysprogs.org/srvman/

About thangletoan

Hallo Aloha

Posted on 09/09/2013, in Converting file exe to a Windows Service, Microsoft, Windows 2003, Windows Server 2008 R2, Windows Service and tagged , . Bookmark the permalink. Để lại bình luận.

Gửi phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s

%d bloggers like this: