Uninstall previous version, regardless of installation type? - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

cbono
Posts: 42
Joined: Thu Aug 14, 2008 10:11 pm

Uninstall previous version, regardless of installation type?

I'm hoping someone can give me some advice or options for this scenario.

Background:
We have an existing product out in the field. That version's installer was made by Visual Studio and is MSI-based. We allowed users to choose the installation type ("Everyone" or "Just Me"), but we defaulted it to "Just Me". Therefore, a majority of installations are on a per-user basis, but we know a few have installed for "Everyone".

We're exploring the use of Advanced Installer for our new version which is still in development. We have changed A LOT of the installed files (some new, some renamed, etc) and we need to do a complete uninstall of the current version of the product before installing this new one. If we don't, we get a horrible mix of old and new files in the same application folder and our product doesn't run correctly. It's a mess.

I've used the Upgrade Code from the previous version's installer in my test AI project.


Problem:
If I install the new version with the same installation type (per-user or per-machine), the old version is properly detected and removed just like it needs to be. But if I don't match the installation type, I get the bad scenario I outlined above.

As I said earlier, the vast majority of field installations will be per-user, but I know for a fact some users have installed per-machine. Therefore locking the AI installer to per-user only isn't going to work.


Ideal Solution:
I'd REALLY love to let AI use the "Per-machine if user is administrator, per-user otherwise" setting, and still guarantee the previous version in uninstalled, regardless of that version's installation type. That may not be realistic, but I wanted to make that known.

Acceptable Solution:
I'd be okay if AI could detect the previous version's installation type and automatically set its own type accordingly. We'd be honoring a previous decision made by the user, and at the same time ensuring the previous product is properly removed.

Do Not Want:
WHAT I DO NOT WANT is to give the users a choice, because inevitably someone will choose wrong and we'll get support calls about botched installations. Plus, this new version has a lot of client and customer anticipation around it and we want to do everything we can to make it a great experience from the very start.


That's it. I sure hope someone can help me out!

Regards,
Chris

P.S. Why is there so much difference between per-user and per-machine installs? It all comes from the same installer; I don't understand why this process has to be as complicated as it's turned out to be.
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Uninstall previous version, regardless of installation type?

Hi,
I'd be okay if AI could detect the previous version's installation type and automatically set its own type accordingly.
Currently Advanced Installer doesn't support this. However, I have added this improvement on our TODO list and it will be available in a future version. Thank you for your suggestion.

Until this improvement is implemented, you can use a custom action to determine the installed state (per-user or per-machine) of the original version. You can find sample code in the MSDN. Note that this code should be written in a DLL designed for a custom action.

After the custom action detects the installed state of the original version, it should set the ALLUSERS property accordingly. Please note that this custom action should be executed in the early stages of the installation.
Why is there so much difference between per-user and per-machine installs? It all comes from the same installer; I don't understand why this process has to be as complicated as it's turned out to be.
Basically, Windows Installer doesn't have access to the per-user locations of another user (the HKEY_CURRENT_USER hive, some folders under "Documents and Settings" etc.). Therefore, a per-machine installation cannot upgrade a per-user installation (it cannot remove the original files).

Also, usually a per-user installation doesn't require Administrator privileges. If it would upgrade a per-machine installation, a per-user installation would always require Administrator privileges, eliminating the advantages of a per-user installation.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cbono
Posts: 42
Joined: Thu Aug 14, 2008 10:11 pm

Re: Uninstall previous version, regardless of installation type?

Hi Cosmin,

Thanks for the suggestion. C++ development is a little over my head, though I have a co-worker who is looking into it now. But I had another idea, and maybe you can tell me if this is possible or not.

I've found that if I add an "Upgrade" item to the Upgrades page for the previous version of our Product, that behind the scenes A.I. is finding the installation AND its Product Code. Here's an excerpt from a debug log:

Code: Select all

MSI (c) (F0:74) [11:19:15:750]: Doing action: FindRelatedProducts
Action 11:19:15: FindRelatedProducts. Searching for related applications
Action start 11:19:15: FindRelatedProducts.
MSI (c) (F0:74) [11:19:15:750]: FindRelatedProducts: current install is per-machine.  Related install for product '{937C5414-F1CB-47DF-B01B-62557D874DB8}' is per-user.  Skipping...
Action ended 11:19:15: FindRelatedProducts. Return value 1.
It knows my product is installed, it knows which install type it is, but most interestingly it knows Product Code for that version.

What I'm thinking is, if I can somehow capture that Product Code and save it into a Property, that I can adapt the Uninstall MSI example from your website and use it to invoke msiexec.exe via a Custom Action, removing the old version before the new version begins its installation process.

I don't know how to capture that Product Code, though. If I could get it, I think I can piece together the rest of the solution. By the way, when you have an Upgrade and you set it to "Detect only, placing results in property" -- what actually gets stored in the property? Just a boolean value indicating whether it found that Upgrade?

Thanks,
Chris
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Uninstall previous version, regardless of installation type?

Hi,
behind the scenes A.I. is finding the installation AND its Product Code.
Please note that this is done automatically by Windows Installer based on the information in the Upgrade table of the MSI. This table is populated by Advanced Installer based on the settings you use in the "Upgrades" page.
if I can somehow capture that Product Code and save it into a Property
When older versions of the package are found on the target machine, their Product Codes are added to the OLDPRODUCTS property. If there is only one older version, this property will contain its Product Code. Note that you can also use a hard-coded Product Code by retrieving it from the original package.
I don't know how to capture that Product Code, though.
The product code can be obtained from the installation package. For this you can open the MSI file with Orca, go to the "Property" table and get the value of the "ProductCode" property.
I can adapt the Uninstall MSI example from your website and use it to invoke msiexec.exe via a Custom Action, removing the old version before the new version begins its installation process.
For this you can use a "Launch file or open URL" custom action scheduled under "InstallUISequence" -> "FindRelatedProducts". Note that this will work only if the installation uses an UI because the custom action cannot run during the "InstallExecuteSequence".
when you have an Upgrade and you set it to "Detect only, placing results in property" -- what actually gets stored in the property?
The specified property will contains the Product Codes of the versions detected on the target system.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cbono
Posts: 42
Joined: Thu Aug 14, 2008 10:11 pm

Re: Uninstall previous version, regardless of installation type?

Hi Cosmin,

I understand what you're saying about getting the Product Code out of the installation package. Unfortunately this doesn't help me because not all of our users are on the latest version of our product. Therefore, we HAVE to detect the currently installed version on each user's machine and get rid of it; hard coding a particular Product Code is not feasible.

What I'm saying in my last post is that the new installation package detects the old product and looks up the Product Code (see Log File excerpt from my last post). Yet because the installation types differ, the new installation package ignores it and discards the Product Code. It does not place it in OLDPRODUCTS, nor does it place it in the custom property when I add an Upgrade with the "Detect only, placing results in property". These methods ONLY work when the installation types match between the old and new install packages. Frustrating!

It would be so much easier if the "Detect only" option for upgrades actually saved that Product Code, regardless of the installation type. But I don't know if this is a limitation of the MSI or of Advanced Installer.

Fortunately, I do have a work around that uses a custom DLL. I will gather the information about it and add it to this thread.


-Chris
cbono
Posts: 42
Joined: Thu Aug 14, 2008 10:11 pm

Our solution using Custom Actions

Here is my solution to uninstalling a previous software version, regardless of whether it was installed "per user" or "per machine."

We're using some custom actions to look for the previous version of our software. If found, the Product Code is loaded into the existing OLDPRODUCTS property, which signals the MSI package to automatically uninstall it during the InstallExecuteSequence > RemoveExistingProducts phase.

It makes for a nice and clean uninstall experience. You don't have to worry about invoking the MSIEXEC executable or running 2 installer processes concurrently.

Note that we changed the Upgrade Code of our newest software (v3.*) from the previous version (v2.*). In this way, we don't have to worry about other MSI upgrade behavior interfering with this custom process.

How we do it:
First, we wrote a custom DLL to look for our old software. It's called OldProductFinder.Dll, and it uses the Upgrade Code and the MSI API to search for any installed 2.* version and get its Product Code. We then save the Product Code to a user property called "OLD_PRODUCT_GUID" (defined on AI's Install Parameters page, and defaulted to empty brackets - "{}").

Source code for this DLL is downloadable here. Code was adapted from the Serial Number Validation sample. The logic for the main module follows:

Code: Select all

#include "stdafx.h"

BOOL APIENTRY DllMain(HANDLE /*hModule*/, DWORD  /*ul_reason_for_call*/,
                      LPVOID /*lpReserved*/)
{
  return TRUE;
}

const int cchGUID = 38;
const char lpUpgradeCode[cchGUID+1] = _T("{CD741E6B-0EF4-41B6-BC0B-34B669B0DCC2}");

UINT __stdcall FindOldProductCode(MSIHANDLE hInstall)
{
	
	UINT uiStatus = ERROR_SUCCESS;
	
	// Product Code buffer
	TCHAR szProductCode[cchGUID+1] = {0};
	
	uiStatus = MsiEnumRelatedProducts(lpUpgradeCode, 0, 0, szProductCode);
	
	if (uiStatus == ERROR_SUCCESS) {
		
		// Place Product Code into MSI property
		UINT res = MsiSetProperty(hInstall, _T("OLD_PRODUCT_GUID"), szProductCode);
	}
	else if (uiStatus == ERROR_NO_MORE_ITEMS) {
		// Do nothing
	}
	else {
		return 1;
	}
	
	return 0;
}
As you see, our old Upgrade Code is hard-coded into the DLL. Obviously, you'll need to update that to your own Upgrade Code (a great enhancement would be to send the Upgrade Code to the DLL as a parameter).

Once we have the DLL, we attach it to a custom action from AI's Custom Actions page.
  1. Expose the InstallUISequence > FindRelatedProducts standard action, if not already visible.
  2. Right-click the standard action and select "New Attached Custom Action"
  3. Browse to find the DLL.
  4. AI should set the Source Type to "Dynamic Linked Library (*.dll)" and set the Function Name to "FindOldProductCode"
Now when we run the AI installer, the "OLD_PRODUCT_GUID" property will be populated with the Product Code. All we have to do now is put that Product Code into the OLDPRODUCTS property in order to get the automatic uninstall behavior we need.
  1. Expose the InstallExecuteSequence > InstallValidate standard action, it not already visible.
  2. Right-click the standard action and select "New Custom Action > Property set with Formatted"
  3. In the Property Name field, type "OLDPRODUCTS" (all caps, no quotes)
  4. In the Formatted Text field, type "[OLD_PRODUCT_GUID]" (no quotes) -- you can also select it via AI's autocomplete
  5. Set the Execution Condition "Expression" field to "OLD_PRODUCT_GUID<>{}" (no quotes)
That's it! The old product, whatever version it may be, should be removed during the "RemoveExistingProducts" phase of the installation.

Note that I am NOT a C++ developer, so I make no guarantees about the correctness of my sample DLL code. Use it at your own risk. I'm fairly certain someone who knows what they're doing could improve on it a lot.

Also, I'd like to hear from the AI folks regarding this solution. Any suggestions, improvements, advice are welcome.

Hope this provides some help to people in the same situation as me.


Best,
Chris
Cosmin
Posts: 5797
Joined: Tue Jul 10, 2007 6:39 pm
Contact: Website

Re: Uninstall previous version, regardless of installation type?

Hi Chris,
It would be so much easier if the "Detect only" option for upgrades actually saved that Product Code, regardless of the installation type.
This is done automatically by Windows Installer, so it cannot be controlled. Since the detection works only for packages which have the same installation type, you cannot use it to detect an older version with a different install type.
As you see, our old Upgrade Code is hard-coded into the DLL.
Please note that you can get the Upgrade Code by using the UpgradeCode property and the MsiGetProperty function.
All we have to do now is put that Product Code into the OLDPRODUCTS property in order to get the automatic uninstall behavior we need.
Note that you can set the OLDPRODUCTS property directly from the custom action, instead of setting a custom property (like OLD_PRODUCT_GUID).
Also, I'd like to hear from the AI folks regarding this solution.
This solution seems correct and it should work as expected.

Regards,
Cosmin
Cosmin Pirvu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
cbono
Posts: 42
Joined: Thu Aug 14, 2008 10:11 pm

Re: Uninstall previous version, regardless of installation type?

Cosmin,

Thanks for reviewing my solution. I had wondered if I could set OLDPRODUCTS directly from the DLL. Because it's an MSI property, I was worried that something else might overwrite it or wipe it out before the process got to the "RemoveOldProducts" step.

I've updated our solution to use that suggestion, as well as looking up a Property to get the Upgrade Code.

New solution source code available for download here (this is a new download; the old one referenced in my earlier post still available).

Here's the main module logic:

Code: Select all

#include "stdafx.h"

BOOL APIENTRY DllMain(HANDLE /*hModule*/, DWORD  /*ul_reason_for_call*/,
                      LPVOID /*lpReserved*/)
{
  return TRUE;
}

const int cchGUID = 38;

UINT __stdcall FindOldProductCode(MSIHANDLE hInstall)
{
	
	UINT uiStatus = ERROR_SUCCESS;

	// Upgrade Code buffer & length
	TCHAR* szUpgradeCode = NULL;
	DWORD cchUpgradeLen = 0;
	
	// Look up the Upgrade Code;
	// Perform an initial lookup to get size of UPGRADE_CODE
	uiStatus = MsiGetProperty(hInstall, _T("UPGRADE_CODE"), _T(""), &cchUpgradeLen);
	if (ERROR_MORE_DATA == uiStatus) {
        
        // Initialize length and buffer
        ++cchUpgradeLen; // on output does not include terminating null, so add 1
        szUpgradeCode = new TCHAR[cchUpgradeLen];
        
        // Perform real lookup for UPGRADE_CODE
        if (szUpgradeCode) {
            uiStatus = MsiGetProperty(hInstall, _T("UPGRADE_CODE"), szUpgradeCode, &cchUpgradeLen);
        }
    }

	// Return if the property could not be retrieved
	if (uiStatus != ERROR_SUCCESS) {
		return 1;
	}
	
	// Product Code buffer
	TCHAR szProductCode[cchGUID+1] = {0};
	
	// Lookup the Product Code;
	// If more than one Code is returned, use the first one
	uiStatus = MsiEnumRelatedProducts(szUpgradeCode, 0, 0, szProductCode);
	
	if (uiStatus == ERROR_SUCCESS) {
		
		// DEBUG
		//MessageBox(0, _T(szProductCode), _T("Result"), MB_ICONSTOP);
		
		// Place Product Code into MSI property
		UINT res = MsiSetProperty(hInstall, _T("OLDPRODUCTS"), szProductCode);
	}
	else if (uiStatus == ERROR_NO_MORE_ITEMS) {
		// Do nothing
	}
	else {
		return 1;
	}
	
	return 0;
}
So now all you need to do is set a custom Property called "UPGRADE_CODE", then associate the DLL with a custom action as I outlined in my earlier post. It should take care of the rest. Of course, the normal disclaimer about using this code at your own risk applies.

Thanks for your help.

-Chris

Return to “Common Problems”