简体   繁体   中英

Application process after msi installer finished is started as SYSTEM user name which sometimes can not create com object

I have a visual studio installer project which installs a C# app, I have a custom action and code to run the process after install is complete.

Let's say the logged in user on windows machine is "john". Now when john runs the msi installer, I check the process in the taskmanager and it shows that msiexec.exe is the process name for installer and it is running as user "john"

The installer completes and runs the install app's process myapp.exe now when I check this process in taskmanager of windows it shows that myapp.exe is running as SYSTEM (which I do know what account is that and why its not running as john)

Problem When myapp.exe runs as SYSTEM user it can not create com component instance of a component (iTunes in my case) which was already running as user john. If the component was not running then creating isntance of the iTunes is sucessful, otherwise it fails.

Question So is it possible to make sure when installer runs as john, when it finishes it starts process myapp.exe as john and not as SYSTEM user ? Note that I do not asks user for password during installer.

Code that I run when installer completes

    // Event handler for 'Committed' event.
    private void MyInstaller_Committed(object sender, InstallEventArgs e)
    {
        try
        {                
            Directory.SetCurrentDirectory(Path.GetDirectoryName
            (Assembly.GetExecutingAssembly().Location));
            Process.Start(Path.GetDirectoryName(
              Assembly.GetExecutingAssembly().Location) + "\\MyApp.exe");
        }
        catch
        {
            // Do nothing... 
        }
    }

This most likely happens because you are running your custom action as deferred. ie it is running by default under the SYSTEM user account.

A solution is to make sure you launch it immediate, for example you can launch it using a published event on the "Finish" button from the last dialog of the installation. I don't know exactly how to add a published event in VS setup projects, or if it is possible, but you can easily add one in packages built with specialized setup authoring tools .

In the Form_Load event write like this:

string lUserName=Environment.GetEnvironmentVariable("USERNAME");

Then instead of getting system user account you will get windows login user name at the time of installing MSI.

Personally I did manage to resolve this by running explorer.exe parameterized with the path to my app's executable:

Process.Start(
    new ProcessStartInfo
    {
        FileName = "explorer.exe",
        Arguments = Context.Parameters["target"],
        UseShellExecute = true
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM