Simulating an OR Condition in the Bootstrapper Package

2011/05/04 Leave a comment

The installation bootstrapper created by the GenerateBootstrapper task in Visual Studio does not have support for compound conditions in the product.xml file. There is however a way to simulate an OR condition as I did with a product that required either Excel version 12 or 14 to be installed.

If you read carefully the description of the RegistryFileCheck you will notice that it says, “If this key does not exist, Property is not set.” That subtle, but very important statement started me thinking. If I were to do two or more RegistryFileChecks into the same property, would it indeed leave the results of the first check if the second key were absent? Yes, it does. Below is an example of how to use this information to determine if Excel 12 or 14 is installed.

<InstallChecks>
  <RegistryFileCheck Property=”ExcelVersion”
    Key=”HKLM\Software\Microsoft\Office\12.0\Excel\InstallRoot”
    Value=”Path” FileName=”EXCEL.EXE” SearchDepth=”0″ />
  <RegistryFileCheck Property=”ExcelVersion”
    Key=”HKLM\Software\Microsoft\Office\14.0\Excel\InstallRoot”
    Value=”Path” FileName=”EXCEL.EXE” SearchDepth=”0″ />
</InstallChecks>

The two checks above leave the property ExcelVersion in one of two states. Either it will contain the version of the higher version of Excel that is installed or it will be “NotExists” if both version 12 and 14 are absent

Once you have the “ExcelVersion,” it is easy to use to fail the installation if neither version of Excel is installed. Add the following to the InstallConditions portion of the Commands in the product.xml file.

<FailIf Property=”ExcelVersion”
  Compare=”ValueNotExists” String=”Excel2007SP2″/>

Categories: Windows Development Tags:

Displaying the UAC Shield in WPF with an Adorner

2009/11/22 2 comments

Since creating an elevated managed COM object, I have, on odd occasions, given thought to how to display the UAC Shield Icon in a WPF application.  After some research failed to uncover any useful information, I embarked on the task of creating a manageable method for displaying the icon.  While a full general purpose solution might be nice, a button that can display the icon as needed, should be sufficient for most purposes and demonstrates a methodology to follow for other controls.

Since I limited my implementation to a button, I created the UACShieldButton derived from Button.  By deriving from Button, all of the click and commanding handling were already done.  I left the final decision to display the icon with the UACShieldButton class.  The caller simply indicates that it desires the shield to be displayed if all other conditions, elevation state and UAC enabled operating system, are correct.

The obvious items required are a method of the calling code to indicate the desire for the shield and the shield icon itself.  Since I wanted binding to work with these properties, they were implemented as dependency properties.  The DesiresShield dependency property represents the calling code’s desire to have the shield displayed.  The ShieldIcon dependency property is an ImageSource for the image to display.  This property will default to the system shield icon, System.Drawing.SystemIcons.Shield through the default value set in the UACShieldButton’s static constructor.

If you have been following along, you have noticed I keep indicating the code utilizing the UACShieldButton class indicates a desire to have the shield displayed.  By allowing the UACShieldButton class to make the final decision to display the shield, some complication is hidden from the caller and code is consolidated into one location.  The shield will be displayed if the three following conditions are true:  the shield is requested, the operating system’s major level is 6 or above (Vista and Server 2008 and higher) and the caller is not already elevated.  The test for elevation is the only one that is not entirely straight forward.  As it happens, the old Win32 API call IsUserAnAdmin supplies the needed information.  Even though the documentation indicates membership in the Administrator’s group, under UAC enabled systems, it indicates that the user is currently elevated to administrator status.

The other nicety provided is the automatic handling of different ToolTips depending on elevation state.  Generally, the non-elevated state indicates that the associated out-of-process COM server has not been created.  Since it is not created, any state information that it would supply is unavailable.  By providing for the automatic handling of the display to different ToolTips according to the elevation state, the UACShieldButton control automatically provides the user with the correct ToolTip.  For example, assume you have a Service Controller Interface COM server and Start and Stop buttons to stop or start a service.   Until such time as the COM server is created, the state of the service is unknown.  The ToolTipNotElevated version of the ToolTip could indicate the lack of state information along with the effect of clicking the button.  Once the COM server is created, the ToolTipElevated ToolTip would be displayed simply indicating the effect of clicking the button.

The only logic that the caller must provide is the setting of DesiresShield to false, once the elevation is provided for any button.  You might be wondering why?  The elevation done here is elevating an out-of-process COM object as opposed to the program elevation that the UACShieldButton class can detect.

The trick to easily displaying the shield is to use an Adorner.  The alternative is to provide an alternate control template for the button and having created an alternate control template once, I do not relish doing it again.  The UACShieldAdorner class derives from Adorner and manages all aspects of displaying the shield.  The only tricky part of this code is the sizing and positioning algorithm.  The shield is resized proportionally to be four pixels less than the hosting button’s height.  It is then placed two pixels below and to the right of the upper left corner of the button.  Since the adorner layer is the topmost layer, the shield can obscure the text or the image in the button.

While this solution is not an end-all solution to displaying UAC Shield Icons, it is a starting point to that can be easily adapted to other controls and positions algorithms.

The code is here

Categories: Windows Development Tags: , , ,
Follow

Get every new post delivered to your Inbox.