2013-04-22 1 views
2

설명이있는 서비스를 만드는 프로그램을 만들고 있습니다. 현재 코드는 서비스를 만들 수 있지만 설명을 만들 수는 없습니다. 내 현재 코드VB6에서 서비스 설명 만들기

모듈 :

Option Explicit 
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Private Const ERROR_SERVICE_DOES_NOT_EXIST = 1060& 

Private Const SERVICE_WIN32_OWN_PROCESS = &H10& 
'Private Const SERVICE_WIN32_SHARE_PROCESS = &H20& 
'Private Const SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS + _ 
           SERVICE_WIN32_SHARE_PROCESS 
'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 STANDARD_RIGHTS_REQUIRED = &HF0000 
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 SERVICE_AUTO_START As Long = 2 
Private Const SERVICE_DEMAND_START As Long = 3 
Private Const SERVICE_ERROR_NORMAL As Long = 1 
Private Const ERROR_INSUFFICIENT_BUFFER = 122& 
Private Enum SERVICE_CONTROL 
    SERVICE_CONTROL_STOP = 1& 
    SERVICE_CONTROL_PAUSE = 2& 
    SERVICE_CONTROL_CONTINUE = 3& 
    SERVICE_CONTROL_INTERROGATE = 4& 
    SERVICE_CONTROL_SHUTDOWN = 5& 
End Enum 
Public Enum SERVICE_STATE 
    UNINSTALLED = &H0 
    STOPPED = &H1 
    START_PENDING = &H2 
    STOP_PENDING = &H3 
    RUNNING = &H4 
    CONTINUE_PENDING = &H5 
    PAUSE_PENDING = &H6 
    PAUSED = &H7 
End Enum 
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 
Public Type QUERY_SERVICE_CONFIG 
    dwServiceType As Long 
    dwStartType As Long 
    dwErrorControl As Long 
    lpBinaryPathName As Long 
    lpLoadOrderGroup As Long 
    dwTagId As Long 
    lpDependencies As Long 
    lpServiceStartName As Long 
    lpDisplayName As Long 
End Type 
Private Declare Function OpenSCManager _ 
     Lib "advapi32" Alias "OpenSCManagerA" _ 
     (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _ 
     ByVal dwDesiredAccess As Long) As Long 
Private Declare Function CreateService _ 
     Lib "advapi32" 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, ByVal lpdwTagId As String, _ 
     ByVal lpDependencies As String, ByVal lp As String, _ 
     ByVal lpPassword As String) As Long 
Private Declare Function DeleteService _ 
     Lib "advapi32" (ByVal hService As Long) As Long 
Private Declare Function CloseServiceHandle _ 
     Lib "advapi32" (ByVal hSCObject As Long) As Long 
Private Declare Function OpenService _ 
     Lib "advapi32" Alias "OpenServiceA" _ 
     (ByVal hSCManager As Long, ByVal lpServiceName As String, _ 
     ByVal dwDesiredAccess As Long) As Long '** Change SERVICE_NAME as needed 
Private Declare Function QueryServiceConfig Lib "advapi32" _ 
     Alias "QueryServiceConfigA" (ByVal hService As Long, _ 
     lpServiceConfig As QUERY_SERVICE_CONFIG, _ 
     ByVal cbBufSize As Long, pcbBytesNeeded As Long) As Long 
Private Declare Function QueryServiceStatus Lib "advapi32" _ 
    (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long 
Private Declare Function ControlService Lib "advapi32" _ 
     (ByVal hService As Long, ByVal dwControl As SERVICE_CONTROL, _ 
     lpServiceStatus As SERVICE_STATUS) As Long 
Private Declare Function StartService Lib "advapi32" _ 
     Alias "StartServiceA" (ByVal hService As Long, _ 
     ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long 
Private Declare Function NetWkstaUserGetInfo Lib "Netapi32" (ByVal Reserved As Any, ByVal Level As Long, lpBuffer As Any) As Long 
Private Declare Function NetApiBufferFree Lib "Netapi32" (ByVal lpBuffer As Long) As Long 

Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long 
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long 

Private Enum InfoLevel 
    SERVICE_CONFIG_DESCRIPTION = 1 
    SERVICE_CONFIG_FAILURE_ACTIONS = 2 
End Enum 

Private Declare Function ChangeServiceConfig2 Lib "advapi32.dll" Alias "ChangeServiceConfig2W" (ByVal hService As Long, ByVal dwInfoLevel As InfoLevel, lpInfo As Any) As Boolean 


Private Type SERVICE_DESCRIPTIONW 
    lpDescription As String 
End Type 


' This function installs service on local computer 
' It returns nonzero value on error 
Public Function CreateNTService(Title As String, Nick As String, Link As String, Description As String) As Long 
Dim lp As SERVICE_DESCRIPTIONW 
lp.lpDescription = Description 
Dim hSCManager As Long 
Dim hService As Long, DomainName As String 
Dim CSC   As Long 

hSCManager = OpenSCManager(vbNullString, vbNullString, _ 
         SC_MANAGER_CREATE_SERVICE) 
If hSCManager <> 0 Then 
' Install service to manual start. To set service to autostart 
' replace SERVICE_DEMAND_START to SERVICE_AUTO_START 
    hService = CreateService(hSCManager, Nick, _ 
         Title, SERVICE_ALL_ACCESS, _ 
         SERVICE_WIN32_OWN_PROCESS, _ 
         SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _ 
         Link, vbNullString, _ 
         vbNullString, vbNullString, "LocalSystem", _ 
         "") 
    CSC = ChangeServiceConfig2(hService, 1, lp) 
    If hService <> 0 Then 
     CloseServiceHandle hService 
    Else 
     CreateNTService = Err.LastDllError 
    End If 

    CloseServiceHandle hSCManager 
Else 
    CreateNTService = Err.LastDllError 
End If 
End Function 

형태 :

Private Sub Form_Load() 
CreateNTService "a", "a", "d:\link", "description" 
End 
End Sub 

문제는이 코드가 약간의 설명을 작성 할 수 있지만, 생성 된 설명은 "敤 捳 楲 瑰 潩 N"이라는 것이다 "설명"이 아님

답변

1

내가보기에 문제는 유니 코드 버전의 ChangeServiceConfig2를 올바르게 사용하는 동안 String 멤버로 UDT를 선언한다는 것입니다. 불행하게도, VB는 String 타입을 보았을 때 자동으로 유니 코드에서 로컬 코드 페이지로 변환합니다. 이것은 당신의 묘사를 엉망으로 만듭니다. 해결 방법은 Long 멤버를 사용하여 구조체를 선언 한 다음이 값을 문자열 포인터의 값으로 설정하는 것입니다.

Option Explicit 

Private Const STANDARD_RIGHTS_REQUIRED = &HF0000 
Private Const SERVICE_WIN32_OWN_PROCESS = &H10& 
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 SERVICE_DEMAND_START As Long = 3 
Private Const SERVICE_ERROR_NORMAL As Long = 1 

Private Const SC_MANAGER_CREATE_SERVICE = &H2& 

Private Declare Function OpenSCManager _ 
     Lib "advapi32" Alias "OpenSCManagerA" _ 
     (ByVal lpMachineName As String, ByVal lpDatabaseName As String, _ 
     ByVal dwDesiredAccess As Long) As Long 
Private Declare Function CreateService _ 
     Lib "advapi32" 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, ByVal lpdwTagId As String, _ 
     ByVal lpDependencies As String, ByVal lp As String, _ 
     ByVal lpPassword As String) As Long 

Private Declare Function CloseServiceHandle _ 
     Lib "advapi32" (ByVal hSCObject As Long) As Long 

Private Enum InfoLevel 
    SERVICE_CONFIG_DESCRIPTION = 1 
    SERVICE_CONFIG_FAILURE_ACTIONS = 2 
End Enum 

Private Declare Function ChangeServiceConfig2 Lib "advapi32.dll" Alias "ChangeServiceConfig2W" (ByVal hService As Long, ByVal dwInfoLevel As InfoLevel, ByRef lpInfo As SERVICE_DESCRIPTIONW) As Boolean 

Private Type SERVICE_DESCRIPTIONW 
    lpDescription As Long 
End Type 

' This function installs service on local computer 
' It returns nonzero value on error 
Public Function CreateNTService(ByRef Title As String, ByRef ServiceName As String, ByRef BinaryPath As String, ByRef Description As String) As Long 

    Dim hSCManager  As Long 
    Dim hService  As Long 
    Dim DomainName  As String 
    Dim CSC    As Long 
    Dim lp    As SERVICE_DESCRIPTIONW 

    hSCManager = OpenSCManager(vbNullString, vbNullString, _ 
          SC_MANAGER_CREATE_SERVICE) 

    If hSCManager <> 0 Then 
    ' Install service to manual start. To set service to autostart 
    ' replace SERVICE_DEMAND_START to SERVICE_AUTO_START 
     hService = CreateService(hSCManager, ServiceName, _ 
          Title, SERVICE_ALL_ACCESS, _ 
          SERVICE_WIN32_OWN_PROCESS, _ 
          SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _ 
          BinaryPath, vbNullString, _ 
          vbNullString, vbNullString, "LocalSystem", _ 
          "") 

     lp.lpDescription = StrPtr(Description) ' Set the string pointer. 
     CSC = ChangeServiceConfig2(hService, 1, lp) 
     If hService <> 0 Then 
      CloseServiceHandle hService 
     Else 
      CreateNTService = Err.LastDllError 
     End If 

     CloseServiceHandle hSCManager 
    Else 
     CreateNTService = Err.LastDllError 
    End If 

End Function 
3

은 U + 6564입니다. 그것은 분명합니다 : "d"는 ASCII 100 (64 hex)이고 "3"은 101 (65 hex)입니다. 나는. 특히 문자 너비와 관련된 문자열 마샬링 문제가 있습니다. 이 아닌 "*A"으로 전화하십시오.

[편집] 죄송합니다. "설명"이 실제로 사용 된 곳을 간과합니다. 해야만합니다 ChangeServiceConfig2A

+0

Ok 답장을 보내 주셔서 감사합니다. 나는 당신이 말한 것과는 정반대로,'ChangeServiceConfig2W'를'ChangeServiceConfig2A'로 대체합니다. 그것은 효과가 있습니다. 나는 당신이 당신의 대답을 편집해야한다고 생각합니다. –