0

I am trying to install a windows service I created in Visual Studio 2012 on Windows XP. How can I do this?

I tried following this tutorial, but had little luck implementing it. http://www.ehow.com/how_6046576_install-service-windows-xp.html

Is there anything wrong with my code?

Public Class MyFirstService
  Dim WithEvents timer1 As New System.Timers.Timer

  Protected Overrides Sub OnStart(ByVal args() As String)
    timer1.Interval = 10000
    timer1.Start()
    WriteLog(Me.ServiceName & " has started ...")
  End Sub

  Protected Overrides Sub OnStop()
    WriteLog(Me.ServiceName & " has stopped ...")
  End Sub

  Private Sub timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer1.Elapsed
    WriteLog(Me.ServiceName & " is running ...")
  End Sub

  Private Sub WriteLog(ByVal strMessage As String)
    Dim strPath As String, file As System.IO.StreamWriter
    strPath = AppDomain.CurrentDomain.BaseDirectory & "\MyService.log"
    file = New System.IO.StreamWriter(strPath, True)
    file.WriteLine("Test")
    file.Close()
  End Sub

End Class

When I click on my .exe I get this error message MyFirstService.exe is not a valid Win32 application. This is my first attempt at a window service app.

Steven Lynn
  • 103
  • 8
  • Well, your code doesn't try to install anything, so I'm not sure what you are asking about. Are you having trouble installing your service or running it? – Brad Aug 12 '14 at 20:39
  • Installing the service. I cannot run it until it is installed correct? What do you mean by my code doesn't install anything? – Steven Lynn Aug 12 '14 at 20:40
  • When I click on my .exe I get this error message MyFirstService.exe is not a valid Win32 application. – Steven Lynn Aug 12 '14 at 20:42
  • If that's the case, then it isn't compiling correctly. And yes, you can run it before it is installed. Most services are just EXEs that accept additional signals. – Brad Aug 12 '14 at 20:56
  • Thanks @Brad. That is good information. – Steven Lynn Aug 12 '14 at 20:58
  • 1
    possible duplicate of [How to compile for Win XP with Visual Studio 2012?](http://stackoverflow.com/questions/13130713/how-to-compile-for-win-xp-with-visual-studio-2012) – Harry Johnston Aug 12 '14 at 23:49
  • You can't run a Windows service by double-clicking the executable. You have to install it with the OS and then run it via the Services console. I have a couple of tutorials on how to create a Windows service and have it install itself. They are done in C#, but it should translate to vb.net. – Matt Davis Aug 13 '14 at 14:38
  • First tutorial: http://stackoverflow.com/questions/593454/easiest-language-for-creating-a-windows-service/593803#593803 – Matt Davis Aug 13 '14 at 14:39
  • Second tutorial: http://stackoverflow.com/questions/1195478/how-to-make-a-net-windows-service-start-right-after-the-installation/1195621#1195621 – Matt Davis Aug 13 '14 at 14:40
  • @HarryJohnston That link solved my problem. It was a problem with compiling for windows xp. I just decided to run it on my windows 7 machine instead. If you post as an answer I will accept it. – Steven Lynn Aug 13 '14 at 14:45

1 Answers1

0

I recently have developed several Windows services, and wanted to be able to easily debug them without having to enter a bunch of stuff into a command prompt every time I built the service.

To do this, I started out by adding the following to my "Build Events" in the project properties of my service project:

Post-Build:

call "$(DevEnvDir)..\Tools\vsvars32.bat"
installutil "$(TargetPath)"
net start "$(SolutionName)"

After successfully building with the above, make sure to add the following to your Pre-Build:

net stop "$(SolutionName)"
call "$(DevEnvDir)..\Tools\vsvars32.bat"
installutil /u "$(TargetPath)"

This will make it so your project is automatically stopped, uninstalled, reinstalled, and started whenever you build it. Also make sure that you run the post-build event On successful build.

Note: This does assume that the name of your service is the same as your solution name. If they are different, you will want to change the $(SolutionName) to your service name.

Forest Kunecke
  • 2,160
  • 15
  • 32