473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Install service

Anyone knows how to install/unistall service with VB.NET without using
InstallUtil program? Thanks
Nov 20 '05 #1
3 13761
* "Chris Wagner" <so@hotmail.com> scripsit:
Anyone knows how to install/unistall service with VB.NET without using
InstallUtil program?


Maybe this article helps:

HOW TO: Create a Setup Project for a Windows Service in Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;317421>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2
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.ServiceProcess
Imports System.Reflection

Module Service
'Service Control Manager object specific access types
Private Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Private Const SC_MANAGER_CONNECT As Integer = &H1
Private Const SC_MANAGER_CREATE_SERVICE As Integer = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE As Integer = &H4
Private Const SC_MANAGER_LOCK As Integer = &H8
Private Const SC_MANAGER_QUERY_LOCK_STATUS As Integer = &H10
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG As Integer = &H20
Private Const SC_MANAGER_ALL_ACCESS As Integer =
(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)

'Service Access types
Private Const SERVICE_QUERY_CONFIG As Integer = &H1
Private Const SERVICE_CHANGE_CONFIG As Integer = &H2
Private Const SERVICE_QUERY_STATUS As Integer = &H4
Private Const SERVICE_ENUMERATE_DEPENDENTS As Integer = &H8
Private Const SERVICE_START As Integer = &H10
Private Const SERVICE_STOP As Integer = &H20
Private Const SERVICE_PAUSE_CONTINUE As Integer = &H40
Private Const SERVICE_INTERROGATE As Integer = &H80
Private Const SERVICE_USER_DEFINED_CONTROL As Integer = &H100
Private Const SERVICE_ALL_ACCESS As Integer = (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_WIN32_OWN_PROCESS As Integer = &H10
Private Const SERVICE_AUTO_START As Integer = &H2
Private Const SERVICE_ERROR_NORMAL As Integer = &H1

Declare Auto Function OpenSCManager Lib "advapi32.dll" (ByVal sMachName
As String, ByVal sDbName As String, ByVal iAccess As Integer) As Integer
Declare Auto Function CloseServiceHandle Lib "advapi32.dll" (ByVal
sHandle As Integer) As Integer
Declare Auto Function CreateService Lib "advapi32.dll" (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.dll" (ByVal hSCManager
As Integer, ByVal lpServiceName As String, ByVal dwDesiredAccess As
Integer) As Integer
Declare Auto Function DeleteService Lib "advapi32.dll" (ByVal hSvc As
Integer) As Boolean
Declare Auto Function GetLastError Lib "KERNEL32" () As Integer

Private Const ServiceName As String = "ServiceName"
Private Const DisplayName As String = "Service Display Text"

Public Sub InstallService()

If ServiceInstalled() Then
Exit Sub
End If

Dim hSCM As Integer = OpenSCManager(Nothing, Nothing,
SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
MsgBox("InstallService: Unable to open Service Control Manager.
Error: " & GetLastError().ToString)
Exit Sub
End If

Dim sExeName As String = [Assembly].GetExecutingAssembly.Location

Dim hSvc As Integer = CreateService(hSCM, ServiceName, DisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, sExeName, Nothing, Nothing, Nothing, Nothing,
Nothing)
If hSvc = 0 Then
CloseServiceHandle(hSvc)
MsgBox("Unable to install service, could not create service
handle. Error: " & GetLastError().ToString)
Exit Sub
End If

CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)

End Sub

Public Sub UninstallService()

If Not ServiceInstalled() Then
Exit Sub
End If

If Not StopService() Then
MsgBox("UninstallService: Could not stop service. Error: " &
GetLastError().ToString)
Exit Sub
End If

Dim hSCM As Integer = OpenSCManager(Nothing, Nothing,
SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
MsgBox("UninstallService: Unable to open Service Control
Manager. Error: " & GetLastError().ToString)
Exit Sub
End If

Dim hSvc As Integer = OpenService(hSCM, ServiceName,
SERVICE_ALL_ACCESS)
If hSvc = 0 Then
CloseServiceHandle(hSCM)
MsgBox("UninstallService: Unable to open service: " &
GetLastError().ToString)
Exit Sub
End If

If Not DeleteService(hSvc) Then
CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)
MsgBox("UninstallService: Unable to delete service: " &
GetLastError().ToString)
End If

CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)

End Sub

Private Function ServiceInstalled() As Boolean

Dim services() As ServiceController
services = ServiceController.GetServices()

For Each sc As ServiceController 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 ServiceController(ServiceName)
If svc.Status = ServiceControllerStatus.Stopped Then
Return True
End If

svc.Stop()
iTimeout = 0
While (svc.Status <> ServiceControllerStatus.Stopped)
System.Threading.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_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 20 '05 #3
Do you know a way to use managed code to install/uninstall service? Thanks,

Anyone?
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1r***************************@40tude.net...
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.ServiceProcess
Imports System.Reflection

Module Service
'Service Control Manager object specific access types
Private Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Private Const SC_MANAGER_CONNECT As Integer = &H1
Private Const SC_MANAGER_CREATE_SERVICE As Integer = &H2
Private Const SC_MANAGER_ENUMERATE_SERVICE As Integer = &H4
Private Const SC_MANAGER_LOCK As Integer = &H8
Private Const SC_MANAGER_QUERY_LOCK_STATUS As Integer = &H10
Private Const SC_MANAGER_MODIFY_BOOT_CONFIG As Integer = &H20
Private Const SC_MANAGER_ALL_ACCESS As Integer =
(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)

'Service Access types
Private Const SERVICE_QUERY_CONFIG As Integer = &H1
Private Const SERVICE_CHANGE_CONFIG As Integer = &H2
Private Const SERVICE_QUERY_STATUS As Integer = &H4
Private Const SERVICE_ENUMERATE_DEPENDENTS As Integer = &H8
Private Const SERVICE_START As Integer = &H10
Private Const SERVICE_STOP As Integer = &H20
Private Const SERVICE_PAUSE_CONTINUE As Integer = &H40
Private Const SERVICE_INTERROGATE As Integer = &H80
Private Const SERVICE_USER_DEFINED_CONTROL As Integer = &H100
Private Const SERVICE_ALL_ACCESS As Integer = (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_WIN32_OWN_PROCESS As Integer = &H10
Private Const SERVICE_AUTO_START As Integer = &H2
Private Const SERVICE_ERROR_NORMAL As Integer = &H1

Declare Auto Function OpenSCManager Lib "advapi32.dll" (ByVal sMachName
As String, ByVal sDbName As String, ByVal iAccess As Integer) As Integer
Declare Auto Function CloseServiceHandle Lib "advapi32.dll" (ByVal
sHandle As Integer) As Integer
Declare Auto Function CreateService Lib "advapi32.dll" (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.dll" (ByVal hSCManager
As Integer, ByVal lpServiceName As String, ByVal dwDesiredAccess As
Integer) As Integer
Declare Auto Function DeleteService Lib "advapi32.dll" (ByVal hSvc As
Integer) As Boolean
Declare Auto Function GetLastError Lib "KERNEL32" () As Integer

Private Const ServiceName As String = "ServiceName"
Private Const DisplayName As String = "Service Display Text"

Public Sub InstallService()

If ServiceInstalled() Then
Exit Sub
End If

Dim hSCM As Integer = OpenSCManager(Nothing, Nothing,
SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
MsgBox("InstallService: Unable to open Service Control Manager.
Error: " & GetLastError().ToString)
Exit Sub
End If

Dim sExeName As String = [Assembly].GetExecutingAssembly.Location

Dim hSvc As Integer = CreateService(hSCM, ServiceName, DisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, sExeName, Nothing, Nothing, Nothing, Nothing,
Nothing)
If hSvc = 0 Then
CloseServiceHandle(hSvc)
MsgBox("Unable to install service, could not create service
handle. Error: " & GetLastError().ToString)
Exit Sub
End If

CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)

End Sub

Public Sub UninstallService()

If Not ServiceInstalled() Then
Exit Sub
End If

If Not StopService() Then
MsgBox("UninstallService: Could not stop service. Error: " &
GetLastError().ToString)
Exit Sub
End If

Dim hSCM As Integer = OpenSCManager(Nothing, Nothing,
SC_MANAGER_ALL_ACCESS)
If hSCM = 0 Then
MsgBox("UninstallService: Unable to open Service Control
Manager. Error: " & GetLastError().ToString)
Exit Sub
End If

Dim hSvc As Integer = OpenService(hSCM, ServiceName,
SERVICE_ALL_ACCESS)
If hSvc = 0 Then
CloseServiceHandle(hSCM)
MsgBox("UninstallService: Unable to open service: " &
GetLastError().ToString)
Exit Sub
End If

If Not DeleteService(hSvc) Then
CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)
MsgBox("UninstallService: Unable to delete service: " &
GetLastError().ToString)
End If

CloseServiceHandle(hSvc)
CloseServiceHandle(hSCM)

End Sub

Private Function ServiceInstalled() As Boolean

Dim services() As ServiceController
services = ServiceController.GetServices()

For Each sc As ServiceController 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 ServiceController(ServiceName)
If svc.Status = ServiceControllerStatus.Stopped Then
Return True
End If

svc.Stop()
iTimeout = 0
While (svc.Status <> ServiceControllerStatus.Stopped)
System.Threading.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_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Owe Armandt | last post by:
Having a problem to install a C# service on my server. The service installs fine on other machines but not on this server. I have installed other services previously though. The error message...
2
by: Jim Slade | last post by:
I downloaded the .NET Framework SP2 exe to a Windows Server 2003 (standard) machine, double clicked on the exe and got a message that starts out: "The upgrade patch cannot be installed by the...
2
by: Harshad | last post by:
Two questions. 1) I followed Walkthrough of Win Service App. At the end, when I run Setup it complains about incorrect user and/or password. Where does it look for userid? I have logged in as...
3
by: Ryan | last post by:
I have a simple project, most of it generated by Visual Studio. Public Windows Service Installer (Also generated), with : public class ProjectInstaller : System.Configuration.Install.Installer...
3
by: David Preuss | last post by:
Hello all, hopefully someone can help me. I have got a service which I want to deploy. I added an installer project to my solution and did everything as stated in some kb articles (eg adding the...
5
by: timburda | last post by:
Scenario: I have a service which has been installed in the service component manager. I am writing a second program (completely unrelated to the service) which will run stand alone. I can...
4
by: kkt49 | last post by:
# vim: et sw=4 ts=8 sts from wxPython.wx import * import sys, os, time import pywintypes import win32serviceutil import win32service import win32event import win32process
3
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have made a Windows Service and I have read that you should use installutil.exe to register the service. However, no service appears in the Control Panel Administrative Tools Services when I run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.