Building Windows Services using Visual Basic 2005 Express

 

Build and Install service on your pc:

Go to build menu and choose Build NewService1 – This will create an EXE file in our project’s \bin\Release folder.

Next we will need to use the InstallUtil.exe to install the service into the PC. 

InstallUtil.exe is part of the Dot Net distribution package. 
It is found in the c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder. 

Its command line usage is: 

Install Service: 
Installutil.exe PATH_TO_EXE.exe

Uninstall Service: 
InstallUtil.exe PATH_TO_EXE.exe /u

For this example, I changed directory into the c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder. 
Then I ran: 

InstallUtil.exe “c:\vb2005\projects\NewService1\bin\Release\NewService1.exe”

The installer should give you lots of text, It should end by saying: 

The Commit phase completed successfully. 
The transacted install has completed.

If there are any error messages in the install of the service, it will list them. 

After successful installation, you should see the Service listed as NewService1 in the Service Manager. 
To get the service manager, Right click on My Computer on the desktop, and choose Manage.

ServiceServiceManager

Then choose Services and Applications > Services. Right-Click on the NewService1 and choose Start. 

Then go to System Tools > Event Viewer > Application, and you should see your log entries. 

The service will make one entry saying “It is running” then disable the Timer so your log doesn’t fill up. 

To install this service on another computer:

Prerequisites: You need to have .net distributable 2.0 on the target PC. 

You need to copy over the files in the Release folder to the target computer. Then inside the c:\windows\Microsoft.net\Firmware\v2.0.50727\ folder run the InstallUtil.exe program as described previously. 

That’s It.

 

Programmers – Use your talents to make some money online.  Click Here to Read More

 

Author: Nick Jolin

My name is Nick Jolin and I’m a full time entrepreneur making a living by creating wonderful tools that help people with online marketing.

Connect with me on Google+

8 thoughts on “Building Windows Services using Visual Basic 2005 Express”

  1. This code runs fine, and the service runs OK, but it stops the service after approx 2 minutes. I can’t see anything that is telling the service to stop, and also, if I did want to programmatically stop the service, how would I go about this?

  2. Great intro into getting a service up and running with vb. Thanks! I was able to get the service running on Windows 7 64-bit with .net runtime 4.x only after switching to the standard (x86) directory for InstallUtil.exe _and_ running the command prompt as Administrator. Wish there was more of an explanation of the code, but that’s just my unfamiliarity with all the shiny new goodness of the VB lang these days and the .Net environment in general. Again, great intro (for a seasoned programmer, IMHO)

  3. error is “Warning 1 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

    can anyone pls help on this when i build ,thanks in advance

  4. error in mylog.SourceExists

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As _
    System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
    Dim MyLog As New EventLog() ‘ create a new event log

    ‘ Check if the the Event Log Exists
    If Not MyLog.SourceExists(“NewService1”) Then ‘error here
    MyLog.CreateEventSource(“NewService1”, “NewService1 Log”) ‘ Create Log
    End If
    MyLog.Source = “NewService1”
    MyLog.WriteEntry(“NewService1 Log”, “It is running”, EventLogEntryType.Information)
    ‘disable the timer so you dont fill up the log
    Timer1.Enabled = False
    End Sub

  5. i am sorry for untidy code, here i repost:
    error is MyLog.SourceExists

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As _
    System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    Dim MyLog As New EventLog() ‘ create a new event log

    ‘ Check if the the Event Log Exists

    If Not MyLog.SourceExists(“NewService1”) Then
    MyLog.CreateEventSource(“NewService1”, “NewService1 Log”) ‘ Create Log

    End If

    MyLog.Source = “NewService1”

    MyLog.WriteEntry(“NewService1 Log”, “It is running”, EventLogEntryType.Information)

    ‘disable the timer so you dont fill up the log

    Timer1.Enabled = False

    End Sub

  6. when i look into my newservice1 log file :

    [code]
    Installing assembly ‘c:\newservice1\newservice1\bin\release\newservice1.exe’.
    Affected parameters are:
    logtoconsole =
    assemblypath = c:\newservice1\newservice1\bin\release\newservice1.exe
    logfile = c:\newservice1\newservice1\bin\release\newservice1.InstallLog
    Installing service NewService1…
    Service NewService1 has been successfully installed.
    Creating EventLog source NewService1 in log Application…
    Rolling back assembly ‘c:\newservice1\newservice1\bin\release\newservice1.exe’.
    Affected parameters are:
    logtoconsole =
    assemblypath = c:\newservice1\newservice1\bin\release\newservice1.exe
    logfile = c:\newservice1\newservice1\bin\release\newservice1.InstallLog
    Restoring event log to previous state for source NewService1.
    Service NewService1 is being removed from the system…
    Service NewService1 was successfully removed from the system.
    [\code]
    any commend?

  7. Impressive; I have got the code example working, but have 1 or 2 minor silly questions:

    1. The main obstacle for not being able to figure (parts of this) out myself, is the fact that ‘ServiceInstaller1’ and ‘ServiceProcessInstaller1’ are not listed in the Toolbox. Why is that? What other ‘gems’ are there, that I can’t see in the Toolbox I wonder.

    2. A bit silly, but…where do I add my custom code I want the service to run?

Leave a Reply

Your email address will not be published. Required fields are marked *