VS installer make the application automacatically start when OS boots - installation

All .
I have an Window application and wanna release it using VS2010 package installer.
One of the features of the package requires that the application automatically start up whenever OS boots. and the supported OS varies from Windows XP(x86/64) to windows 7 . Could you help to enlighten me to make a solution general to the mentioned OS.
Thanks a lot .

Probably your best bet is to package a .bat script with your project and create a Custom Action to call that file. Call the sc command in it to create your service:
sc create "My Service Name" binPath= "C:\Program Files\My Folder\bin.exe" type= "own" start= "auto" DisplayName="My Service Name"
See this article on technet

Related

Windows service using nssm is working but not working with windows SC

I was supposed to convert a GoLang compiled file .exe as a service in windows but as the service was executed with 9 parameters from outside so I decided to use SC to make the .exe as a service and my syntax was ,
sc create myservice binPath= "\"PATH\file.exe\" -param1=value -param2=value -param3=value...-param9=value" displayname= "MyServer" start= auto
the service created successfully but when I try to start it, it fails with
"service did not respond in a timely fashion" ..
But When I created the same service with nssm syntax ,
nssm install myservice "PATH\file.exe" -param1=value -param2=value -param3=value...-param9=value
It was working and I was getting response from my service , I don't know whether the problem is with the syntax in SC or my service...
I even tried many possible ways like removing \" and giving parameters directly like binPath= "PATH/file.exe -param1=value -param2=value...param9=value"
but it didn't work and I also tried to pass the parameters inside quotes , It didn't work either :( Any help would be appreciated.
While SC will happily install any executable as a windows service, it should only be used to install executables that are already Windows Services. If you use SC to install a regular exe, your service will fail with Error 1053 when you try to start it. Your Go exe, which does not implement the Windows Service interface, falls victim to this situation.

How to install a service on windows 7 without Visual Studio?

Install a windows service: there is a util with Visual Studio: InstallUtil.exe.
If I have a service MyService.exe and want to install it on a windows 7 without Visual Studio on it, how can I install it?
One way is to use sc.exe, which is part of OS. Example:
sc create **ServiceName** start= auto binPath= "c:\mydir\myService.exe"
where binPath is exe file containing your service.
if you don't want start type = automatic then you can use net start and net stop command on command line of windows to start and stop service respectively.
You can try NSSM (Non-Sucking Service Manager) for the times the "sc" doesn't work quite as expected. http://nssm.cc
I had an service I built in Go not too long ago that I could not get to run under "sc", nssm worked perfectly.
you can find installUtil.exe at "C:\Windows\Microsoft.NET\Framework\v2.0.50727", launch from CMD prompt, and add the path to your service
Couldn't get NSSM to work on my W7. It always put its own path as the path to the program I put in the Path box. Had to go manual with the sc example above. That worked perfectly. To get a list of sc parameters, just execute sc. The two * preceeding the ServiceName will cause the ServiceName to appear at the top of the C:\Windows\System32\compmgmt.msc list. To remove a service, use sc delete ServiceName. You will have focus the list and use the Refresh menu item to see the change in compmgmt.msc.

Uninstall Windows Service from Deployment package

I know that you can install your Windows Service via the VS deployment system which I've done. But how do you deploy updates after that? Each time I deploy a new version, it says that the service already exists and exits.
I tried to add a little DOS CMD file with the following:
net stop [ServiceName]
sc delete [ServiceName]
It would work fine if I could just get to run it, but the custom scripts options in the Deployment system doesn't allow that file type.
How can I either a). Update my package on the clients pc or b). run that uninstall utility from within my installer so I can run the update?
Any help would be greatly appreciated.
Regards,
Storm
If the service is already installed, all you need to do is NET STOP it, replace the exe, then NET START the service.

How to uninstall a Windows Service when there is no executable for it left on the system?

How do I uninstall a Windows Service when there is no executable for it left on the system? I can not run installutil -u since there is not executable left on the system. I can still see an entry for the service in the Services console.
The reason for this state is probably because of a problem in the msi package that does not remove the service correctly, but how do I fix it once the service is in this state?
You should be able to uninstall it using sc.exe (I think it is included in the Windows Resource Kit) by running the following in an "administrator" command prompt:
sc.exe delete <service name>
where <service name> is the name of the service itself as you see it in the service management console, not of the exe.
You can find sc.exe in the System folder and it needs Administrative privileges to run. More information in this Microsoft KB article.
Alternatively, you can directly call the DeleteService() api. That way is a little more complex, since you need to get a handle to the service control manager via OpenSCManager() and so on, but on the other hand it gives you more control over what is happening.
Remove Windows Service via Registry
Its very easy to remove a service from registry if you know the right path. Here is how I did that:
Run Regedit or Regedt32
Go to the registry entry "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services"
Look for the service that you want delete and delete it. You can look at the keys to know what files the service was using and delete them as well (if necessary).
Delete Windows Service via Command Window
Alternatively, you can also use command prompt and delete a service using following command:
sc delete
You can also create service by using following command
sc create "MorganTechService" binpath= "C:\Program Files\MorganTechSPace\myservice.exe"
Note: You may have to reboot the system to get the list updated in service manager.
Here is the powershell script to delete a service foo
$foo= Get-WmiObject -Class Win32_Service -Filter "Name='foo'"
$foo.delete()
found here
I just tried on windows XP, it worked
local computer:
sc \\. delete [service-name]
Deleting services in Windows Server 2003
We can use sc.exe in the Windows Server 2003 to control services, create services and delete services. Since some people thought they must directly modify the registry to delete a service, I would like to share how to use sc.exe to delete a service without directly modifying the registry so that decreased the possibility for system failures.
To delete a service:
Click “start“ - “run“, and then enter “cmd“ to open Microsoft Command Console.
Enter command:
sc servername delete servicename
For instance, sc \\dc delete myservice
(Note: In this example, dc is my Domain Controller Server name, which is not the local machine, myservice is the name of the service I want to delete on the DC server.)
Below is the official help of all sc functions:
DESCRIPTION:
SC is a command line program used for communicating with the
NT Service Controller and services.
USAGE:
sc
My favourite way of doing this is to use Sysinternals Autoruns application. Just select the service and press delete.
I'd use PowerShell for this
Remove-Service -Name "TestService"
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-service
Create a copy of executables of same service and paste it on the same path of the existing service and then uninstall.

Run batch file as a Windows service

In order to run one application, a batch file has to be kicked off (which does things like start Jetty, display live logs, etc). The application will work only if this batch file is running. I am hence forced to have this batch file running and not logout from the Windows server.
Can this batch file be run as a service? I am experimenting with one of the suggestions from a similar question.
NSSM is totally free and hyper-easy, running command prompt / terminal as administrator:
nssm install "YourCoolServiceNameLabel"
then a dialog will appear so you can choose where is the file you want to run.
to uninstall
nssm remove "YourCoolServiceNameLabel"
There's a built in windows cmd to do this: sc create. Not as fancy as nssm, but you don't have to download an additional piece of software.
sc create "ServiceName" start= demand displayname= "DisplayName" binpath= [path to .bat file]
Note
start=demand means you must start the service yourself. Options include: boot, system, auto, demand, disabled, delayed-auto
whitespace is required after =
I did encounter an error on service start that the service did not respond in a timely manner, but it was clear the service had run the .bat successfully. Haven't dug into this yet but this thread experienced the same thing and solved it using nssm to install the service.
No need for extra software. Use the task scheduler -> create task -> hidden. The checkbox for hidden is in the bottom left corner. Set the task to trigger on login (or whatever condition you like) and choose the task in the actions tab. Running it hidden ensures that the task runs silently in the background like a service.
Note that you must also set the program to run "whether the user is logged in or not" or the program will still run in the foreground.
On Windows 2019 Server, you can run a Minecraft java server with these commands:
sc create minecraft-server DisplayName= "minecraft-server" binpath= "cmd.exe /C C:\Users\Administrator\Desktop\rungui1151.lnk" type= own start= auto
The .lnk file is a standard windows shortcut to a batch file.
--- .bat file begins ---
java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar
--- .bat file ends ---
All this because:
service does not know how to start in a folder,
cmd.exe does not know how to start in a folder
Starting the service will produce "timely manner" error, but the log file reveals the server is running.
If you need to shut down the server, just go into task manager and find the server java in background processes and end it, or terminate the server from in the game using the /stop command, or for other programs/servers, use the methods relevant to the server.
As Doug Currie says use RunAsService.
From my past experience you must remember that the Service you generate will
have a completely different set of environment variables
have to be carefully inspected for rights/permissions issues
might cause havoc if it opens dialogs asking for any kind of input
not sure if the last one still applies ... it was one big night mare in a project I worked on some time ago.
While it is not free (but $39), FireDaemon has worked so well for me I have to recommend it. It will run your batch file but has loads of additional and very useful functionality such as scheduling, service up monitoring, GUI or XML based install of services, dependencies, environmental variables and log management.
I started out using FireDaemon to launch JBoss application servers (run.bat) but shortly after realized that the richness of the FireDaemon configuration abilities allowed me to ditch the batch file and recreate the intent of its commands in the FireDaemon service definition.
There's also a SUPER FireDaemon called Trinity which you might want to look at if you have a large number of Windows servers on which to manage this service (or technically, any service).
Since NSSM is no longer maintained, you can consider using WinSW. It has binaries that would work with or without .Net.
Basically you create an XML file and then install it. Here is a sample of a minimal XML:
<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>myapp</id>
<!-- Path to the executable, which should be started -->
<!-- CAUTION: Don't put arguments here. Use <arguments> instead. -->
<executable>%BASE%\myExecutable.exe</executable>
</service>
And then you can install and start it:
winsw install myapp.xml
winsw start myapp.xml
Install NSSM and run the .bat file as a windows service.
Works as expected
My easest way is using opensource svcbatch (https://github.com/mturk/svcbatch/) as wrapper of CMD(BAT) in sc :
sc create myservice binPath= ""%cd%\svcbatch.exe" myservice.bat"

Resources