--
StephanLinn - 29 Jun 2009
HOWTO Create and Install a Window Service WITHOUT the .NET framework.
It turns out that if I followed Richard's instructions, I met with a number of insurmountable obstacles - the worst of which is that it will not work for C++. I'm not sure if this is a bug or a feature meant to encourage the use of Visual Basic or C#.
Nevertheless there is a 'back door' that is left over from the NT days. First, it is not necessary to do anything special in your code for it to run as a Windows Service - any compiled .exe will work. The operating system procedures are called "INSTSRV.EXE" and "SRVANY.EXE". There is also "SC.EXE" which is used by "INSTSRV.EXE", but you will need to create/edit your own registry entries.
In addition, there is also a procedure called "MSIEXEC.EXE" that will deploy a ".msi" project file without having to interactively log on a machine. Projects are useful because all dependencies (dll's) are automatically packaged.
Here is the
link
for the Microsoft documentation.
Below is a Visual Basic script that installs a project file "deploy24Aug08.msi" in "C:\temp", and then creates and runs "Safety.exe " as the "SafetyServer" Windows Service.
Thanks to Bobby and Frank for pointing these out to me.
Recently we have adopted a DCS philosophy that uses PVSS Control Language Extensions instead of Windows Services.
'//----------------------------------------------------------
'//
'// Visual Basic Script for installing an application as a
'// Windows Service.
'// Modified InstallDIMDNSService
'// SLL 17 Aug 2008
'//
'// To Use modify:
'// strPath - working directory ( do not use folder names with spaces)
'// strApp the application name
'// strSrv the server name
'// strMsi install file
'//
'//
'//----------------------------------------------------------
Option Explicit
'------------------------------------------------------------
'Parameter definition
'------------------------------------------------------------
Dim arg1,arg2,arg3
'------------------------------------------------------------
'Declarations
'------------------------------------------------------------
Const constHKEY_LOCAL_MACHINE = &H80000002
Dim objRegistry
Dim oShell
Dim strPath
Dim strApp
Dim strSrv
Dim strMsi
Dim argObj
Dim strComputer
strComputer = "."
strPath = "C:\temp\"
strMsi = "deploy24Aug08.msi"
'//40
'------------------------------------------------------------
' create a WScript object and popup for 10 sec
'------------------------------------------------------------
Set oShell = WScript.CreateObject("WScript.Shell")
'//Set ArgObj = WScript.Arguments
'// arg1 = ArgObj(0)
oShell.Popup "Installing "&strApp , 10
'------------------------------------------------------------
' install the msi
'------------------------------------------------------------
oShell.Run( "cmd /c msiexec /qn /i " & strPath & strMsi & " TARGETDIR="& strPath )
'//oShell.Run( "cmd /k msiexec /qn /x " & strPath&strMsi & " TARGETDIR=" &strPath )
'------------------------------------------------------------
' create the services one by one
'//oShell.Run "sc create "& strSrv & " binPath=" & strPath & "srvany.exe start= auto", 1, true
'//oShell.Run strPath & "instsrv.exe " & strSrv & "REMOVE"
'------------------------------------------------------------
strApp = "Safety.exe "
strSrv = "SafetyServer"
oShell.Run strPath & "instsrv.exe " & strSrv & " " & strpath & "srvany.exe"
'------------------------------------------------------------
' fix the registry
'------------------------------------------------------------
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.CreateKey constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & strSrv & "\Parameters"
objRegistry.SetStringValue constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & StrSrv & "\Parameters","Application", strPath&strApp
'------------------------------------------------------------
' start/stop it
'//oShell.Run "sc stop " & strSrv , 1, true
'------------------------------------------------------------
oShell.Run "sc failure " & strSrv &" reset= 20 actions= restart/30000", 1, true
oShell.Run "sc start " & strSrv , 1, true
strApp = "SeaLevel.exe "
strSrv = "SeaLevelServer"
oShell.Run strPath & "instsrv.exe " & strSrv & " " & strpath & "srvany.exe"
'------------------------------------------------------------
' fix the registry
'------------------------------------------------------------
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.CreateKey constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & strSrv & "\Parameters"
objRegistry.SetStringValue constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & StrSrv & "\Parameters","Application", strPath&strApp
'------------------------------------------------------------
' start/stop it
'//oShell.Run "sc stop " & strSrv , 1, true
'------------------------------------------------------------
oShell.Run "sc failure " & strSrv &" reset= 20 actions= restart/30000", 1, true
oShell.Run "sc start " & strSrv , 1, true
strApp = "Laser.exe "
strSrv = "LaserServer"
oShell.Run strPath & "instsrv.exe " & strSrv & " " & strpath & "srvany.exe"
'------------------------------------------------------------
' fix the registry
'------------------------------------------------------------
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.CreateKey constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & strSrv & "\Parameters"
objRegistry.SetStringValue constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & StrSrv & "\Parameters","Application", strPath&strApp
'------------------------------------------------------------
' start/stop it
'//oShell.Run "sc stop " & strSrv , 1, true
'------------------------------------------------------------
oShell.Run "sc failure " & strSrv &" reset= 20 actions= restart/30000", 1, true
oShell.Run "sc start " & strSrv , 1, true
strApp = "Lvtmm.exe "
strSrv = "LvtmmServer"
oShell.Run strPath & "instsrv.exe " & strSrv & " " & strpath & "srvany.exe"
'------------------------------------------------------------
' fix the registry
'------------------------------------------------------------
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegistry.CreateKey constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & strSrv & "\Parameters"
objRegistry.SetStringValue constHKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services\" & StrSrv & "\Parameters","Application", strPath&strApp
'------------------------------------------------------------
' start/stop it
'//oShell.Run "sc stop " & strSrv , 1, true
'------------------------------------------------------------
oShell.Run "sc failure " & strSrv &" reset= 20 actions= restart/30000", 1, true
oShell.Run "sc start " & strSrv , 1, true
WScript.Quit(0)