August 1, 2012

C# File OpenAs Dialog (Win7)

Some time ago I ran into something weird. These lines of code worked perfectly the last years in order to show the open as dialog on Windows XP but stopped working on my x64 Windows 7 machine (it would have stopped working in Vista too, but I skipped this version).
ProcessStartInfo startInfo=new ProcessStartInfo() { WindowStyle=ProcessWindowStyle.Normal, FileName=fileName, Verb="openas", UseShellExecute=true, ErrorDialog=true, }; p=Process.Start(startInfo);
Well to solve this issue I needed to check how the system used to show the open as dialog. As a result, I ended up with this line of code which brings up the open as dialog as usual.
p=Process.Start("rundll32.exe", string.Format("shell32,OpenAs_RunDLL \"{0}\"", fileName));
By the way, you'll need both versions if you want that your application works on Windows XP and later versions. This line indicates whether the application is running on Windows Vista or later.
Environment.OSVersion.Platform==PlatformID.Win32NT && Environment.OSVersion.Version>=new Version(6, 0, 6000);

March 16, 2012

Skype [beta] for WP7

I was wondering where I can download Skype beta for my Windows Phone 7 but I just couldn't find it in the Windows Phone Marketplace. Well, if you are searching for it too, use this direkt link on your WP7 :-)
zune://navigate/?appid=c3f8e570-68b3-4d6a-bdbb-c0a3f4360a51
I found this link somewhere on the skype website - but very very hidden.

January 19, 2012

IntPtr Navigation

Have you ever wrote something like that?
IntPtr ptr=...
Marshal.PtrToStringUni((IntPtr)(ptr.ToInt32()+wDeviceOffset));

Such code is often necesary when invoking the Win32 API. Well this code works well unless you're not running this code on a x64 machine.
When running on a x64 machine you might get a nasty surprise as soon as the pointer's address is above int.MaxValue. To fix this problem you might use the following code.
IntPtr ptr=...
Marshal.PtrToStringUni(IntPtr.Add(ptr, wDeviceOffset));

There also exists a static Subtract method which let's you decrease the pointer's address.

January 10, 2012

Random <> Random

Yes, Random is not Random at all. The Random class in .NET gives you the functionality to produce random numbers. Anyway how is randomness being calculated? There are plenty of factors used for calculating a new random number. To read a little more, checkout the documentation on MSDN.
Even though the documentation recommends creating just one instance of the Random class and using this instead of creating a new instance every time you need one.
You don't think this is necessary? Well let's just do a simple experiment:

We will now create random numbers. Both snippets use a simple generic list of integers.
var items=new List<int>();

The first snippet creates a new instance of the Random type on each call. I also want to highlight that the loop will run 10.000 times creating random numbers from 1 to 30.
for (int i=0; i<10000; i++)
  items.Add(new Random().Next(1, 30));

The second snippet only creates one instance and reuses it on each call.
Random rnd=new Random();
for (int i=0; i<10000; i++)
  items.Add(rnd.Next(1, 30));

To get the results, we use a StringBuilder object and just write the distinct numbers into it
var sb=new StringBuilder();
foreach (var i in items.Distinct().OrderBy(x => x))
  sb.AppendFormat("{0},", i);
Console.WriteLine("Nums: {0}", sb.ToString());

Now, let's have a look at the results:
The first style returned the following output
3,6,11,16,22,28,

The second style this output
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,

So now you see that using the first style really doesn't provide random numbers. Only 6 numbers out of 30 have been used!
On the opposite the second style seems to return usable random numbers - all numbers have been used.