Archive for the ‘.NET’ Category

Performance: CLI vs Win32 API

Monday, May 25th, 2009

In my little free time I develop some open source projects, for example my last creature is “Minimalistic Explorer“. This project is made in C# WPF for the UI and actually core is written in C#, but I hope to port the existing core in C++/CLI.

hd_me_735243

(more…)

Snippet: Icon from shell to ImageSource with alpha channel

Thursday, May 21st, 2009

The following code snippet performs the following task:

  1. Get icon of the given file
  2. Convert in a bitmap preserving the alpha channel
  3. Convert it in a ImageSource

This code is shortcut to retrieve the displayed icon of a file. alternative way to do this is useing shell32.dll and SHGetFileInfo.

I have not used IconBitmapDecoder becuase this class lost the alpha channel.

   1:  public BitmapSource GetIconFromPath(string path)
   2:  {
   3:      System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(path);
   4:      System.Drawing.Bitmap bitmap = icon.ToBitmap();
   5:      return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),
   6:              IntPtr.Zero, System.Windows.Int32Rect.Empty,
   7:              System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
   8:  }

Reflection tip, IsOverridden

Friday, September 19th, 2008

Reflection is a power tool of managed language like Java and .NET (C#, VB.NET,C++/CLI and others). In the last days I have discovered that MethodInfo class don’t provide a method to check if is overridden or not.This activity is vary simple but on internet you can found a dozen of example about Reflection without one about this case.


   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using System.Reflection;
   5:  
   6:  namespace ReflectionMethod
   7:  {
   8:      internal class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              BaseClass derived = new DerivedClass();
  13:  
  14:              Type type = derived.GetType();
  15:  
  16:              MethodInfo method1 = type.GetMethod("Method2");
  17:  
  18:              Console.WriteLine(String.Format("This method is overridden? {0}",
  19:                  IsOverridden(method1)));
  20:  
  21:              Console.ReadKey();
  22:          }
  23:  
  24:          public static Boolean IsOverridden(MethodInfo info)
  25:          {
  26:              MethodInfo baseDef = info.GetBaseDefinition();
  27:  
  28:              return !(info.DeclaringType.FullName == baseDef.DeclaringType.FullName);
  29:          }
  30:      }
  31:  }

Make WiX setup for a Wizard of Visual Studio

Sunday, August 31st, 2008

For learns the WiX toolset I have decided to write a setup for install a Wizard of Visual Studio. This setup is very easy with WiX and his Visual Studio extension.

This wxs file make the follow steps:

  1. Install the IWizard implementation in the GAC repository of target system.
  2. Copy the Template for Visual Studio 2005 or 2008 in his ProjectTemplate folder.
  3. Execute the command to install the template in visual studio (devenv /installvstemplates)

(more…)

Transform Asynchronous method to Synchronous

Sunday, June 22nd, 2008

In the last day, I have found problem to transform an asynchronous call to synchronous. It is not very big problem but this problem is repeated for a big number of methods and components. I think that a good programmer should avoid the copy/paste of code but write a reusable code, for this I write a class that implement a lock/unlock mechanism.
This class has three methods

  • Lock: Lock the object.
  • Unlock: Unlock the object.
  • Wait: Caller remains to wait the Unlock method is called.

The implementation is based over the ReaderWriterLock class, this class protect an enumeration that describe the status of class

(more…)

IWizard and MultiProject

Wednesday, April 16th, 2008

Example: click here.

It is a Draft document In the previous month I should create a Wizard that it generates a solution with 3 complex projects. The first thinks is use the IWizard interface of Visual Studio. It works fine with a single project template but when you create a MultiProject template this mechanism doesn’t work! For exactly the replacement mechanism that is embedded in Visual Studio don’t propagates the repalcementsDictionary to the other template. I have resolved this problem in the follow way: I have added in each template a WizardExtenstion and a WizardData. In this way for each template is called my IWizard implementation and with static members a communicate between IWizard instances. In each call of RunStart I add in the repalcementsDictionary the correct value (for example projectname and all custom template parameters) for the current template

At this link you can download an example of solution click here.

Installing the example

In the example you can find a folder called “Template” and “IWizardImpl”, in the first folder you find the template that should be decompressed in <User>/Visual Studio 2008/Templates/ProjectTemplates/Visual C#/ (the same thing with VS2005 but the folder is Visual Studio 2005 not Visual Studio 2008) folder.

After that you must register in the GAC the IWizardImpl.dll assembly. This example came with a Debug version placed in IWizardImpl\bin\Debug folder.

You should call the “gacutil.exe” application from the Visual Studio command line or providing full path (for example” C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe”) with the /I param and passing the dll that you won’t store in the GAC.

gacutil.exe /i IWizardImpl.dll

For more detail you can found information about this steps on the MSDN about Visual Studio IWizard.

Example structure of template

The template folder contains a MutiProject template this template refers other two templates . Each templates call the same Wizard but each call has a different custom parameter.

This custom parameter is defined in the WizardData tag, an example is the follow portion of template file of UserControl/MyTemplate.vstemplate.

<WizardData Name="ProjectPart">UserControl</WizardData>

This tag at runtime means that in the replacementDictionary an element with key “ProjectPart” and value “UserControl” is added and you can read this value.

Summering the steps

  1. Understands if the calling template is a MultiProject or a Project with WizardRunKind enumeration
    1. If it is a MultiProject, shows my wizard and collect some information and stores it in statics members.
    2. If a Project I read the custom parameter to understand if I working over the Skins or UserControl. Go to step 2
  2. Populates the replacementDictionary with my customization and replacing for example the projectname with an appropriate value.

This steps are useful for avoiding the problem of no replacing mechanism don’t work properly in the case of MultiProject template.