Gå til innhold

CreateService, ingenting fungerer!


Anbefalte innlegg

Jeg prover meg pa CreateService i VB6, skal innstalere en kernel driver, men ingen av funksjonene fungerer!

 

Er det noen her som har erfaring med VB6 Services etc?

 

btw, her er koden min: (clsService)

Option Explicit

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 Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000

Private Const ERROR_SERVICE_ALREADY_RUNNING As Long = 1056&
Private Const ERROR_SERVICE_MARKED_FOR_DELETE As Long = 1072&
Private Const ERROR_SERVICE_EXISTS As Long = 1073&
Private Const ERROR_SERVICE_DATABASE_LOCKED As Long = 1055&
Private Const ERROR_SERVICE_DEPENDENCY_DELETED As Long = 1075&
Private Const ERROR_SERVICE_DEPENDENCY_FAIL As Long = 1068&
Private Const ERROR_SERVICE_DISABLED As Long = 1058&
Private Const ERROR_SERVICE_LOGON_FAILED As Long = 1069&
Private Const ERROR_SERVICE_NO_THREAD As Long = 1054&
Private Const ERROR_SERVICE_REQUEST_TIMEOUT As Long = 1053&
Private Const ERROR_SERVICE_CANNOT_ACCEPT_CTRL As Long = 1061&
Private Const ERROR_SERVICE_NOT_ACTIVE As Long = 1062&

Private Const ERROR_INVALID_HANDLE As Long = 6&
Private Const ERROR_INVALID_NAME As Long = 123&
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_INVALID_SERVICE_ACCOUNT As Long = 1057&
Private Const ERROR_INVALID_SERVICE_CONTROL As Long = 1052&

Private Const ERROR_PATH_NOT_FOUND As Long = 3&
Private Const ERROR_ACCESS_DENIED As Long = 5&
Private Const ERROR_CIRCULAR_DEPENDENCY As Long = 1059&
Private Const ERROR_DUP_NAME As Long = 52&
Private Const ERROR_DEPENDENT_SERVICES_RUNNING As Long = 1051&
Private Const ERROR_SHUTDOWN_IN_PROGRESS As Long = 1115&

Private Const SC_MANAGER_CONNECT As Long = &H1
Private Const SC_MANAGER_CREATE_SERVICE As Long = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE As Long = &H4
Private Const SC_MANAGER_LOCK As Long = &H8
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG As Long = &H20
Private Const SC_MANAGER_QUERY_LOCK_STATUS As Long = &H10
Private Const SC_MANAGER_ALL_ACCESS As Long = (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 As Long = &H1
Private Const SERVICE_CHANGE_CONFIG As Long = &H2
Private Const SERVICE_QUERY_STATUS As Long = &H4
Private Const SERVICE_ENUMERATE_DEPENDENTS As Long = &H8
Private Const SERVICE_START As Long = &H10
Private Const SERVICE_STOP As Long = &H20
Private Const SERVICE_PAUSE_CONTINUE As Long = &H40
Private Const SERVICE_INTERROGATE As Long = &H80
Private Const SERVICE_USER_DEFINED_CONTROL As Long = &H100
Private Const SERVICE_ALL_ACCESS As Long = (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_KERNEL_DRIVER As Long = &H1
Private Const SERVICE_DEMAND_START As Long = &H3
Private Const SERVICE_ERROR_NORMAL As Long = &H1
Private Const SERVICE_CONTROL_STOP As Long = &H1
Private Const SERVICE_BOOT_START As Long = &H0

Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long 'SC_HANDLE OpenSCManager(
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, ByRef lpServiceStatus As SERVICE_STATUS) 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, ByRef lpdwTagId As Long, ByVal lpDependencies As String, ByVal lp As String, ByVal lpPassword As String) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Private Declare Function GetLastError Lib "kernel32.dll" () 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 DeleteService Lib "advapi32.dll" (ByVal hService As Long) As Long
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Public Function GetErrorDescription(lError As Long, nFunction As Long) As String
Dim strText As String
Select Case nFunction
   Case 1: 'Install
       Select Case lError
           Case ERROR_ACCESS_DENIED: strText = "The handle to the specified service control manager database does not have SC_MANAGER_CREATE_SERVICE access."
           Case ERROR_CIRCULAR_DEPENDENCY: strText = "A circular service dependency was specified."
           Case ERROR_DUP_NAME: strText = "The display name already exists in the service control manager database either as a service name or as another display name."
           Case ERROR_INVALID_HANDLE: strText = "The handle to the specified service control manager database is invalid."
           Case ERROR_INVALID_NAME: strText = "The specified service name is invalid."
           Case ERROR_INVALID_PARAMETER: strText = "A parameter that was specified is invalid."
           Case ERROR_INVALID_SERVICE_ACCOUNT: strText = "The user account name specified in the lpServiceStartName parameter does not exist."
           Case ERROR_SERVICE_EXISTS: strText = "The specified service already exists in this database."
       End Select
   Case 2: 'Uninstall
       Select Case lError
           Case ERROR_ACCESS_DENIED: strText = "The specified handle was not opened with DELETE access."
           Case ERROR_INVALID_HANDLE: strText = "The specified handle is invalid."
           Case ERROR_SERVICE_MARKED_FOR_DELETE: strText = "The specified service has already been marked for deletion."
       End Select
   Case 3: 'Stop
       Select Case lError
           Case ERROR_ACCESS_DENIED: strText = "The specified handle was not opened with the necessary access."
           Case ERROR_DEPENDENT_SERVICES_RUNNING: strText = "The service cannot be stopped because other running services are dependent on it."
           Case ERROR_INVALID_HANDLE: strText = "The specified handle was not obtained using CreateService or OpenService, or the handle is no longer valid."
           Case ERROR_INVALID_PARAMETER: strText = "The requested control code is undefined."
           Case ERROR_INVALID_SERVICE_CONTROL: strText = "The requested control code is not valid, or it is unacceptable to the service."
           Case ERROR_SERVICE_CANNOT_ACCEPT_CTRL: strText = "The requested control code cannot be sent to the service because the state of the service is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING."
           Case ERROR_SERVICE_NOT_ACTIVE: strText = "The service has not been started."
           Case ERROR_SERVICE_REQUEST_TIMEOUT: strText = "The process for the service was started, but it did not call StartServiceCtrlDispatcher, or the thread that called StartServiceCtrlDispatcher may be blocked in a control handler function."
           Case ERROR_SHUTDOWN_IN_PROGRESS: strText = "The system is shutting down."
       End Select
   Case 4: 'Start
       Select Case lError
           Case ERROR_ACCESS_DENIED: strText = "The specified handle was not opened with SERVICE_START access."
           Case ERROR_INVALID_HANDLE: strText = "The specified handle is invalid."
           Case ERROR_PATH_NOT_FOUND: strText = "The service binary file could not be found."
           Case ERROR_SERVICE_ALREADY_RUNNING: strText = "An instance of the service is already running."
           Case ERROR_SERVICE_DATABASE_LOCKED: strText = "The database is locked."
           Case ERROR_SERVICE_DEPENDENCY_DELETED: strText = "The service depends on a service that does not exist or has been marked for deletion."
           Case ERROR_SERVICE_DEPENDENCY_FAIL: strText = "The service depends on another service that has failed to start."
           Case ERROR_SERVICE_DISABLED: strText = "The service has been disabled."
           Case ERROR_SERVICE_LOGON_FAILED: strText = "The service could not be logged on."
           Case ERROR_SERVICE_MARKED_FOR_DELETE: strText = "The service has been marked for deletion."
           Case ERROR_SERVICE_NO_THREAD: strText = "A thread could not be created for the service."
           Case ERROR_SERVICE_REQUEST_TIMEOUT: strText = "The process for the service was started, but it did not call StartServiceCtrlDispatcher, or the thread that called StartServiceCtrlDispatcher may be blocked in a control handler function."
       End Select
End Select
GetErrorDescription = strText
End Function

Public Function Start(strName As String) As Boolean
Dim hService As Long, hSCM As Long, ret As Long, SS As SERVICE_STATUS
Start = False

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
   Exit Function
End If

hService = OpenService(hSCM, strName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
   ret = StartService(hService, 0, 0)
   If ret <> 0 Then
       Start = True
   End If
End If

CloseServiceHandle hSCM
CloseServiceHandle hService
End Function

Public Function Uninstall(strName As String) As Boolean
Dim hService As Long, hSCM As Long, ret As Long

Uninstall = False

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
   Exit Function
End If

hService = OpenService(hSCM, strName, SERVICE_ALL_ACCESS)

If hService <> 0 Then
   ret = DeleteService(hService)
   If ret <> 0 Then Uninstall = True
End If

CloseServiceHandle hSCM
CloseServiceHandle hService
End Function

Public Function Install(strName As String, strFile As String, Optional bBoot As Boolean = False) As Boolean
Dim hService As Long, hSCM As Long, ret As Long
Install = False

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_CREATE_SERVICE)
If hSCM = 0 Then
   Debug.Print "INSTALL", "hSCM = 0"
   Exit Function
End If

Dim lFlag As Long
If bBoot Then
   lFlag = SERVICE_BOOT_START
Else
   lFlag = SERVICE_DEMAND_START
End If

If Not Left(strFile, 1) = Chr(34) Then strFile = Chr(34) & strFile & Chr(34)

hService = CreateService(hSCM, strName, strName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, lFlag, SERVICE_ERROR_NORMAL, strFile, 0, 0, vbNullString, strName, 0)
If hService <> 0 Then
   Install = True
Else
   Sleep 100
   DoEvents
   ret = GetLastError
   If ret <> 0 Then
       Dim strText As String
       Select Case ret
           Case ERROR_ACCESS_DENIED: strText = "The handle to the specified service control manager database does not have SC_MANAGER_CREATE_SERVICE access."
           Case ERROR_CIRCULAR_DEPENDENCY: strText = "A circular service dependency was specified."
           Case ERROR_DUP_NAME: strText = "The display name already exists in the service control manager database either as a service name or as another display name."
           Case ERROR_INVALID_HANDLE: strText = "The handle to the specified service control manager database is invalid."
           Case ERROR_INVALID_NAME: strText = "The specified service name is invalid."
           Case ERROR_INVALID_PARAMETER: strText = "A parameter that was specified is invalid."
           Case ERROR_INVALID_SERVICE_ACCOUNT: strText = "The user account name specified in the lpServiceStartName parameter does not exist."
           Case ERROR_SERVICE_EXISTS: strText = "The specified service already exists in this database."
       End Select

       Err.Raise ret, "INSTALL", strText
   End If
End If
CloseServiceHandle hSCM
CloseServiceHandle hService
End Function

Public Function Pause(strName As String) As Boolean
Dim hService As Long, hSCM As Long, ret As Long, SS As SERVICE_STATUS
Pause = False

hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
   Exit Function
End If

hService = OpenService(hSCM, strName, SERVICE_ALL_ACCESS)
If hService <> 0 Then
   ret = ControlService(hService, SERVICE_CONTROL_STOP, SS)
   If ret <> 0 Then
       Pause = True
   End If
End If
CloseServiceHandle hSCM
CloseServiceHandle hService
End Function

Lenke til kommentar
Videoannonse
Annonse

um det var da veldig så mye unødvendig kode du hadde der

det er ikke så lurt å ta det første du finner på nettet

let etter noe enklere

jeg har en modul jeg bruker (har laget) selv men jeg trokke den hjelper deg så veldig

 

koden du har er jo oversiktlig og grei, og veldig overkill :p

 

har sett igjennom koden og...

det er ingen kode der for å lage en windows service klient

det du har er kode for å lage, starte, stoppe ntservices..

Lenke til kommentar

Det er ikke kode for og gjore om mitt programm til en service, men for og innstalere en service(.sys fil, drivere etc), med funksjone til og innstalere, uinstalere, start og stop

 

btw, har skrevet alt selv, og ikke bare kopiert fra nettet...

 

Btw, den modulen din, hvordan funker den?

Lenke til kommentar

den sier fra at en service er startet / stoppet

dvs gjør prosessen om til en ntservice

 

jeg kan ikke sjekke koden din for feil, jeg kan ikke ntservice api'et

hvem kan vel det...

uansett, bruk Install "servicename", "filepath", (boolean) autorun

men det visste du vel allerede.. :p

 

en ting... jeg veit ikke om du bare kan sette den som SERVICE_KERNEL_DRIVER

mulig windows freaker ut pga manglende privileges

hvem vet

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...