473,799 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Install service

Anyone knows how to install/unistall service with VB.NET without using
InstallUtil program? Thanks
Nov 20 '05 #1
3 13790
* "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.microso ft.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.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.
Nov 20 '05 #3
Do you know a way to use managed code to install/uninstall service? Thanks,

Anyone?
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcg lobal[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.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.

Nov 20 '05 #4

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

Similar topics

0
1220
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 from InstallUtil is: "No public installers with the RunInstallerAttribute.Yes attribute could be found" Any clue to what is missing??
2
1891
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 Windows Installer Service because the program to be upgraded may be missing, or the upgrade patch may upgrade a different version of the program..." I don't understand. This server does in fact have the framework installed (1.1.4322); and was...
2
1956
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 Administrator. 2) Why cannot I install the service with ServiceName.exe /install command as I do for non-dotNet service? -- Thanks for your help.
3
12650
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
3751
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 .exe and on and on.). Everything is deployed to the folders but the service itself is not installed. Can someone give some hints on how to get the service installed?
5
13539
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 find all of the services on the machine with the following line of code: System.ServiceProcess.ServiceController foo =
4
3184
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
16902
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 installutil. How can I install my service? I get one message when using installutil that I don't know if it is an error message or not: "No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\... assembly.
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.