开发者

Failed to change service config

开发者 https://www.devze.com 2023-04-04 11:04 出处:网络
I\', trying to create a service and change its configurations accourding to my needs. creation is not a problem but when I want to change service settings it fails. I want to create Interactive servic

I', trying to create a service and change its configurations accourding to my needs. creation is not a problem but when I want to change service settings it fails. I want to create Interactive service. here's my code:

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

it gives me error code 1057 - The account name is开发者_Go百科 invalid or does not exist, or the password is invalid for the account name specified.

Edit: here is my API delaration:

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

You can download code of my project here(I downloaded orginal source from Internet):

http://www.mediafire.com/?138esmdw5tvt19q

I tested this program on XP (and failed) but in windows 7 it seems weird : I doesn't register service but changing its configuration is successful!


If the error message is complaining about the account, then start there. You have the account parameters declared as string:

... ,ByVal lpServiceStartName As String, ByVal lpPassword As String

so you will need to use vbNullString as the value to pass.

Alternatively, redefine thjem as long:

..., ByVal lpServiceStartName As Long, ByVal lpPassword As Long

and then pass 0& as the value.

EDIT1:

I changed

Public Const SERVICE_NO_CHANGE = &HFFFF 

to

Public Const SERVICE_NO_CHANGE = &HFFFFFFFF 

and, referencing my own service that was already installed, I called setInteractiveOption and it was successful on XP Pro. I made sure that the account of the service is setup as LocalSystem, as indicated in the MSDN.

EDIT2:

Here is the decalre (you must also use the constant from 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

Here is your method:

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

I then added a button to call just this method to your form and it worked great.

0

精彩评论

暂无评论...
验证码 换一张
取 消