创建服务函数
创建一个服务对象并且把它加入到服务管理数据库中SyntaxVB
C#
C++
F#
JScript
Copy
SC_HANDLE CreateService(
SC_HANDLE hSCManager, //服务控制管理程序维护的登记数据库的句柄,由系统函数OpenSCManager 返回
LPCTSTR lpServiceName, //以NULL 结尾的服务名,用于创建登记数据库中的关键字
LPCTSTR lpDisplayName, //以NULL 结尾的服务名,用于用户界面标识服务
DWORD dwDesiredAccess, //指定服务返回类型
DWORD dwServiceType, //指定服务类型
DWORD dwStartType, //指定何时启动服务
DWORD dwErrorControl, //指定服务启动失败的严重程度
LPCTSTR lpBinaryPathName, //指定服务程序二进制文件的路径
LPCTSTR lpLoadOrderGroup, //指定顺序装入的服务组名
LPDWORD lpdwTagId, //忽略,NULL
LPCTSTR lpDependencies, //指定启动该服务前必须先启动的服务或服务组
LPCTSTR lpServiceStartName, //以NULL 结尾的字符串,指定服务帐号。如是NULL,则表示使用LocalSystem 帐号
LPCTSTR lpPassword //以NULL 结尾的字符串,指定对应的口令。为NULL表示无口令。但使用LocalSystem时填NULL
);
参数
hSCManager[in] 服务控制管理器数据库的句柄。 此句柄由OpenSCManager函数返回,并且必须 具有 SC_MANAGER_CREATE_SERVICE 的访问权限。 有关更多的信息请参阅Service安全和访问权限。
lpServiceName[in] 要安装该服务的名称。 最大字符串长度为 256 个字符。 服务控制管理器数据库将保留在的字符的大小写,但是服务名称比较总是区分大小写。 正斜杠和一个反斜线不是有效的服务名称字符。
lpDisplayName[in, optional] 对被用户界面程序用来识别服务的显示名称。 此字符串具有最大长度为 256 个字符。 服务控制管理器中的情况下保留名称。 显示名称比较总是不区分大小写。
dwDesiredAccess[in] 对服务的访问。 请求的访问之前,系统将检查调用进程的访问令牌。 一个值列表请参阅服务安全和访问权限。
dwServiceType[in] 服务类型。 此参数可以是下列值之一。
值
作用
SERVICE_ADAPTER 0x00000004
Reserved.
SERVICE_FILE_SYSTEM_DRIVER 0x00000002
File system driver service.
SERVICE_KERNEL_DRIVER 0x00000001
Driver service.
SERVICE_RECOGNIZER_DRIVER 0x00000008
Reserved.
SERVICE_WIN32_OWN_PROCESS 0x00000010
Service that runs in its own process.
SERVICE_WIN32_SHARE_PROCESS 0x00000020
Service that shares a process with one or more other services. For more information, see Service Programs.
If you specify either SERVICE_WIN32_OWN_PROCESS or SERVICE_WIN32_SHARE_PROCESS, and the service is running in the context of the LocalSystem account, you can also specify the following value.
Value
Meaning
SERVICE_INTERACTIVE_PROCESS 0x00000100
The service can interact with the desktop.
For more information, see Interactive Services.
dwStartType[in] The service start options. This parameter can be one of the following values.
Value
Meaning
SERVICE_AUTO_START 0x00000002
A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services.
SERVICE_BOOT_START 0x00000000
A device driver started by the system loader. This value is valid only for driver services.
SERVICE_DEMAND_START 0x00000003
A service started by the service control manager when a process calls theStartServicefunction. For more information, see Starting Services on Demand.
SERVICE_DISABLED 0x00000004
A service that cannot be started. Attempts to start the service result in the error code ERROR_SERVICE_DISABLED.
SERVICE_SYSTEM_START 0x00000001
A device driver started by theIoInitSystemfunction. This value is valid only for driver services.
dwErrorControl[in] The severity of the error, and action taken, if this service fails to start. This parameter can be one of the following values.
Value
Meaning
SERVICE_ERROR_CRITICAL 0x00000003
The startup program logs the error in the event log, if possible. If the last-known-good configuration is being started, the startup operation fails. Otherwise, the system is restarted with the last-known good configuration.
SERVICE_ERROR_IGNORE 0x00000000
The startup program ignores the error and continues the startup operation.
SERVICE_ERROR_NORMAL 0x00000001
The startup program logs the error in the event log but continues the startup operation.
SERVICE_ERROR_SEVERE 0x00000002
The startup program logs the error in the event log. If the last-known-good configuration is being started, the startup operation continues. Otherwise, the system is restarted with the last-known-good configuration.
lpBinaryPathName[in, optional] 服务二进制文件,完全限定路径。 如果路径中包含空格它必须被引用,以便它正确的解析。 例如"d: 我 sharemyservice.exe"应指定为""d: 我 sharemyservice.exe""。该路径也可以包含一个自动启动服务的参数。 例如"d:mysharemyservice.exe arg1 arg2"。 这些参数被传递给服务的入口点通常主要作用。
如果另一台计算机上指定一个路径,共享必须可以访问由本地计算机的计算机帐户,因为这是在远程调用中使用的安全上下文。 但是,这项规定会影响本地计算机远程计算机中允许任何潜在的漏洞。 因此,最好使用一个本地文件。
lpLoadOrderGroup[in, optional] 在加载顺序此服务所属的组的名称。 指定NULL或空字符串,如果该服务不属于组。
启动程序使用加载顺序组加载服务相对于其他组的指定顺序组。 加载顺序组列表包含在下面的注册表值: HKEY_LOCAL_MACHINESystemCurrentControlSetControlServiceGroupOrder
[out,可选] lpdwTagId A 指向接收 lpLoadOrderGroup 参数中指定的组中唯一的标记值的变量。 如果不更改现有的标签,请指定 NULL。
订购服务启动一个加载顺序通过在以下注册表值中指定标记顺序矢量的组中,您可以使用的标记:
HKEY_LOCAL_MACHINESystemCurrentControlSetControlGroupOrderList
标记只能用于具有 SERVICE_BOOT_START 或启动类型的 SERVICE_SYSTEM_START 驱动程序服务。
lpDependencies[in, optional] 空分隔名称的服务或加载顺序组系统必须在这个服务开始之前的双空终止数组的指针。 如果服务没有任何依赖关系,请指定为 NULL 或空字符串。 一组依赖意味着如果至少一个组的成员运行后试图启动组的所有成员,可以运行此服务。
必须,以便他们可以区分名服务由于服务和服务组共享相同的命名空间前缀与 SC_GROUP_IDENTIFIER 的组名。
lpServiceStartName[in, optional] 该服务应在其下运行的帐户的名称。 如果 SERVICE_WIN32_OWN_PROCESS 服务类型 DomainNameUserName 窗体中使用一个帐户名。 服务进程将被作为此用户登录。 如果该帐户属于内置域,您可以指定.UserName。
如果为 NULL 此参数 CreateService 使用 LocalSystem 帐户。 如果服务类型指定 SERVICE_INTERACTIVE_PROCESS,该服务必须运行在 LocalSystem 帐户。
如果NTAUTHORITYLocalService 此参数 CreateService 使用 LocalService 帐户。 如果 NT AUTHORITYNetworkService 参数 CreateService 使用 NetworkService 帐户。
共享的处理可以运行任何用户。
如果服务类型 SERVICE_KERNEL_DRIVER 或 SERVICE_FILE_SYSTEM_DRIVER 名称是系统用来加载设备驱动程序的驱动程序对象名称。 如果该驱动程序将使用由IO系统创建的默认对象名称,请指定 NULL。
可以将服务配置为使用一个托管的帐户或虚拟帐户。 如果该服务被配置为使用一个托管的服务帐户,名称是托管的服务帐户名称。 如果该服务被配置为使用一个虚拟帐户,作为 NT SERVICEServiceName 指定名称。 有关托管的服务帐户和虚拟帐户的详细信息请参阅服务帐户的分步指南。
WindowsServer2008、 WindowsVista、 Windows Server 2003 和 Windows XP2000: Windows 7 和 Windows Server 2008 R2 之前不支持托管的服务帐户和虚拟帐户。
lpPassword[in, optional] 由 lpServiceStartName 参数指定的帐户名的密码。 如果该帐户没有密码,或者如果在 LocalService,NetworkService 或 LocalSystem 帐户中运行该服务,请指定一个空字符串。 更多的信息请参阅服务记录列表。
如果托管的服务帐户或虚拟帐户名称的名称由 lpServiceStartName 参数指定的帐户名称 lpPassword 参数必须为 NULL。
密码将被忽略,对驱动程序服务。
Return Value
如果函数成功,返回值将是该服务的句柄。
如果函数失败,则返回值为 NULL。 若要扩展的错误了解调用GetLastError。
下面的错误代码可以由服务控制管理器设置。 其他错误代码可以设置的由服务控制管理器调用注册表函数。
Return code
Description
ERROR_ACCESS_DENIED
The handle to the SCM database does not have the SC_MANAGER_CREATE_SERVICE access right.
ERROR_CIRCULAR_DEPENDENCY
A circular service dependency was specified.
ERROR_DUPLICATE_SERVICE_NAME
The display name already exists in the service control manager database either as a service name or as another display name.
ERROR_INVALID_HANDLE
The handle to the specified service control manager database is invalid.
ERROR_INVALID_NAME
The specified service name is invalid.
ERROR_INVALID_PARAMETER
A parameter that was specified is invalid.
ERROR_INVALID_SERVICE_ACCOUNT
The user account name specified in thelpServiceStartNameparameter does not exist.
ERROR_SERVICE_EXISTS
The specified service already exists in this database.
RemarksTheCreateServicefunction creates a service object and installs it in the service control manager database by creating a key with the same name as the service under the following registry key:
HKEY_LOCAL_MACHINESystemCurrentControlSetServices
Information specified byCreateService,ChangeServiceConfig, andChangeServiceConfig2is saved as values under this key. The following are examples of values stored for a service.
Value
Description
DependOnGroup
Load-ordering groups on which this service depends, as specified bylpDependencies.
DependOnService
Services on which this service depends, as specified bylpDependencies.
Description
Description specified byChangeServiceConfig2.
DisplayName
Display name specified bylpDisplayName.
ErrorControl
Error control specified bydwErrorControl.
FailureActions
Failure actions specified byChangeServiceConfig2.
Group
Load ordering group specified bylpLoadOrderGroup. Note that setting this value can override the setting of theDependOnServicevalue.
ImagePath
Name of binary file, as specified bylpBinaryPathName.
ObjectName
Account name specified bylpServiceStartName.
Start
When to start service, as specified bydwStartType.
Tag
Tag identifier specified bylpdwTagId.
Type
Service type specified bydwServiceType.
Setup programs and the service itself can create additional subkeys for service-specific information.
The returned handle is only valid for the process that calledCreateService. It can be closed by calling theCloseServiceHandlefunction.
If you are creating services that share a process, avoid calling functions with process-wide effects, such asExitProcess. In addition, do not unload your service DLL.ExamplesFor an example, see Installing a Service.Requirements<th>Minimum supported client</th>Windows 2000 Professional
<th>Minimum supported server</th>Windows 2000 Server
<th>Header</th>Winsvc.h (include Windows.h)
<th>Library</th>Advapi32.lib
<th>DLL</th>Advapi32.dll
<th>Unicode and ANSI names</th>CreateServiceW(Unicode) andCreateServiceA(ANSI)
SC_HANDLE CreateService(
SC_HANDLE hSCManager, //服务控制管理程序维护的登记数据库的句柄,由系统函数OpenSCManager 返回
LPCTSTR lpServiceName, //以NULL 结尾的服务名,用于创建登记数据库中的关键字
LPCTSTR lpDisplayName, //以NULL 结尾的服务名,用于用户界面标识服务
DWORD dwDesiredAccess, //指定服务返回类型
DWORD dwServiceType, //指定服务类型
DWORD dwStartType, //指定何时启动服务
DWORD dwErrorControl, //指定服务启动失败的严重程度
LPCTSTR lpBinaryPathName, //指定服务程序二进制文件的路径
LPCTSTR lpLoadOrderGroup, //指定顺序装入的服务组名
LPDWORD lpdwTagId, //忽略,NULL
LPCTSTR lpDependencies, //指定启动该服务前必须先启动的服务或服务组
LPCTSTR lpServiceStartName, //以NULL 结尾的字符串,指定服务帐号。如是NULL,则表示使用LocalSystem 帐号
LPCTSTR lpPassword //以NULL 结尾的字符串,指定对应的口令。为NULL表示无口令。但使用LocalSystem时填NULL
);
其中dwStartType取值类型如下:
共有五种启动类型。前三种类型是:SERVICE_AUTO_START、SERVICE_DISABLED 和 SERVICE_DEMAND_START。对应的标准启动类型:自动、禁用和手动,通常使用“计算机管理”管理工具中的“服务”进行配置。后两种类型是:SERVICE_BOOT_START 和 SERVICE_SYSTEM_START,通常用于配置加载设备驱动程序的方式。例如,在启动计算机时或启动 Windows 2000 时加载。
Delphi sample:
Procedure InstallSelfToSvr();
var
hscm,hsvr:THandle;
PCPathName:PAnsiChar;
begin
try
hscm := OpenSCManager(nil,nil,SM_MANAGER_ALL_ACCESS);
if hscm = 0 then exit;
PCPathName := pchar(paramstr(0));
hsvr := CreateService(hscm,'MyService','MyService Display Name',SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,PCPathName,nil,nil,nil,nil,nil);
CloseServiceHandle(hsvr);
CloseServiceHandle(hscm);
except
end;
end;