简体   繁体   中英

Install a windows service on windows xp

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM