Showing posts with label BCL. Show all posts
Showing posts with label BCL. Show all posts

4 April 2012

Query KB updates installed in Window 7

Prior to Windows Vista, WMI's Win32_QuickFixEngineering class was used for querying the KB updates installed in the system. But, Starting with Windows Vista, win32_QuickFixEngineering returns only the updates supplied by Component Based Servicing (CBS). It will not return updates installed by MSI or Windows Updates. Read this page for more info. So, to query the updates installed via MSI or Windows Updates, you can use Windows Update Agent Object Model. Here I am going to show you, how to query the installed KB updates in C#.

We begin by adding reference to Wuapi.dll. You can do that by selecting "WUAPI 2.0 Type Library" from COM tab in Add Reference dialog in Visual Studio.


Once you add reference to Wuapi.dll, you can create an object of WUApiLib.UpdateSession class. This class cab be used by the caller to perform operations that involve updates, such as download, install or search for updates. Once you have UpdateSession object ready, you can call QueryHistory on it to get history of installed updates. Let me show you the code directly.

WUApiLib.UpdateSession session = new UpdateSession();
IUpdateSearcher us = session.CreateUpdateSearcher();

var updatesCollection = session.QueryHistory(String.Empty, 0, us.GetTotalHistoryCount());
for (int i = 0; i < updatesCollection.Count; i++)
{
    Console.WriteLine(coll[i].Title);
}

Note that, the list of updates resulted from above code may not match with the list of updates in Add/Remove Programs window in control panel.  The missing updates might be installed via Component Based Servicing or they are non-microsoft updates.

References:
  •  Searching, Downloading, and Installing Updates (link)
  •  Using the Windows Update Agent API (link)

10 February 2012

Contravariance: Is it really what it is?

By definition, contravariance referes to a situtaion where you assign base class instance to a child class reference. Let's directly see an example,

class BaseClass
{
}

class ChildClass: BaseClass
{
}

class TestContraVariance
{       
    delegate void SomeContraVariantDelegate(ChildClass argument);

    public void CreateContravariantDelegate()
    {           
        SomeContraVariantDelegate cvd = new SomeContraVariantDelegate(EventHandlerMethod);
    }

    private void EventHandlerMethod(BaseClass arg)
    {
    }
}

Here, you can see the contravariance - Delegate SomeContraVariantDelegate expects a method that accepts ChildClass instance. But, the function EventHandlerMethod accepts an argument of type BaseClass. So, it appears that a base class object is assigned to a child class object which is not possible in normal scenarios. But, is it really what it seems to be? I don't think so. Let me explain,

In the above example, if you see carefully, the delegate calls the method but not method calls delegate. So, when delegate calls EventHandlerMethod it sends a ChildClass instance as parameter. This is perfectly valid because the method accepts BaseClass instance and the delegate passes ChildClass instance. So, actually ChildClass instance is assigned to BaseClass object but not the other way.

So, there exists nothing like assigning assigning BaseClass instance to ChildClass object and hence no contravariance like thing exists too.

22 December 2011

Obtaining Culture Information through FormatProviders

Often we come across the situations where we need to read the culture specific information like date/time format, decimal seperator, currency symbol, number styles etc. Mostly we need this information for formatting the output according to user culture. In this post, I will show you how you can obtain such culture specific information.
That said, without spending much time in theory, let's see where we find this information. As you might know, System.Globalization.CultureInfo class is the one you must target for this purpose. This class, CultureInfo, provides a public function called GetFormat (a method declared in IFormatProvider interface) whose signture looks like below,
 public virtual object GetFormat(Type formatType);
As of now, formatType paramter can be either NumberFormatInfo or DataTimeFormatInfo both of which are defined in System.Globalization namespace. The NumberFormatInfo class provides you information about number system such as Positive Sign, Negetive Sign, Decimal seperator, Currency Symbo, Native Digits etc. On the other hand DateTimeFormatInfo provides information about Date and Time formats like, DateSeperator, FirstDayOfWeek, LongDate format, ShortDate format, Time Seperator etc.
 
For example, here is how I get the currency symbol for the culture fr-FR.
 Type formatType = typeof(NumberFormatInfo);  
 CultureInfo frCulture = CultureInfo.GetCultureInfo("fr-FR");  
 NumberFormatInfo info = frCulture.GetFormat(formatType) as NumberFormatInfo;  
 String currencySymbol = info.CurrencySymbol;
I think the above example gives you the whole idea on how to get culture specific information. Below is the code that will give you all DateTime format info that is possible to get from current culture. You can use this code for NumberFormatInfo as well (just replace DateTimeFormatInfo with NumberFormatInfo).

 public String GetDateTimeFormatInfo(String cultureName)  
 {  
   CultureInfo cultInfo = CultureInfo.GetCultureInfo(cultureName);  
   var formatInfo = (DateTimeFormatInfo)cultInfo.GetFormat(typeof(DateTimeFormatInfo));  
   PropertyInfo[] properties = formatInfo.GetType().GetProperties();  
   String info = String.Empty;  
   foreach (var prop in properties)  
   {  
      if (prop.GetValue(formatInfo, null) is Array)  
      {  
         Array a = prop.GetValue(formatInfo, null) as Array;  
         String values = String.Empty;  
         foreach (var item in a)  
         {  
            values += item + ",";  
         }  
         info += prop.Name+":"+values.Substring(0,values.Length-1);  
         info += Environment.NewLine;  
      }  
      else  
      {  
         info += prop.Name + " : " + prop.GetValue(formatInfo, null);  
         info += Environment.NewLine;  
      }  
   }  
   return info;  
 }  
Note: You  can also obtain all this information from registry. If you are keen, you can have examine the below registry key.

 HKEY_CURRENT_USER\Control Panel\International