VB 修改系统服务启动类型

如题,,用CMD来执行命令scconfig服务名称start=auto这样的就免了.最好是给出代码..谢谢了~~来个高手帮忙看看,,,谢谢下面的大哥的回复,不过对我没什么... 如题,,用CMD来执行命令sc config 服务名称 start= auto这样的就免了.
最好是给出代码..谢谢了~~
来个高手帮忙看看,,,谢谢下面的大哥的回复,不过对我没什么作用,请高手出现,,...
展开
 我来答
yhu53603fd94fe
2009-04-21 · TA获得超过7838个赞
知道大有可为答主
回答量:7079
采纳率:55%
帮助的人:2837万
展开全部
VB中操作系统服务

原创
觉得对你有帮忙的 请顶一个

太长 分2部分发出

'系统服务操作模块
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal szDest As String, ByVal szcSource As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Long, ByVal cbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_DWORD = 4
Private Const KEY_READ = &H20019

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 OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function EnumServicesStatus Lib "advapi32.dll" Alias "EnumServicesStatusA" (ByVal hSCManager As Long, ByVal dwServiceType As Long, ByVal dwServiceState As Long, lpServices As Any, ByVal cbBufSize As Long, pcbBytesNeeded As Long, lpServicesReturned As Long, lpResumeHandle 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 GetServiceKeyName Lib "advapi32.dll" Alias "GetServiceKeyNameA" (ByVal hSCManager As Long, ByVal lpDisplayName As String, ByVal lpServiceName As String, lpcchBuffer As Long) As Long
Private Declare Function GetServiceDisplayName Lib "advapi32.dll" Alias "GetServiceDisplayNameA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal lpDisplayName As String, lpcchBuffer As Long) As Long
Private Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function QueryServiceConfig Lib "advapi32.dll" Alias "QueryServiceConfigA" (ByVal hService As Long, lpServiceConfig As Byte, ByVal cbBufSize As Long, pcbBytesNeeded As Long) As Long
Private Declare Function QueryServiceConfig2 Lib "advapi32.dll" Alias "QueryServiceConfig2A" (ByVal hService As Long, ByVal dwInfoLevel As Long, ByRef SD As SERVICE_DESCRIPTION_STRUCT, ByVal cbBufSize As Long, ByRef pcbBytesNeeded As Long) As Long
Private Declare Function CreateService Lib "advapi32.dll" Alias "CreateServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal lpDisplayName As String, ByVal dwDesiredAccess As Long, ByVal dwServiceType As Long, ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, lpdwTagId As Long, ByVal lpDependencies As String, ByVal lp As String, ByVal lpPassword As String) As Long
Private Declare Function ChangeServiceConfig Lib "advapi32.dll" Alias "ChangeServiceConfigA" (ByVal hService As Long, ByVal dwServiceType As Long, ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, lpdwTagId As Long, ByVal lpDependencies As String, ByVal lpServiceStartName As String, ByVal lpPassword As String, ByVal lpDisplayName As String) As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Private Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long

Private Type QUERY_SERVICE_CONFIG
dwServiceType As Long
dwStartType As Long
dwErrorControl As Long
lpBinaryPathName As Long 'String
lpLoadOrderGroup As Long ' String
dwTagId As Long
lpDependencies As Long 'String
lpServiceStartName As Long 'String
lpDisplayName As Long 'String
End Type

Private Type SERVICE_DESCRIPTION_STRUCT
lpDescription As Long
szDescription As String * 1020
End Type

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 Type ENUM_SERVICE_STATUS
lpServiceName As Long
lpDisplayName As Long
ServiceStatus As SERVICE_STATUS
End Type

Private Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Private Const SERVICES_FAILED_DATABASE = "ServicesFailed"

Private Const SERVICE_NO_CHANGE = &HFFFF
Private Const SERVICE_ACTIVE = &H1
Private Const SERVICE_INACTIVE = &H2
Private Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)

'Service Controls
Private Const SERVICE_CONTROL_STOP = &H1
Private Const SERVICE_CONTROL_PAUSE = &H2
Private Const SERVICE_CONTROL_CONTINUE = &H3
Private Const SERVICE_CONTROL_INTERROGATE = &H4
Private Const SERVICE_CONTROL_SHUTDOWN = &H5

' Service State for CurrentState
Private Const SERVICE_STOPPED = &H1
Private Const SERVICE_START_PENDING = &H2
Private Const SERVICE_STOP_PENDING = &H3
Private Const SERVICE_RUNNING = &H4
Private Const SERVICE_CONTINUE_PENDING = &H5
Private Const SERVICE_PAUSE_PENDING = &H6
Private Const SERVICE_PAUSED = &H7

Private Const SERVICE_ACCEPT_STOP = &H1
Private Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
Private Const SERVICE_ACCEPT_SHUTDOWN = &H4

Private Const SC_MANAGER_CONNECT = &H1
Private Const SC_MANAGER_CREATE_SERVICE = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Private Const SC_MANAGER_LOCK = &H8
Private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20

Private Const READ_CONTROL = &H20000

Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_ALL = &H1F0000

Private Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)

Private Const SERVICE_QUERY_CONFIG = &H1
Private Const SERVICE_CHANGE_CONFIG = &H2
Private Const SERVICE_QUERY_STATUS = &H4
Private Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Private Const SERVICE_START = &H10
Private Const SERVICE_STOP = &H20
Private Const SERVICE_PAUSE_CONTINUE = &H40
Private Const SERVICE_INTERROGATE = &H80
Private Const SERVICE_USER_DEFINED_CONTROL = &H100

Private Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

Private Const ERROR_MORE_DATA = 234
Private Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Private Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
Private Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS Or SERVICE_WIN32_SHARE_PROCESS
Private Const GENERIC_READ = &H80000000

搜索更多相关主题的帖子: 操作系统 服务

偶的小窝>>[url="http://Xacker.Net"]http://Xacker.Net[/url]

[广告] 最新一期擂台题目出来了!

xacker

学者

文盲

UID 22324
精华 2
积分 2574
帖子 547
威望 266
擂点 0
人气 0
阅读权限 80
注册 2004-12-24
状态 离线 #2发表于 2007-6-21 11:46 资料 主页 个人空间 短消息
'修改服务启动类型 只会用写注册表方法 ServiceName是服务名 StartType 2自动 3手动 4禁用
'如果谁在VB下有更好的办法 请告诉小弟
Public Function SetServiceStartType(ServiceName As String, StartType As Long) As Boolean
Dim id As Long
If RegCreateKey(&H80000002, "SYSTEM\CurrentControlSet\Services\" & ServiceName, id) Then Exit Function
If RegSetValueEx(id, "Start", 0, REG_DWORD, StartType, Len(StartType)) = 0 Then SetServiceStartType = True
Reg = RegCloseKey(id)
End Function
'删除服务'需先停止服务
Public Function ServiceDelete(ServiceName As String) As Boolean
Dim hSCManager As Long, hService As Long
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS)
ServiceDelete = DeleteService(hService)
CloseServiceHandle (hService)
CloseServiceHandle (hSCManager)
End Function
'写注册表方法删服务,需重启才有效
Public Function DeleteServiceFlag(ServiceName As String) As Boolean
Dim Flag As Long
Dim id As Long
Flag = 1
If RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & ServiceName, id) Then Exit Function
If RegSetValueEx(id, "DeleteFlag", 0, REG_DWORD, Flag, Len(Flag)) = 0 Then DeleteServiceFlag = True
Reg = RegCloseKey(id)
End Function
'启动服务
Public Function ServiceStart(ServiceName As String) As Boolean
Dim hSCManager As Long
Dim hService As Long
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS)
ServiceStart = StartService(hService, 0, 0)
CloseServiceHandle (hService)
CloseServiceHandle (hSCManager)
End Function
'控制服务 ServiceStatic对应Service Controls常数
Public Function ServiceControl(ServiceName As String, ServiceStatic As Byte) As Boolean
Dim Service As SERVICE_STATUS
Dim hSCManager As Long, hService As Long

hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS)
ServiceControl = ControlService(hService, ServiceStatic, Service)
CloseServiceHandle (hService)
CloseServiceHandle (hSCManager)
End Function
'返回该服务QUERY_SERVICE_CONFIG结构
Public Function GetServiceConfig(ServiceName As String) As QUERY_SERVICE_CONFIG
Dim hSCM As Long
Dim hSVC As Long
Dim udtConfig As QUERY_SERVICE_CONFIG
Dim lret As Long
Dim lBytesNeeded As Long

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hSVC = OpenService(hSCM, ServiceName, SERVICE_ALL_ACCESS)
ReDim abConfig(0) As Byte
lret = QueryServiceConfig(hSVC, abConfig(0), 0&, lBytesNeeded)
ReDim abConfig(lBytesNeeded) As Byte
lret = QueryServiceConfig(hSVC, abConfig(0), lBytesNeeded, lBytesNeeded)
CopyMemory udtConfig, abConfig(0), Len(udtConfig)
GetServiceConfig = udtConfig

CloseServiceHandle (hSVC)
CloseServiceHandle (hSCM)
End Function
'获取服务状态 可以使用GetServiceConfig
Public Function GetServiceStatic(sKeyName As String) As String
Dim hSCManager As Long, hService As Long, lngResult As Long
Dim Service As SERVICE_STATUS
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCManager, sKeyName, SERVICE_ALL_ACCESS)
QueryServiceStatus hService, Service
Select Case Service.dwCurrentState
Case 1: GetServiceStatic = "停止"
Case 4: GetServiceStatic = "运行"
Case Else: GetServiceStatic = "未知"
End Select
CloseServiceHandle hSCManager
CloseServiceHandle hService
End Function
'获取服务启动类型 可以使用GetServiceConfig
Public Function GetServiceStartType(sKeyName As String) As String
Dim Service As QUERY_SERVICE_CONFIG
Service = GetServiceConfig(sKeyName)
Select Case Service.dwStartType
Case 2: GetServiceStartType = "自动"
Case 3: GetServiceStartType = "手动"
Case 4: GetServiceStartType = "禁止"
End Select
End Function
'获取服务长 短名
Public Function GetServiceName(ServiceName As String, Optional IsKeyName As Boolean = True) As String
Dim hSCManager As Long, hService As Long, lngResult As Long
Dim strTemp As String * 256
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
If IsKeyName Then
GetServiceKeyName hSCManager, ServiceName, strTemp, 255
Else
GetServiceDisplayName hSCManager, ServiceName, strTemp, 255
End If
CloseServiceHandle hSCManager
GetServiceName = Trim0(strTemp)
End Function
'获取服务描述
Public Function GetServiceDescription(ByVal ServiceName As String) As String
Dim hSCM As Long, hSVC As Long
Dim SD_STRUCT As SERVICE_DESCRIPTION_STRUCT
Dim cbDescription As Long

hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_READ)
hSVC = OpenService(hSCM, ServiceName, SERVICE_QUERY_CONFIG)
QueryServiceConfig2 hSVC, SERVICE_QUERY_CONFIG, SD_STRUCT, Len(SD_STRUCT), cbDescription
GetServiceDescription = Left(SD_STRUCT.szDescription, ((cbDescription - Len(SD_STRUCT.lpDescription)) / 2) - 1)
CloseServiceHandle hSVC
CloseServiceHandle hSCM
End Function
'读注册表方式获取服务描述
Public Function GetServiceDescription_ReadReg(ServiceName As String) As String
Dim Handle As Long
Dim buf As Long
Dim Description As String * 256

If RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & ServiceName, 0, KEY_READ, Handle) Then Exit Function
ReDim resBinary(0) As Byte
RegQueryValueEx Handle, "Description", 0, 1, resBinary(0), buf

If buf = 0 Then Exit Function
ReDim resBinary(buf - 1) As Byte
RegQueryValueEx Handle, "Description", 0, 1, resBinary(0), buf
CopyMemory ByVal Description, resBinary(0), buf

GetServiceDescription_ReadReg = Trim0(Description)
End Function
'设置服务描述 只会用写注册表的方法.. 如果谁在VB下有更好的办法 请告诉小弟
Public Function SetServiceDescription(ServiceName As String, ServiceDescription As String) As Boolean
Dim id As Long
If RegCreateKey(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & ServiceName, id) Then Exit Function
RegSetValueExString id, "Description", 0, 1, ServiceDescription, LenB(ServiceDescription)
Reg = RegCloseKey(id)
End Function

'枚举服务列表(服务短名) 为兼容VB5 数组当参数传址 EnumServicesStatus函数VB用法在MSDN中查到
'用法
'dim a() as string
'call getservicelist(a) 即可
Public Function GetServiceList(ServiceList() As String) 'As ENUM_SERVICE_STATUS()
Dim lngServiceStatusInfoBuffer As Long, lngBytesNeeded As Long, lngServicesReturned As Long
Dim hNextUnreadEntry As Long, lngStructsNeeded As Long, lngResult As Long
Dim hSCM As Long, hService As Long, i As Long
Dim lpEnumServiceStatus() As ENUM_SERVICE_STATUS
Dim strServiceName As String * 250

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ENUMERATE_SERVICE)

lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ByVal &H0, &H0, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
lngStructsNeeded = lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1

ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
lngServiceStatusInfoBuffer = lngStructsNeeded * Len(lpEnumServiceStatus(0))
lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, lpEnumServiceStatus(0), lngServiceStatusInfoBuffer, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)

For i = 0 To lngServicesReturned - 1
ReDim Preserve ServiceList(i)
'ServiceList(i) = lpEnumServiceStatus(i)
lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpServiceName)
ServiceList(i) = CheckStr(strServiceName)
'lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpDisplayName)
'ServiceList(i) = CheckStr(strServiceName)
Next i
CloseServiceHandle hSCM
End Function
'字符串结束
Private Function CheckStr(Str As String) As String
Dim i As Long
i = InStr(Str, Chr$(0))
If i Then
CheckStr = Left$(Str, i - 1)
Else
CheckStr = Str
End If
End Function

复制的
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式