oleg-shilo / wixsharp

Framework for building a complete MSI or WiX source code by using script files written with C# syntax.
MIT License
1.12k stars 175 forks source link

Restart Office apps after installation #1598

Closed Aaswin1996 closed 3 months ago

Aaswin1996 commented 4 months ago

My installer adds registry keys for VSTO addins to my office application .I want to restart office apps after installation so that the vsto addins are loaded after installation .Is there a way to do this in WixSharp .I want to use the Restart Manager for this purpose .

Torchok19081986 commented 4 months ago

morning, remeber that you can always use AfterInstall Event from WixSharp in your msi packafge. Difficulty here is , to use external Process to do something. windows installer is bad to use something outside of installer, unfortunatelly.
If you want to use Restart manage is has to be part of your installer. There is alternative way , to use bootstrapper installer, where you can do almost anything. Your BA installer is Ui with logic of windows installer and here you can use multuiple events from BA. Download latest version of wixsharp and go to bootstrapper folder. @oleg-shilo did very good job to show almost all cases for BA Installer. With this you can control flow of installer completly. Modern installer are almost all , ~95% all BA Installer : free or paid. After install, all those ask for reboot or restart. Here you can use shotdown.exe with parameters to reboot or user logout immerdiatly and you dont need to use restart manager. Best regards, Torchok.

Aaswin1996 commented 4 months ago

@Torchok19081986 Cannot switch to a BA installer now .Need to use the restart manager as its the only API which I found which kills the application and restarts from where it was killed ...something like a restore .

Torchok19081986 commented 4 months ago

then only one way direct to restart manager with MSI installer and msi package. Try afterinstall event from Wixsharp. If this doesnt work here you can create Custom Action, but here is huge problem. custom action rely on current msi install session.


[CustomAction]
public static ActionResult StartRestartManager(Session session)
{
  //here comes the code, BUT somehow you have to  execute exe from here direct. something like session["INSTALLDIR"] = Process.Start("Restartmanager") will be not work, you have to deal with process startinfo and path to restart manager and here msi doesnt want it execute it. 

}
Aaswin1996 commented 4 months ago

then only one way direct to restart manager with MSI installer and msi package. Try afterinstall event from Wixsharp. If this doesnt work here you can create Custom Action, but here is huge problem. custom action rely on current msi install session.

[CustomAction]
public static ActionResult StartRestartManager(Session session)
{
  //here comes the code, BUT somehow you have to  execute exe from here direct. something like session["INSTALLDIR"] = Process.Start("Restartmanager") will be not work, you have to deal with process startinfo and path to restart manager and here msi doesnt want it execute it. 

}

@Torchok19081986 Can you explain a bit more ?i Did not get the last part

Aaswin1996 commented 4 months ago

then only one way direct to restart manager with MSI installer and msi package. Try afterinstall event from Wixsharp. If this doesnt work here you can create Custom Action, but here is huge problem. custom action rely on current msi install session.

[CustomAction]
public static ActionResult StartRestartManager(Session session)
{
  //here comes the code, BUT somehow you have to  execute exe from here direct. something like session["INSTALLDIR"] = Process.Start("Restartmanager") will be not work, you have to deal with process startinfo and path to restart manager and here msi doesnt want it execute it. 

}

@Torchok19081986 Can you explain a bit more ?i Did not get the last part

The issue I am facing is applications are getting shutdown but not restarted

Torchok19081986 commented 4 months ago

that i said, you have to figured out, how to call restartmanager with parameters within CA. Try use class ProcessStartInfo and add verbs or verb to it. If your Restartmanager doesnt have parameter to restart apps after action, i think, you can try use Process Class , here you can kill and restart process by name. All another things you can , maybe, examinate log from wixshar in temp or window errorlog.

oleg-shilo commented 4 months ago

Actually, you can start any application from the AfterInstall event handler. The same way as from any other .NET application.

I am not sure what exactly you are referring to as RestartManager as this term is somewhat overused. But you can definitely use this:

project.AfterInstall += args =>
{
    if (!args.IsUninstalling)
        Process.Start("shutdown", "-r")
};

Or if you want to use the Start extension method then you can call "shutdown".Start("-r");

Torchok19081986 commented 4 months ago

hallo, @oleg-shilo. Was my suggestion, but @Aaswin1996 want to use special restartmanager and dont want use shutdown.

oleg-shilo commented 4 months ago

"shutdown.exe" is just a windows utility

oleg-shilo commented 4 months ago

Then I have no idea what "restartmanager" is. But the way using it will be the same regardless if it is setup event, raw CA, or a BA.

Aaswin1996 commented 4 months ago

@oleg-shilo @Torchok19081986 RestartManager is a windows API which is written in c++ which helps in gracefully shutting down applicatioand and then restarting them from where it was shutdown .

I am using this as a wrapper code in c# to access c++ native functions :

https://gist.github.com/falahati/34b23831733151460de1368c5fba8e93

My use case is after installation I want to restart outlook and excel .Restart means shutdown and resume from where it was shutdown

oleg-shilo commented 4 months ago

Great, I looked at the code you shared. It is a pure interop (WIN API) so you have no dependency on anything.

then add that cs file to your project and then call the restart routine from the AfterInstall event handler:

project.AfterInstall += args =>
{
    if (!args.IsUninstalling)
        using (var rm = new RestartManagerSession())
        {
            rm.RegisterProcess(Process.GetProcessesByName("explorer"));
            rm.Shutdown(RestartManagerSession.ShutdownType.Normal);
            rm.Restart();
        }
}
oleg-shilo commented 4 months ago

But of course replace "explorer" with whatever you need it to be

Aaswin1996 commented 4 months ago

your project and then call the restart routine from the AfterInstall event handler:

@oleg-shilo It does not work Shutdown happens but Restart Fails

oleg-shilo commented 4 months ago

Then you need to follow it with that "retstartmanager" solution that you use.

I say, try it outside of WixSharp (unless you already did it). Verify that it works and only when it does integrate it into your setup.

Aaswin1996 commented 4 months ago

@oleg-shilo Its working outside only when I merge it with WixSharp it starts failing

oleg-shilo commented 4 months ago

OK, then maybe it the difference is caused by the fact that this API is called from the elevated process (your install session). The real answer will be in that rstrtmgr.dll documentation if it exists.

However, you can just try to unelevate your afterinstall:

project.AfterInstallEventExecution = EventExecution.MsiSessionScopeImmediate;

Small chance, but it might work

Aaswin1996 commented 4 months ago

image image @oleg-shilo Same Error

oleg-shilo commented 4 months ago

Wait, so you have the error information! You know that it is the RestartManager service is failing. The event log is very clear about it. So why are we looking at MSI to solve this problem?

I suggest you now look at the RestartManager service documentation for the solution.

Aaswin1996 commented 4 months ago

@oleg-shilo https://learn.microsoft.com/en-us/windows/win32/api/_rstmgr/

Aaswin1996 commented 4 months ago

@oleg-shilo The issue is that normally without MSI this works fine but when I include it in the WixSharp restarting applications starts failing

oleg-shilo commented 4 months ago

Yes I understand the issue. Though it's not "in the WixSharp restarting applications". It happens when you do it from the MSI session.

Why it is happening can only be understood by digging in the restartmanager documentation. WixSharp documentation will not help you. Neiter WiX nor MSI. This is something specific for restartmanager. You will need to do yourresearch

Torchok19081986 commented 4 months ago

hallo, @Aaswin1996. If you have library of RestartManager , you coult try defered CA from External Assembly. I had same issue wiith 3. party Libs for creating Task for Windows TaskScheduler. External Assembly for execution in CA brought the solution for me. Thanks to @oleg-shilo. there is example from oleg in latest wixsharp folder. Source\src\WixSharp.Samples\Wix# Samples\DTF (ManagedCA)\Different Scenarios\ExternalAssembly. Here locatet sample.