Windows Service运行的账号类型有两种:
1) 本地系统帐号(Local System Account);
使用此帐号,Windows Service即使用当前登录的系统帐号来运行服务。使用此类型帐号,可通过打开Allow service to interact with desktop选项来允许服务与桌面交互;譬如从服务向一个窗口发送消息。
2) 其它帐号;
使用此类型帐号运行服务与1) 本地系统帐号的一个显著区别是,它不能与桌面交互。
使用WMI来设置服务帐号:
1) 向工程中添加对System.Management.dll的引用;
2) 使用Win32_BaseService的Change方法来设定帐号:
附:
1)
uint32 Change(
[in] string DisplayName,
[in] string PathName,
[in] uint8 ServiceType,
[in] uint8 ErrorControl,
[in] string StartMode,
[in] boolean DesktopInteract,
[in] string StartName,
[in] string StartPassword,
[in] string LoadOrderGroup,
[in] string LoadOrderGroupDependencies[],
[in] string ServiceDependencies[]
);
2):
Return code | Description |
---|---|
| The request is accepted. |
| The request is not supported. |
| The user does not have the necessary access privileges. |
| The service cannot be stopped because other services that are running are dependent on it. |
| The requested control code is not valid, or is unacceptable to the service. |
| The requested control code cannot be sent to the service because the State property in the Win32_BaseService object is equal to 0, 1, or 2. |
| The service has not been started. |
| The service does not respond to the start request quickly. |
| Interactive process. |
| The directory path to the service executable file is not found. |
| The service is already running. |
| The database to add a new service is locked. |
| A dependency for which this service relies on is removed from the system. |
| The service does not find the service needed from a dependent service. |
| The service is disabled from the system. |
| The service does not have the correct authentication to run on the system. |
| This service is being removed from the system. |
| There is no execution thread for the service. |
| There are circular dependencies when starting the service. |
| There is a service running under the same name. |
| There are invalid characters in the name of the service. |
| Invalid parameters have been passed to the service. |
| The account for this service to run under is not valid or does not have the permissions to run the service. |
| The service exists in the database of services available from the system. |
| The service is currently paused in the system. |
3) 参考:http://msdn.microsoft.com/en-us/library/aa384899(VS.85).aspx