我正在尝试创建服务并根据我的需求更改其配置。创建不是问题,但是当我想更改服务设置时,它会失败。我想创建交互式服务。这是我的代码:
Public Function setInteractiveOption() As Boolean
Dim hSCManager As Long
Dim hService As Long
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
hService = OpenService(hSCManager, SERVICE_NAME, SERVICE_CHANGE_CONFIG)
Dim result As Long
result = ChangeServiceConfig(hService, SERVICE_WIN32_OWN_PROCESS Or SERVICE_INTERACTIVE_PROCESS, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull, vbNull)
CloseServiceHandle hService
If result Then
setInteractiveOption = True
Else
setInteractiveOption = False
End If
End Function
它给出了错误代码1057 - 帐户名无效或不存在,或者密码对于指定的帐户名无效。
修改:这是我的API声明:
Public 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 OpenService _
Lib "advapi32" Alias "OpenServiceA" _
(ByVal hSCManager As Long, ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Long) As Long
Private Declare Function OpenSCManager _
Lib "advapi32" Alias "OpenSCManagerA" _
(ByVal lpMachineName As String, ByVal lpDatabaseName As String, _
ByVal dwDesiredAccess As Long) As Long
Public Const SERVICE_NO_CHANGE = &HFFFFM
你可以在这里下载我的项目代码(我从互联网上下载了原始资料):
http://www.mediafire.com/?138esmdw5tvt19q
我在XP上测试了这个程序(并且失败了)但是在Windows 7中看起来很奇怪:我没有注册服务但是改变它的配置是成功的!
答案 0 :(得分:1)
如果错误消息抱怨该帐户,请从那里开始。您将帐户参数声明为字符串:
... ,ByVal lpServiceStartName As String, ByVal lpPassword As String
所以你需要使用vbNullString作为要传递的值。
或者,重新定义thjem:
..., ByVal lpServiceStartName As Long, ByVal lpPassword As Long
然后传递0&作为价值。
<强> EDIT1:强>
我改变了
Public Const SERVICE_NO_CHANGE = &HFFFF
到
Public Const SERVICE_NO_CHANGE = &HFFFFFFFF
并且,引用我自己已安装的服务,我调用了setInteractiveOption,它在XP专业版上取得了成功。我确保该服务的帐户设置为LocalSystem,如MSDN中所示。
<强> EDIT2:强>
这是decalre(您还必须使用EDIT1中的常量)
Public 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, _
ByVal lpdwTagId As String, _
ByVal lpDependencies As String, _
ByVal lpServiceStartName As String, _
ByVal lpPassword As String, _
ByVal lpDisplayName As String) As Long
这是你的方法:
Public Function setInteractiveOption() As Boolean
Dim hSCManager As Long
Dim hService As Long
hSCManager = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
MsgBox "hSCManager: " & hSCManager
hService = OpenService(hSCManager, SERVICE_NAME, SERVICE_CHANGE_CONFIG)
MsgBox "hService: " & hService
Dim result As Long
result = ChangeServiceConfig(hService, _
SERVICE_WIN32_OWN_PROCESS Or SERVICE_INTERACTIVE_PROCESS, _
SERVICE_NO_CHANGE, _
SERVICE_NO_CHANGE, _
vbNullString, _
vbNullString, _
vbNullString, _
vbNullString, _
vbNullString, _
vbNullString, _
vbNullString)
MsgBox "result: " & result & vbNewLine & "Error: " & Err.LastDllError
CloseServiceHandle hService
If result Then
setInteractiveOption = True
Else
setInteractiveOption = False
End If
End Function
然后我添加了一个按钮,只需将此方法调用到您的表单中,效果很好。