On Mon, 19 Jul 2004 20:19:57 -0700, Chris Wagner wrote:
Anyone knows how to install/unistall service with VB.NET without using
InstallUtil program? Thanks
Here is code I use. I look for command line parameters of /INSTALL or
/UNINSTALL and install or uninstall the service depending on which one is
specified.
Imports System.ServiceP rocess
Imports System.Reflecti on
Module Service
'Service Control Manager object specific access types
Private Const STANDARD_RIGHTS _REQUIRED As Integer = &HF0000
Private Const SC_MANAGER_CONN ECT As Integer = &H1
Private Const SC_MANAGER_CREA TE_SERVICE As Integer = &H2
Private Const SC_MANAGER_ENUM ERATE_SERVICE As Integer = &H4
Private Const SC_MANAGER_LOCK As Integer = &H8
Private Const SC_MANAGER_QUER Y_LOCK_STATUS As Integer = &H10
Private Const SC_MANAGER_MODI FY_BOOT_CONFIG As Integer = &H20
Private Const SC_MANAGER_ALL_ ACCESS As Integer =
(STANDARD_RIGHT S_REQUIRED Or SC_MANAGER_CONN ECT Or
SC_MANAGER_CREA TE_SERVICE Or SC_MANAGER_ENUM ERATE_SERVICE Or
SC_MANAGER_LOCK Or SC_MANAGER_QUER Y_LOCK_STATUS Or
SC_MANAGER_MODI FY_BOOT_CONFIG)
'Service Access types
Private Const SERVICE_QUERY_C ONFIG As Integer = &H1
Private Const SERVICE_CHANGE_ CONFIG As Integer = &H2
Private Const SERVICE_QUERY_S TATUS As Integer = &H4
Private Const SERVICE_ENUMERA TE_DEPENDENTS As Integer = &H8
Private Const SERVICE_START As Integer = &H10
Private Const SERVICE_STOP As Integer = &H20
Private Const SERVICE_PAUSE_C ONTINUE As Integer = &H40
Private Const SERVICE_INTERRO GATE As Integer = &H80
Private Const SERVICE_USER_DE FINED_CONTROL As Integer = &H100
Private Const SERVICE_ALL_ACC ESS As Integer = (STANDARD_RIGHT S_REQUIRED
Or SERVICE_QUERY_C ONFIG Or SERVICE_CHANGE_ CONFIG Or SERVICE_QUERY_S TATUS Or
SERVICE_ENUMERA TE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or
SERVICE_PAUSE_C ONTINUE Or SERVICE_INTERRO GATE Or
SERVICE_USER_DE FINED_CONTROL)
Private Const SERVICE_WIN32_O WN_PROCESS As Integer = &H10
Private Const SERVICE_AUTO_ST ART As Integer = &H2
Private Const SERVICE_ERROR_N ORMAL As Integer = &H1
Declare Auto Function OpenSCManager Lib "advapi32.d ll" (ByVal sMachName
As String, ByVal sDbName As String, ByVal iAccess As Integer) As Integer
Declare Auto Function CloseServiceHan dle Lib "advapi32.d ll" (ByVal
sHandle As Integer) As Integer
Declare Auto Function CreateService Lib "advapi32.d ll" (ByVal hSCM As
Integer, ByVal sName As String, ByVal sDisplay As String, ByVal iAccess As
Integer, ByVal iSvcType As Integer, ByVal iStartType As Integer, ByVal
iError As Integer, ByVal sPath As String, ByVal sGroup As String, ByVal
iTag As Integer, ByVal sDepends As String, ByVal sUser As String, ByVal
sPass As String) As Integer
Declare Auto Function OpenService Lib "advapi32.d ll" (ByVal hSCManager
As Integer, ByVal lpServiceName As String, ByVal dwDesiredAccess As
Integer) As Integer
Declare Auto Function DeleteService Lib "advapi32.d ll" (ByVal hSvc As
Integer) As Boolean
Declare Auto Function GetLastError Lib "KERNEL32" () As Integer
Private Const ServiceName As String = "ServiceNam e"
Private Const DisplayName As String = "Service Display Text"
Public Sub InstallService( )
If ServiceInstalle d() Then
Exit Sub
End If
Dim hSCM As Integer = OpenSCManager(N othing, Nothing,
SC_MANAGER_ALL_ ACCESS)
If hSCM = 0 Then
MsgBox("Install Service: Unable to open Service Control Manager.
Error: " & GetLastError(). ToString)
Exit Sub
End If
Dim sExeName As String = [Assembly].GetExecutingAs sembly.Location
Dim hSvc As Integer = CreateService(h SCM, ServiceName, DisplayName,
SERVICE_ALL_ACC ESS, SERVICE_WIN32_O WN_PROCESS, SERVICE_AUTO_ST ART,
SERVICE_ERROR_N ORMAL, sExeName, Nothing, Nothing, Nothing, Nothing,
Nothing)
If hSvc = 0 Then
CloseServiceHan dle(hSvc)
MsgBox("Unable to install service, could not create service
handle. Error: " & GetLastError(). ToString)
Exit Sub
End If
CloseServiceHan dle(hSvc)
CloseServiceHan dle(hSCM)
End Sub
Public Sub UninstallServic e()
If Not ServiceInstalle d() Then
Exit Sub
End If
If Not StopService() Then
MsgBox("Uninsta llService: Could not stop service. Error: " &
GetLastError(). ToString)
Exit Sub
End If
Dim hSCM As Integer = OpenSCManager(N othing, Nothing,
SC_MANAGER_ALL_ ACCESS)
If hSCM = 0 Then
MsgBox("Uninsta llService: Unable to open Service Control
Manager. Error: " & GetLastError(). ToString)
Exit Sub
End If
Dim hSvc As Integer = OpenService(hSC M, ServiceName,
SERVICE_ALL_ACC ESS)
If hSvc = 0 Then
CloseServiceHan dle(hSCM)
MsgBox("Uninsta llService: Unable to open service: " &
GetLastError(). ToString)
Exit Sub
End If
If Not DeleteService(h Svc) Then
CloseServiceHan dle(hSvc)
CloseServiceHan dle(hSCM)
MsgBox("Uninsta llService: Unable to delete service: " &
GetLastError(). ToString)
End If
CloseServiceHan dle(hSvc)
CloseServiceHan dle(hSCM)
End Sub
Private Function ServiceInstalle d() As Boolean
Dim services() As ServiceControll er
services = ServiceControll er.GetServices( )
For Each sc As ServiceControll er In services
If sc.ServiceName = ServiceName Then
Return True
End If
Next
Return False
End Function
Private Function StopService() As Boolean
Dim iTimeout As Integer = 0
Dim bTimeout As Boolean = False
Dim svc As New ServiceControll er(ServiceName)
If svc.Status = ServiceControll erStatus.Stoppe d Then
Return True
End If
svc.Stop()
iTimeout = 0
While (svc.Status <> ServiceControll erStatus.Stoppe d)
System.Threadin g.Thread.Sleep( 1000)
iTimeout += 1
If iTimeout > 120 Then
bTimeout = True
Exit While
End If
svc.Refresh()
End While
Return Not bTimeout
End Function
End Module
--
Chris
dunawayc[AT]sbcglobal_lunch meat_[DOT]net
To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.