Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Jan 22, 2013 6:57 am Post subject: Can I uninstall licenses when my application is uninstalled? |
|
|
Yes. If you are using ILS 5.8 or later with Authenticated Licenses then the end user can deactivate an authentication for a computer by uninstalling the license. You may want to also automatically uninstall the license when you application is uninstalled. You can do this by writing a custom uninstall action for your installer that calls the AuthenticatedLicenseProvider Deauthenticate and UninstallLicense methods.
Below is some code for a WIX custom action that checks if there is a license installed and gives the user the option to uninstall it.
Code: | /// <summary>
/// Defines custom actions for a WIX installer
/// </summary>
public class CustomActions
{
/// <summary>
/// License Validation Parameters String copied from the License Key Generator (formatted with line breaks
/// to make more readable)
/// </summary>
const string LICENSE_PARAMETERS = @"<AuthenticatedLicenseParameters>...</AuthenticatedLicenseParameters>";
/// <summary>
/// The name of the file the license key is stored in
/// </summary>
static string _licenseFile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\MyAuthenticatedProduct\MyAuthenticatedProduct.lic";
/// <summary>
/// Show an MSI message box with the given parameters
/// </summary>
/// <param name="session">The MSI session</param>
/// <param name="text">The text to display</param>
/// <param name="icon">The icon</param>
/// <param name="buttons">The buttons</param>
/// <param name="defaultButton">The default button</param>
/// <returns>The MessageBox result</returns>
/// <remarks>
/// We can't just use the standard Windows.Forms.MessageBox because it hides behind the installer
/// and doesn't handle silent installs.
/// </remarks>
private static MessageResult ShowMessageBox(Session session,
string text,
MessageBoxIcon icon,
MessageBoxButtons buttons)
{
Record record = new Record(1);
record.SetString(0, "[1]");
record.SetString(1, text);
int flags = (int)InstallMessage.User + ((int)icon | (int)buttons);
return session.Message((InstallMessage)flags, record);
}
/// <summary>
/// Show a question to the user and get the result
/// </summary>
/// <param name="session">The MSI session</param>
/// <param name="text">The question text</param>
/// <param name="defaultButton">The default button</param>
/// <returns>True if the user clicks yes</returns>
private static bool ShowQuestion(Session session, string text)
{
MessageResult result = ShowMessageBox(session, text, MessageBoxIcon.Question, MessageBoxButtons.YesNo);
return result == MessageResult.Yes;
}
/// <summary>
/// Show an Error to the user
/// </summary>
/// <param name="session">The MSI session</param>
/// <param name="text">The error text</param>
private static void ShowError(Session session, string text)
{
ShowMessageBox(session, text, MessageBoxIcon.Error, MessageBoxButtons.OK);
}
/// <summary>
/// Uninstall the license if installed
/// </summary>
/// <param name="session">The MSI session</param>
/// <returns>The result</returns>
[CustomAction]
public static ActionResult UninstallLicense(Session session)
{
try
{
AuthenticatedLicenseProvider provider = new AuthenticatedLicenseProvider(LICENSE_PARAMETERS, _licenseFile);
AuthenticatedLicense license = provider.GetLicense();
if (license != null)
{
if (ShowQuestion(session, string.Format(LicenseResources.UninstallLicenseMsg, provider.Parameters.ProductName)))
{
bool done = false;
while (!done)
{
try
{
provider.DeauthenticateLicense(license);
done = true;
}
catch (DeauthenticationsExceededException)
{
ShowError(session, LicenseResources.DeauthenticationsExceededMsg);
done = true;
}
catch (WebException)
{
done = !ShowQuestion(session, LicenseResources.DeauthenticationConnectionErrorMsg);
}
catch (Exception e)
{
string msg = string.Format(LicenseResources.DeauthenticationErrorMsg, e.Message);
done = !ShowQuestion(session, msg);
}
}
provider.UninstallLicense();
}
}
}
catch (Exception e)
{
ShowError(session, string.Format(LicenseResources.UninstallErrorMsg, e.Message, _licenseFile));
}
return ActionResult.Success;
}
} |
Note that this uses the MSI session.Message method to display messages rather than System.Windows.Forms.MessageBox because standard forms message boxes get hidden behind the installer.
The custom action is called only when the product is being uninstalled by setting up InstallExecuteSequence as shown below:
Code: | <CustomAction Id="UninstallLicense"
BinaryKey="LicenseUninstaller"
DllEntry="UninstallLicense"
Execute="immediate"
Return="check" />
<InstallExecuteSequence>
<Custom Action="UninstallLicense" Before="RemoveFiles">Installed AND NOT UPGRADINGPRODUCTCODE AND (REMOVE="ALL")</Custom>
</InstallExecuteSequence> |
_________________ Infralution Support |
|