Archive for the ‘Coding’ 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:  }

Snippet: Enable Low Fragmentation Heap for all heaps

Monday, February 23rd, 2009

The following code is a code snippet that enable the Low Fragmentation Heap for all heaps. Remembers that a process has at last one heap but for example a CRT console application has practically at last two heaps. The first heap is default heap of process and the second is the CRT heap. If you use a lot of external DLLs is very common that one of this DLL allocate a private heap.

   1:  // code snippet that try to enable LFH for all heaps of your runnig program.
   2:      HANDLE* hHeaps = NULL;
   3:      int numHeap = GetProcessHeaps(0, NULL);
   4:      if(numHeap > 0)
   5:      {
   6:          ULONG ulEnableLFH = 2;
   7:          hHeaps = (HANDLE*)malloc(sizeof(HANDLE)*numHeap);
   8:          if(GetProcessHeaps(numHeap, hHeaps) == numHeap)
   9:          {
  10:              for(int i = 0; i < numHeap; i++)
  11:              {
  12:                  if(!HeapSetInformation(hHeaps[i], HeapCompatibilityInformation, &ulEnableLFH, sizeof(ulEnableLFH)))
  13:                  {
  14:                      printf("Failed to enable LFH for heap %d.", i);
  15:                  }
  16:              }
  17:          }
  18:          else
  19:          {
  20:              // quite strange !!!
  21:          }
  22:          free(hHeaps);
  23:      }

Suggested reading Windows Internals 4th edition for more details.

Analisi da riga di comando di un dump

Wednesday, February 11th, 2009

Nell’ultimo week-end, un applicazione in sviluppo è terminata inaspettatamente un infinità di volta. Generando un gran numero di memory dump, tutti di dimensioni considerevoli. La domanda sorge spontanea.
Come posso analizzare tutti questi dump senza sprecare un sacco del mio tempo?

(more…)

How to use NtQuerySystemInformation

Saturday, December 27th, 2008

NtQuerySystemaInformation http://msdn.microsoft.com/en-us/library/ms724509(VS.85).aspx is useful undocumented and discouraged API of ntdll.dll. This API shouldn’t be used but in some case is a short way to get a lot information.

With this API you can make a lot of thinks but in this example is used to get some information about process.

This function should be linked at runtime, see more information at this link: http://msdn.microsoft.com/en-us/library/ms686944(VS.85).aspx.

To invoke NtQuerySystemInformation you should pass the follow argument:

  • Kind of information that you want get, in this example the SystemProcessInformation.
  • Address of a chunk of memory to place all information.
  • Size of previous chunk of memory.
  • A integer passed by reference that will filled with the size of return value.

Unfortunately size of second arguments isn’t know, a solution is pass a chunk of memory and if it is too small retry. This solution is possible because the NtQuerySystemInformation in case of a chunk of memory is too small it return a specific error code.

In this example enumerations and structures used by this program are placed in stdafx.h header file.

This example is written with Visual Studio 2008.

Download example

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:  }

Hug a developer today

Sunday, September 14th, 2008

Thanks Franks

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…)

STRIP Planner RoboCaffè

Monday, August 25th, 2008

Si tratta di un progettino per il corso di inteligenza artificiale, in sostanza è un pianificatore STRIPS scritto in prolog (algoritmo usato A*).
Il problema che modella è quello di un robot che deve portare il caffè a varie persone disponendo di due macchinette del caffè.
(more…)

Quick install JBoss + MySQL su Win

Sunday, August 24th, 2008

Questa piccola guida serve a spiegare a chi non ha conoscenze tecniche da Sistemista/Programmatore di installare delle Web Applicazioni e testarle.

Prima di tutto bisogna procurarsi gli installer di MySQL (io installo la 4.1) e l’installer di JBoss (scarico la 4.0.x l’installar che è un file jar). Come prerequisito deve essere gia installato Java nella versione 1.4 o superiore dal sito di sun. (more…)