0

I have a windows service that runs every 10 minutes and checks to make sure that all of the correct parts of our software are installed and up-to-date. Every time that the service executes, it uses the following function to install an executable:

    public static bool InstallControlPanel()
    {
        try
        {
            //this will change
            string sSetupFile = @"C:\Projects\SyncAgentV2\SyncAgentV2.ControlPanel.Installer\SyncAgentV2.ControlPanel.Installer\Express\SingleImage\DiskImages\DISK1\setup.exe";

            if (!System.IO.File.Exists(sSetupFile))
            {
                Debug.WriteLine("Service Installer doesn't exist.");
                return false;
            }

            Process oProcess = new Process();

            oProcess.StartInfo.ErrorDialog = true;
            oProcess.StartInfo.UseShellExecute = true;
            oProcess.StartInfo.Arguments = "/s /v/qn";
            oProcess.StartInfo.FileName = sSetupFile;
            oProcess.Start();
            oProcess.WaitForExit();

            return true;
        }
        catch (Exception oEx)
        {
            Debug.WriteLine(oEx);
            return false;
        }
    }

When I debug this method when it is called from the service, it runs and progresses through all of the steps, returning true, but it does not install the executable. I can call this method from a WPF and it works, so the problem must be the way that the service calls it.

Right now, the service is set to log on as Local System. I suspect that a service cannot operate the way that I want it to or that I need it to log on as a different user. Can someone verify this or point me in a different direction?

EDIT: This method works, but not when I call it from a service.

ijb109
  • 942
  • 1
  • 19
  • 30
  • I would add debugging code that write information to a text file to determine your error in your code. – Security Hound May 23 '13 at 20:02
  • Where would you recommend putting that? I have a logger in the main application, but this method hasn't thrown any errors or exceptions when I watch it. – ijb109 May 23 '13 at 20:27

1 Answers1

2

Do you run your process under win7? Are there any error exit codes from installer in event log? Probably your installer needs to show UI, and suffering from Session 0 isolation.

Since process is started by windows service - it is isolated from currently logged in user. You need to run your installer in currently logged in user's context. There is an WinAPI function for that - CreateProcessAsUser / CreateProcessWithToken. Unfortunately, these functions are not present in .net base class library, and must be called through PInvoke. Here is a great post, describing how to implement wrapper for launching any command in user context.

Alexander
  • 4,153
  • 1
  • 24
  • 37
  • As to the first part, I run win7, and there are no error exit codes. They have all exited with code 0, which seems to be normal. I have gotten it to successfully install without UI from a WPF, but it didn't work either way with the service. I'm still looking into Session 0 Isolation. – ijb109 May 23 '13 at 20:26
  • That's exactly what was going on, thank you so much! I had no idea where to even start with this problem. – ijb109 May 24 '13 at 13:05