| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 484 人关注过本帖
标题:[讨论]关于启动与停止windows服务问题
只看楼主 加入收藏
遥远的梦
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-3-13
收藏
 问题点数:0 回复次数:1 
[讨论]关于启动与停止windows服务问题

Private Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type

Private Declare Function OpenSCManager Lib "advapi32.dll" Alias _
"OpenSCManagerA" (ByVal lpMachineName As String, _
ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject _
As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
(ByVal hSCManager As Long, ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Long) As Long
Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" _
(ByVal hService As Long, ByVal dwNumServiceArgs As Long, _
ByVal lpServiceArgVectors As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As _
Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long

Const GENERIC_EXECUTE = &H20000000
Const SERVICE_CONTROL_STOP = 1
Const SERVICE_CONTROL_PAUSE = 2
Const SERVICE_CONTROL_CONTINUE = 3

' start/stop/pause/continue a service
' SERVICENAME is the internal name of the service
' COMMAND can be 0=Start, 1=Stop, 2=Pause, 3=Continue
'
' returns True if successful, False otherwise
' if any error, call Err.LastDLLError for more information

Function ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _
Boolean
Dim hSCM As Long
Dim hService As Long
Dim res As Long
Dim lpServiceStatus As SERVICE_STATUS

' first, check the command
If command < 0 Or command > 3 Then Err.Raise 5

' open the connection to Service Control Manager, exit if error
hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_EXECUTE)
If hSCM = 0 Then Exit Function

' open the given service, exit if error
hService = OpenService(hSCM, ServiceName, GENERIC_EXECUTE)
If hService = 0 Then GoTo CleanUp

' start the service
Select Case command
Case 0
' to start a service you must use StartService
res = StartService(hService, 0, 0)
Case SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, _
SERVICE_CONTROL_CONTINUE
' these commands use ControlService API
' (pass a NULL pointer because no result is expected)
res = ControlService(hService, command, lpServiceStatus)
Debug.Print res
End Select
If res = 0 Then GoTo CleanUp

' return success
ServiceCommand = True

CleanUp:
If hService Then CloseServiceHandle hService
' close the SCM
CloseServiceHandle hSCM

End Function

Private Sub Command1_Click()
ServiceCommand "Themes", 1
End Sub

Private Sub Command2_Click()
ServiceCommand "Themes", 2
End Sub

这是我写好的,怎么没用,请问各位,问题出在哪里?

搜索更多相关主题的帖子: windows 服务 
2007-08-20 10:18
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
let me have a try

Shell "net start messenger"
2007-08-20 11:40
快速回复:[讨论]关于启动与停止windows服务问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014069 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved