Prevent setup project from uninstalling busy service - windows-services

How do I prevent my setup project from uninstalling my Windows service while it is performing a lengthy work routine?
Ideally, MSI should report that the "Service is currently busy and cannot be uninstalled."
How to create a condition for the installer to check if the service is busy and fail the installation?

You could maybe use an Installer class for your application. You could override the OnBeforeUninstall method so that it looks to see if the process is running, and then waits for it to stop before proceeding.

A similar solution to the one YWE posted is to create a custom action inside a DLL and run it when the uninstall process starts. In the custom action you could interrogate the service to check it's status and if it's busy fail the install with a relevant message for the user.
Walkthrough: Creating a custom action

Related

Inno Setup tries uninstalling Delphi TService multiple times

I have a Windows Service built with Delphi 2010, and I'm using a Inno Setup installer for deployment.
It's mostly working smoothly - I correctly stop the service before uninstalls and upgrades, and restart it following installs.
[Run]
Filename: {app}\MyService.exe; Parameters: "/INSTALL /SILENT"; ...
[UninstallRun]
Filename: {app}\MyService.exe; Parameters: "/UNINSTALL /SILENT"; ...`
However, I have one problem left: If I run the installer twice in succession, then the next time I uninstall it fails showing a Message Dialog : Service XXX failed to uninstall with error: "System Error. Code:1060. The specified service does no exist."
Logging shows that the "UninstallRun" section is being executed twice when the uninstaller runs. And the second time, it fails and throws the error message (from TServiceApplication::RegisterServices) because the service is already uninstalled. Pretty sure this is because of the way Inno Setup tracks multiple installations in uninstall.dat.
My 'hack' to fix this is to use the TService's ServiceBeforeUninstall handler, and exit(0) if the service isn't installed. This feels like a brute force approach - is there a smarter way of resolving the problem?
Don't use the built-in SCM wrapper functions in TService, use the SCM API, they will give you much more control on service management. I wrote an InnoSetup script years ago, you can find it here - it's a while I don't update it, but it is still a starting point.

Forcing a Windows service to fail

Whenever a specific Windows service fails I want to run a program I've created myself. However, I simply can't find a way to make it fail on purpose, so that I can actually test that everything works correctly.
Note that the service in question is not something I've written myself, so I can't make it fail programmatically from inside the code. I wouldn't, however, mind writing a program that can make a service fail.
Of course I would prefer just having a "Make service fail" button somewhere in services.msc ... ;)
The server I'm doing this on is running Windows Server 2012.
If you don't want to use command line :
As an admin open the Windows Task Manager, in the Services tab find the service you want to test. Right click the service and click on Go to process. The selected process (if any) is the one corresponding to your service. Kill this process to simulate a service failure.
Be aware that killing a process this way can lead to problems.
Define "fail". If you want the process to end, just use pskill or a similar tool that can terminate a process elevated (as an admin).

restart service in case of exception

I'm developing service application that must restart itself. What are the ways of doing that? Is it possible to ask system start application again if it is stopped? I'm using Delphi 2007.
Your service can programably configure recovery options for itself by calling the Win32 API ChangeServiceConfig2() function inside of its AfterInstall event. Set the dwInfoLevel to SERVICE_CONFIG_FAILURE_ACTIONS and set the lpInfo to point at a SERVICE_FAILURE_ACTIONS record describing what you want to happen when the service fails.
If you go into services.msc you can configure this for you service. You don't have to do it in code. See the Recovery tab when you open the properties of you service.

VS Setup Project Windows service questions

My setup project installs a windows service. Right now this action is in the Install category. Would the commit category be a better location for it?
I've added a custom action to remove the service in the Uninstall category, but when I run the uninstall, the user is prompted to shut down the service. As the user should never really know that the service exists, I'd rather this never pops up, but rather stops the service on its own and simply uninstalls this. I can't seem to find a setting for that. How might I accomplish it?
Should I have anything in the rollback section? I added the same custom action for the install/uninstall, figuring it would be smart enough to uninstall the service if the rollback occurs after the service was installed. Is this correct? Would it be moot if I installed on commit instead?

(SC) DeleteService FAILED 1072

Last time I create WAS profile and WASService then I try to config and run many script for learn how to config WAS, Finally it crash so i use wasprofile delete this profile and forgot delete WASService.
Now I found IBM Webphere Application Server service display in services.msc list, so I tried to delete it with WASService.exe -remove command and windows SC command but I got message
C:\Program Files\IBM\WebSphere\AppServer\bin>sc delete "IBMWAS61Service - DEV"
[SC] DeleteService FAILED 1072:
The specified service has been marked for deletion.
make sure the service is stopped, the services control panel is closed, and no open file handles are open by the service.
Also make sure ProcessExplorer is not running.
I had a similar problem and what I did to overcome it was the following:
Stop the service: net stop "ServiceName"
Ensure: the "mmc.exe" process does not exist (The "Services" list window): taskkill /F /IM mmc.exe
Delete the service: sc delete "ServiceName"
C:\server>sc delete "ServiceName"
[SC] DeleteService SUCCESS
Now, if I execute another sc command, what I get is the following:
C:\server>sc delete "ServiceName"
[SC] OpenService FAILED 1060:
The specified service does not exist as an installed service.
But not the 1072 error message
What I've done is go to this location in regedit:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
From here, you will see a folder for every service on your machine. Simply delete the folder for the service you wish, and you're done.
N.B: Stop the service before you try this.
For some buggy reason both Event Viewer and/or Services.msc won't do a proper refresh when you tell them to!
Close them and restart, and the service would have been deleted anyway.
I had the same issue. After I closing and re-opening the Computer Management window the service was removed from the list. I'm running windows 7
In Windows 7, make sure Event Viewer closed before deleting.
I had this error also, make sure the exe the service is pointing to is stopped. Also make sure you don't have any Windows dialog boxes behind your other windows. That is why mine wasn't deleting. There was a windows message behind it saying this service has been deleted or something similar.. just had to click ok, there it went.
I had the same error due to a typo in the service name, i was trying to delete the service display name instead of the service name.
Once I used the right service name it worked fine
Logging-out and logging-in again close all blocking apps thus resolves the problem.
The 3rd party application uninstaller had removed the files for the service and then left the service in this pending deletion state.
After trying to close all applications, identifing PID of service(couldn't) for kill, logging off all other users and logging off and on, rebooting was the only fix that worked for me.
One situation where this can also happen is if there is some other service or application that is holding open a service handle obtained with OpenService. For example, a monitoring service that starts and stops services based on some external event can keep open handles to each of the services it monitors. In this case, uninstalling the service would leave it in the "marked for deletion" state until all handles obtained with OpenService are closed.

Resources