I launch msiexec in another process and wait for exit:
var p = new Process
{
StartInfo =
{
FileName = "msiexec",
Arguments = string.Format("/i \"{0}\" /qb", #"c:\install\setup.msi"),
Verb = "runas"
}
};
p.Start();
p.WaitForExit();
int exitCode = p.ExitCode;
If the setup.msi has not been previously installed it is installing to silent mode and returns 0. It normal.
But if setup.msi already installed (launch this code second time), installation not starting and return code 0 - success result! But in fact, the files have not been established, because product is already installed. How I can determine this situation?
You received an exit code of 0 because the product is already installed and you are not attempting to install a new version. In otherwords, your MSI does not have a new Product Code and version number, therefore the MSIExec installer considers it a reconfiguration, and exits. I tested this out by turning on the /log switch and reading the output after installing one of my MSI files twice.
MSI (c) (98:EC) [15:19:27:912]: Product: Product Name -- Configuration
completed successfully. MSI (c) (98:EC) [15:19:27:912]: Windows
Installer reconfigured the product. Product Name: Product Name.
Product Version: 4.8.22. Product Language: 1033. Manufacturer: Manufacturer. Reconfiguration success or error status: 0.
If you were trying to install a new version of your product and your MSI was not configured to remove previous versions, you would receive an error code of 1638. See list of error codes here: MSDN
If you want to check if the product is already installed with the existing MSI information (not an upgrade) you would need to check the registry at: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\YourProductCode
If it turns out it is installed (according to the system/registry -- maybe the files were deleted but it still is considered to be installed) you can try uninstalling it using the /x or /uninstall switch and then reinstalling. You could also use the /fa switch to do a repair and reinstall all files.
msiexec.exe /x ProductCode will uninstall it. Then you can run the install again after that.
msiexec.exe /fa ProductCode will do a repair of all files. The /f switch has a lot of different options for how it reinstalls files, so you'd do well to read the link to the msiexec switches article I posted above.
Some other notes about msiexec:
/qb displays a basic user interface. You probably want /qn.
When I was setting up my live-update software I ran into a bunch of problems, I had to make sure I called msiexec from system32 by using
p.StartInfo.FileName == Path.Combine(System.Environment.ExpandEnvironmentVariables("%windir%\\system32"), "MSIExec.exe");
First, the other remarks concerning 1638 return code are a bit misleading.
When you install the exact MSI file a second time, you get a return code 0 like you already (and correctly) observed. That's "correct" behaviour or in other words: That's how MSI is designed.
Moreover there are no changes made to your existing setup in this case. If you delete all files before the second install, you end up with nothing although MSI is returning zero.
So return code alone is not helping you for this scenario.
In short terms, you have the following possibilities:
Easy but seems unnormal: Just uninstall (maybe silently) the product before you install with:
msiexec /x {yourproductcode} /qn
(You will not get an error even if the product has been NOT installed before because of the silent parameter "/qn"
Advised if enough for you: Just use the repair mode if you want to install a second time:
Example:
msiexec /i ... REINSTALL=ALL REINSTALLMODE=vemus
Optimal: Use a launcher (boot-strapper or other names are the same thing) to test IF the product is already installed, etc. With this you can automate the previous options (pre-uninstall or add of repair parameters). This can be done also with scripts but in each case this is programming, so not the easiest way.
Now we come to the mentioned 1638 return code in some other answers:
IF (and only if) your build system (like InstallShield does by default) changes the socalled MSI PackageCode) in every build AND you are trying to update this slightly different built (MSI) to a previous installed one, you get the 1638 return code.
These things are often misunderstood.
Changing the PackageCode for every build is a very recommended practice.
On the other hand it will complicate things for you not only a bit, if you will release such MSIs to your customers. The name of this update type is "small update or minor upgrade" (their difference is not important here, because it has the same limitations for you.
If you really want to solve your problem with a return code, you can use this. But as said, the 1638 you will not get for the second install of the exactly SAME MSI!
To continue the recommendation about updates, there are more ways: the easiest to handle way (for beginners) are Major Upgrades. This is what is called "new version" in another answer which was not wrong, but not so exact.
For Major Upgrades you have to change MSI PackageCode and MSI ProductCode at minimum, recommended is changing the ProductVersion also.
(Another way would be to use MSI patches as delta updates, but this is not easy either).
MSI COM API: If you can use the MSI COM API you can use the ProductState property. In other words you can check for an installed product with two lines of code if you have the actual product code (How can I find the product GUID of an installed MSI setup?):
Dim installer : Set installer = CreateObject("WindowsInstaller.Installer")
MsgBox installer.ProductState("{00000000-0000-0000-0000-000000000001}") ' <= PRODUCT CODE
Results: The normal states are 5 for installed or -1 for not installed:
INSTALLSTATE_UNKNOWN -1 The product is neither advertised or installed.
INSTALLSTATE_ADVERTISED 1 The product is advertised but not installed.
INSTALLSTATE_ABSENT 2 The product is installed for a different user.
INSTALLSTATE_DEFAULT 5 The product is installed for the current user.
Interactive VBScript: Here is a larger version of the VBScript with interactive input of product GUID in an InputBox - for use with any product GUID in an ad-hoc fashion: CheckProductState-Interactive.vbs
Links:
Check for installed VCRedist
How can I find the product GUID of an installed MSI setup?
WiX Installer wrong about newer version already installed (find product details based on product code)
Related
I have a stable production version of software which always has version number "1.X.0". We don't have any testing environment so I created a development version with version number "1.X.Y" for testing purposes. I test this version on production computer before releasing new production version (1.X+1.0)
This works fine, there are safety measures in place so that they cannot get mixed up. I also created automatic update for both versions using
startInfo.Arguments = string.Format("/passive /norestart /i \"{0}\" TARGETDIR=\"{1}\"", msiPath, installDirectory);
installDirectory is different for production and development version.
The problem is this:
The upgrade code is the same for both versions. I don't want to switch between upgrade codes.
I also set RemovePreviousVersion to false, because I don't want to have version 1.X.0 removed, when installing version 1.X.Y.
Is it possible to have versions 1.X.0 and 1.X.Y on the same computer at the same time and when version 1.X.0 gets upgraded to version 1.X+1.0, only 1.X.0 is uninstalled? The same should apply for 1.X.Y version.
Or is there a better way to handle this?
Thank you
I would strongly recommend using a virtual machine to test installation and upgrades of software. If you have pro or enterprise versions of windows you already have access to HyperV. This has several advantages:
You have a known environment, either a clean windows install, or in combination with the software of your choice.
You can easily revert the VM to a previous known state.
You can keep multiple VMs or snapshots of different versions.
You can test different operating systems in a easy way.
You can limit memory and processor capacity without affecting your main workstation.
You can install network limiting tools without affecting your main workstation.
You can, and should, still run and test development builds of the software on your own workstation since this simplify debugging. While VMs are more appropriate when doing "release" tests.
I found the solution for having 2 or more different versions of the same software installed at the same time and updating them automatically.
First I find the version I am currently using with
var version = Assembly.GetExecutingAssembly().GetName().Version;
I then compare that to the version of .msi file which is in a update folder on server (there are production and development folders). If the .msi version is higher than installed version, the upgrade begins.
First I install the new software in the same folder using
startInfo.Arguments = string.Format("/passive /norestart /i \"{0}\" TARGETDIR=\"{1}\"", path, installDirectory);
var process = System.Diagnostics.Process.Start(startInfo);
process.WaitForExit();
Then I uninstall only the version I opened.
I find the product code of that specific version using below code (I found the code in Windows GUID or Application List answer and modified it a bit)
private string GetProductCode(string programName, string version)
{
StringBuilder sbProductCode = new StringBuilder(39);
int iIdx = 0;
while (
0 == MsiEnumProducts(iIdx++, sbProductCode))
{
Int32 productNameLen = 512;
StringBuilder sbProductName = new StringBuilder(productNameLen);
MsiGetProductInfo(sbProductCode.ToString(),
"ProductName", sbProductName, ref productNameLen);
if (sbProductName.ToString().Contains(programName))
{
Int32 installDirLen = 1024;
StringBuilder sbVersionString = new StringBuilder(installDirLen);
MsiGetProductInfo(sbProductCode.ToString(),
"VersionString", sbVersionString, ref installDirLen);
if (version.Contains(sbVersionString.ToString()))
{
return sbProductCode.ToString();
}
}
}
return null;
}
and run
startInfo.Arguments = string.Format("/passive /norestart /x \"{0}\"", productCode);
System.Diagnostics.Process.Start(startInfo);
At the end there are still 2 installed version of the softare and only one was updated.
I have an installer written in WIX, that has a WPF Bootstrapper. Recently we added a silent installation mode, and we need to use return codes in order to specify what kind of error occurred during silent installation, for instance: invalid username or password, incorrect server address, unsupported Windows version, etc.
We use the Engine.Quit() method from Bootstrapper class to exit the installer with an exit code. This exit code can be seen in the installer log:
[5490:4F84][2018-09-14T14:31:03]i007: Exit code: 0x101, restarting: No
However, when I check the %errorlevel% environment variable, it remains unchanged. Using Environment.Exit() did not help either.
I suspected, that MSI might be responsible for such behavior by overwriting what WIX tried to set, but even forcing ActionResult.Failure in one of the actions of installer does not help. The MSI exit code is in the MSI log, but %errorlevel% remains unchanged:
MSI (c) (AC:9C) [14:30:59:133]: MainEngineThread is returning 1603
=== Verbose logging stopped: 2018-09-14 14:30:59 ===
Is it possibe to make WIX set the %errorlevel% to a custom value, and if yes, how can it be done?
The %ERROPRLEVEL% value is a feature that you get in batch file environments (the Windows cmd BAT shell, and also PowerShell, I believe) so you're not going to see that value outside of a scripting batch environment. It's not clear from your post if your silent install is a batch script or not.
Having said that, an MSI install process returns standard Windows error results that are documented here:
https://learn.microsoft.com/en-us/windows/desktop/Msi/error-codes
so they can't be customized. The particular errors you mentioned (such as invalid user name or invalid server address) appear to be errors from your custom action code in the MSI. People generally deal with error diagnostics in custom actions by using the logging features of Windows Installer to add your messages to the standard log file. This uses MsiProcessMessage() or equivalent as here:
https://social.msdn.microsoft.com/Forums/windows/en-US/5698aaee-11e5-4a8c-b307-f96b9eb1884f/writing-custom-messages-to-log-file-of-msi-using-msiprocessmessage?forum=winformssetup
https://learn.microsoft.com/en-us/windows/desktop/msi/sending-messages-to-windows-installer-using-msiprocessmessage
So you're not going to get errors specific to your custom actions unless you arrange to record the details in the log, as above, or put them somewhere that your silent install can see them (registry?).
Initially I created an application that I completely rewrite in a second version. It is a complete different Visual studio solution.
Now I would like that its Setup installer uninstall the previous version but because it was not created using the same solution, the automatic uninstallation of previous version does not work.
Is there any way to force the installer to uninstall certain application based on product name or product code?
I found a WMIC command that works when run from command line
wmic product where name="XXXX" call uninstall /nointeractive
So I created a VBS script which execute a bat file containing the WMIC code and I added it to the Setup project
dim shell
set shell=createobject("wscript.shell")
shell.run "uninstallAll.bat",0,true
set shell=nothing
but when I run the result MSI, it fires an error 1001, meaning that a service already exists. , in other words the uninstallation didn't work.
The old program is still present and they create a service with the same name. :/
any suggestion?
There are 2 options:
You can increase the version of MSI project so it will treat as upgrade and it will not throw any error while installing.
another way out is the write some in the installer project as follows:
protected override void OnBeforeInstall(IDictionary savedState)
{
//Write uninstall powershell script
//installutil /u <yourproject>.exe
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("");
PowerShellInstance.AddParameter("");
}
PowerShellInstance.Invoke();
}
Note: This InstallUtil is available with the .NET Framework, and its path is %WINDIR%\Microsoft.NET\Framework[64]\<framework_version>.
For example, for the 32-bit version of the .NET Framework 4 or 4.5.*, if your Windows installation directory is C:\Windows, the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.
For the 64-bit version of the .NET Framework 4 or 4.5.*, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe
I decided to go for the option of introducing c# code in the project installer. Firstly I added the reference for System.Management.Automation via nuget
https://www.nuget.org/packages/System.Management.Automation
After this, I just created a string variable containing the PS code I need to uninstall several programs with a similar name.
string unInstallKiosk = #"$app = get-WMIObject win32_Product -Filter ""name like 'KIOSK'""
foreach ($program in $app){
$app2 = Get-WmiObject -Class Win32_Product | Where -Object { $_.IdentifyingNumber -match ""$($program.identifyingNumber)""
}
$app2.Uninstall()}";
and passed this variable to the method PowerShellInstance.AddScript()
PowerShellInstance.AddScript(unInstallKiosk);
The installation ends but the uninstallation simply dont happens.
anyone has an idea how to solve this?
I'm trying to install 3 msi drivers one after another.
There are all in the same folder and I'm using the same attrubutes for all of them/
I already did it before and it worked fine, but now I don't know what possibly can go wrong.
string attributesToDownload = "REINSTALL=ALL /qb- /norestart";
Process proc = Process.Start("msiexec.exe", string.Format("/i \"{0}\" {1}", driverPath, attributesToDownload));
proc.WaitForExit();
return proc.ExitCode;
The curious thing is:
For the first driver my ExitCode is 1603.
For the second sriver the ExitCode is 0 but the driver is not really installed.
For the third one it all works fine.
When I tried to install the first and the second drivers directly from cmd with the same attributes it installed succesfully.
"msiexec /i driverPath REINSTALL=ALL /qb- /norestart"
Waiting for some ideas.....
Got it, I think.... without the REINSTALL=ALL, the only way to do it (that I found) is uninstalling the current version and than installing the new one
If you're doing REINSTALL=ALL to get this to work, then you are updating an existing product installed on the system. That's ok, maybe you want to patch the existing product, but you don't seem to be aware that you cannot install the same product twice - the newer one needs to a patch or and update like your REINSTALL=ALL or a major upgrade. If you want to uninstall the older one then you need a major upgrade!!! There is no need to uninstall the older version because a major upgrade does that automatically.
I am trying to uninstall a program from my visual studio project but that seems to requiere me to run vs as an admin....so i tried doing this from the cmd to debug it .
I have managed to uninstall a msi setup project installation with this command from cmd :
msiexec /x {3A40307D-6DF2-4412-842F-B1D848043367} /quiet , but that only works when i start cmd as an admin, without admin rights it wont uninstall. What am i doing wrong and is there another approach to get the result I want?
I want to be able to silent uninstall an application without having to ask the user to login as an admin.
Edit:
This is the result from the log :
Error 1001. Error 1001. Unable to delete file C:\ProgramData\XXX.InstallState.
DEBUG: Error 2769: Custom Action _F6174138_B428_4AB6_9FEF_C4DD7A69BDC0.uninstall did not close 1 MSIHANDLEs.
The installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 2769. The arguments are: _F6174138_B428_4AB6_9FEF_C4DD7A69BDC0.uninstall, 1,
CustomAction _F6174138_B428_4AB6_9FEF_C4DD7A69BDC0.uninstall returned actual error code 1603 (note this may not be 100% accurate if translation happened inside sandbox)
Action ended 17:54:40: InstallExecute. Return value 3.
Action ended 17:54:40: INSTALL. Return value 3.
MSI (s) (F0:3C) [17:54:40:355]: Product: XXX -- Removal failed.
That error from the log file indicates that a custom action is crashing. You'll want to investigate the root cause of that issue. My guess is that the custom action requires elevation (admin privileges) to work correctly but is not marked deferred (i.e. runs during the part when the MSI is elevated).
If you launch the uninstall of the MSI from Add/Remove Programs (Programs and Features) then you should not be prompted for elevated credentials. Thus the root issue probably is this custom action.
It appears you have several options here. All of them require creating an msi that doesn't require Admin privileges from the start. If the msi requires them from the start (eg, you have no control over the creation of the msi), there is no way around it. It all depends on what files are being edited as to whether or not admin rights are really required. Check out this answer: How can I create a windows installer MSI that does not require admin access