使用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

转载于:https://www.cnblogs.com/beiguren/archive/2009/11/02/1594427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值