您可以通过API, ADSI或WMI三种方式在VB6中启动和停止windows2000的系统服务。
1.API方式。
您需要调用下面4个API函数:
OpenSCManager用以获得service信息数据库
OpenService用以获得service的句炳
StartService/StopService用以启动/停止service
以下链接提供了用VB查询service的状态和设置的样例程序,在Q189633等文章里面的代码可以作为您在VB里调用NT service操作的一个参考。
http://support.microsoft.com/support/kb/articles/q189/6/33.asp
您可以参考以下链接提供的VC样例程序,停止一个service。
http://support.microsoft.com/support/kb/articles/q245/2/30.asp
2.ADSI方式
您可以使用IADsServiceOperations接口的start/stop方法启动/停止一个NT的service.
如下例在Windows 2000 Professional中启动一个Microsoft® Fax Service
Dim cp As IADsComputer
Dim so As IADsServiceOperations
Set cp = GetObject("WinNT://myMachine,computer")
Set so = cp.GetObject("Service", "Fax")
If (so.Status <> ADS_SERVICE_RUNNING) Then ' The operation is not running.
so.Start
End If
详细信息请参考:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/iadsserviceoperations_start.asp
3.WMI方式。
您也可以用WMI编程对系统service进行管理,如下例:
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select *
from Win32_Service where State='Stopped' )
‘上面的代码返回停止状态的service对象集。
for each Service in ServiceSet
‘ 您可以在这里用Win32_Service对象的StartService/StopService方法启动/停止service。
Next
关于WMI的详细信息请参考以下链接:
http://msdn.microsoft.com/library/en-us/dnwmi/html/mngwmi.asp
- 微软全球技术中心 VB技术支持
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。