1

I have created a tool which offers similar functionality as the PSExec tool from Microsoft, but does this from Java (Without the "Exec" Command) https://dev.c-ware.de/confluence/display/PUBLIC/JRWShell+-+A+Java+library+for+remote+controlling+a+windows+shell

My problem is that normally my lib automatically stops and uninstalls the service it created, but in case of communication failure this does not happen automatically. I would like the dynamically created Service to start and listen for connections and to terminate after the connection is closed. Ideally I would like the Windows Service to terminate itself and uninstall itself automatically, but I think the self-uninstalling might not be possible without using a second Watchdog service that automatically cleanes up stopped services.

So my question is: Is it possible for a service to uninstall itself? If it is, how is this done and if it is not possible how can I make a service stop itself?

2
  • 1
    I've never tried, but there's no obvious reason why the DeleteService function wouldn't work when called from the service itself. It is trivial for a service to stop itself, it just does exactly the same thing as it would do if it received a stop request, i.e., calls SetServiceStatus with dwCurrentState set to SERVICE_STOPPED. Commented Jul 30, 2012 at 20:35
  • I'll certainly give that a try ... thanks for the pointes ... I'm a Java guy ... no .Net guy so I'm not used to the Apis ;-) Commented Aug 1, 2012 at 9:05

2 Answers 2

2

It turned out that it was possible to uninstall the service from within the service. The Service however semms to stay in the service list as long as the service is still executing and removed after it terminates.

0

I'm using next code, and it's work on my service. But I also add "net stop serviceName" process to uninstall custom actions in my setup project (perhaps it matters).

    private static string getMsiexecParameters()
    {
        return string.Format("/x {0} /quiet /qn", GetProductCode(CProductName));
    }

    public static string GetProductCode(string productName)
    {
        string query = string.Format("select * from Win32_Product where Name='{0}'", productName);
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        {
            foreach (ManagementObject product in searcher.Get())
                return product["IdentifyingNumber"].ToString();
        }
        return null;
    }

    public static void Uninstall()
    {
        ProcessStartInfo psi = new ProcessStartInfo("msiexec.exe", getMsiexecParameters());
        Process.Start(psi);
    }

Sorry for extra code, it's from my work project

Not the answer you're looking for? Browse other questions tagged or ask your own question.