使用WMI来设置服务(Windows Service)的运行帐号

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方法来设定帐号:

Using service As ManagementObject = New ManagementObject(New ManagementPath(objPath))
Dim wmiParams() As Object = Array.CreateInstance(GetType(Object), 11)
If Not String.IsNullOrEmpty(PrintProvider.Instance.PrintSettings.PrintServiceSetting.Credentials.UserName) Then
If Not PrintProvider.Instance.PrintSettings.PrintServiceSetting.Credentials.UserName.Contains("\") Then
wmiParams(6) = ".\" & PrintProvider.Instance.PrintSettings.PrintServiceSetting.Credentials.UserName
Else
wmiParams(6) = PrintProvider.Instance.PrintSettings.PrintServiceSetting.Credentials.UserName
End If
Else
wmiParams(6) = "LocalSystem"
End If
wmiParams(7) = PrintProvider.Instance.PrintSettings.PrintServiceSetting.Credentials.Password
objResult = service.InvokeMethod("Change", wmiParams)
End Using

 

附:

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 codeDescription
0

The request is accepted.

1

The request is not supported.

2

The user does not have the necessary access privileges.

3

The service cannot be stopped because other services that are running are dependent on it.

4

The requested control code is not valid, or is unacceptable to the service.

5

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.

6

The service has not been started.

7

The service does not respond to the start request quickly.

8

Interactive process.

9

The directory path to the service executable file is not found.

10

The service is already running.

11

The database to add a new service is locked.

12

A dependency for which this service relies on is removed from the system.

13

The service does not find the service needed from a dependent service.

14

The service is disabled from the system.

15

The service does not have the correct authentication to run on the system.

16

This service is being removed from the system.

17

There is no execution thread for the service.

18

There are circular dependencies when starting the service.

19

There is a service running under the same name.

20

There are invalid characters in the name of the service.

21

Invalid parameters have been passed to the service.

22

The account for this service to run under is not valid or does not have the permissions to run the service.

23

The service exists in the database of services available from the system.

24

The service is currently paused in the system.

3) 参考:http://msdn.microsoft.com/en-us/library/aa384899(VS.85).aspx

posted @ 2009-11-02 12:09  Rickey Hu  阅读(1694)  评论(0编辑  收藏  举报