代码如下:
Imports System Imports System.Management Imports System.Windows.Forms
Namespace WMISample
Public Class CallWMIMethod
Public Overloads Shared Function Main() As Integer
Try
Dim classInstance As New ManagementObject( _ "root\CIMV2", _ "Win32_TerminalService", Nothing)
' Obtain [in] parameters for the method Dim inParams As ManagementBaseObject = _ classInstance.GetMethodParameters("UserControlService")
' Add the input parameters.
' Execute the method and obtain the return values. Dim outParams As ManagementBaseObject = _ classInstance.InvokeMethod("UserControlService", inParams, Nothing)
' List outParams Console.WriteLine("Out parameters:") Console.WriteLine("ReturnValue: {0}", outParams("ReturnValue"))
Catch err As ManagementException
MessageBox.Show("An error occurred while trying to execute the WMI method: " & err.Message) End Try End Function End Class End Namespace
乐博网附参数说明:
UserControlService 方法会尝试将用户定义的控制代码发送给服务。它将返回下述整数值之一: 0 - 请求已被接受; 1 - 不支持请求; 2 - 用户不具有必要的访问权限; 3 - 服务不能被终止,原因是其他正在运行的服务要依赖它; 4 - 请求的控制代码无效,或服务不接受它; 5 - 请求的控制代码无法发送给服务,原因是服务状态(Win32_BaseService:State)等于 0、1 或 2; 6 - 服务尚未启动; 7 - 服务未以及时方式对启动请求作出响应; 8 - 启动服务时出现不明错误; 9 - 未发现指向可执行服务的目录路径; 10 - 服务已经运行; 11 - 负责添加新服务的数据库被锁住; 12 - 该服务所依赖的某个从属物已从系统上删除; 13 - 该服务无法从依赖的服务上发现所需的服务; 14 - 该服务已被系统禁止; 15 - 该服务不具有在系统上运行所需的正确鉴定; 16 - 正在从系统上删除该服务; 17 - 该服务没有执行线程; 18 - 启动该服务时,存在循环依附关系; 19 - 有一个具有相同名称的服务正在运行; 20 - 在该服务的名称中存在无效字符; 21 - 为该服务传递了无效参数; 22 - 对运行该服务的帐户来说,或是无效、或是缺乏运行此服务的许可权限; 23 - 系统可用的服务数据库中已存在该服务; 24 - 该服务在系统中目前处于暂停状态; 其他 - 关于上面所列以外的整数值,请参阅 Win32 错误代码文档。 |