Happy Holidays

Sat, December 22, 2007, 11:53 AM under Personal
The wife and I are going to the States on a little tour of the East Coast (Washington, Boston, New York, Niagara Falls and Philadelphia - with a return flight via Chicago), so things will be quiet on this blog 'till January some time. Upon return, photos will be posted to the usual place.

Allow me to take this opportunity to wish you and your loved ones a very enjoyable break from "the routine" :-)

Code signing

Fri, December 21, 2007, 05:21 AM under Windows | Vista
In all my Vista talks I emphasize that with Windows Vista, applications must be signed. Not just for UAC, but for other areas as well (e.g. WER) and it is an actual logo certification requirement. If you haven't dabbled with code signing before I guess it implies that you don’t work for a large enterprise. In that case, head over here for an end-to-end "how to" on code signing.

6 years of hotmail

Fri, December 21, 2007, 01:38 AM under Random
I registered with hotmail in September 2001. Have you had a hotmail account longer than that? To play the "mine is longer than yours" game, go over to Ian's blog (where he has the link on how to find out).

Volatile RegistryKey

Thu, December 20, 2007, 12:48 PM under dotNET | MobileAndEmbedded
When the Windows Mobile Managed APIs were introduced they made use of a new underlying Windows CE 5 feature (that WM 5.0 is based on): volatile registry keys (see dwOptions). Volatile keys are also available on normal/desktop/server Windows. In a nutshell, volatile registry keys do not survive an OS reboot so anything you write there is not persisted the next time you restart. The obvious question at this point is: Does the managed registry API support volatile keys? The answer is "no" for both the full .NET Framework and the .NET Compact Framework. Let's explore an UNSUPPORTED solution for both platforms.

On the surface without too much digging it appears that the native RegCreateKeyEx is wrapped by RegistryKey.CreateSubKey. So for the BCL team to add support for volatile keys to the managed API, one way would be to add a new overload to CreateSubKey that accepts our desire for volatileness and at the point it calls RegCreateKeyEx it passes in REG_OPTION_VOLATILE instead of 0 which is what the implementation does now (i.e. a change of one line of code).

But since we are not the BCL team and we cannot edit the library's code, we can create a helper static method to do that job for us. Maybe that method would live in the static Program class with a signature something like this:
public static RegistryKey CreateSubKey(this RegistryKey rk, string subkey, bool isVolatile)
if (!isVolatile)
return rk.CreateSubKey(subkey); // i.e. no change
// call our own method since we cannot change the framework's
return MothCreateVolatileSubKey(rk, subkey, RegistryKeyPermissionCheck.Default);
Note on the above:
1. I've made the method an extension method which means that we can call it in either of these two ways (the first being more natural):
RegistryKey rk1 = Registry.CurrentUser.CreateSubKey("my vol", true);

RegistryKey rk2 = Program.CreateSubKey(Registry.CurrentUser, "my vol", true);
2. As you can imagine the implementation of MothCreateVolatileSubKey is a simple duplication of the implementation of the existing framework method with 2 differences:
a) Where the framework's implementation passes 0 as the 4th argument to the native RegCreateKeyEx, we will pass REG_OPTION_VOLATILE.
b) Wherever the framework's implementation calls private/internal methods we will have to use reflection to access them (unless you fancy duplicating all of that too;)).

Yes, I know the above is very ugly, very unsupported and very much a hack. I have created a VS2008 solution with the above that you can use to test it out at your own risk. It contains two projects, one for NETCF to run on your Windows Mobile and one for the desktop.

It is quite possible that by running the code from the project your machine will be wiped clean and that your monitor will go up in flames after transferring all your money out of your bank account. You have been warned!

So, at your own risk, get the VS2008 projects here.

For UK techies only – 2008 launch registration

Tue, December 18, 2007, 07:14 AM under Links
You all know that 19th March is the UK launch of the three 2008 products. If you are interested in receiving an email about with the registration link a few hours before anyone else sees it, please register your interest here.

WPF for LOB apps?

Tue, December 18, 2007, 03:29 AM under Links
Tim asserts that most developers think WPF is irrelevant and blames Microsoft for it. I am as guilty as the next person about stating where I think WPF fits compared to WinForms. Of course, not everybody holds that view and Jaime backs up his thoughts with a pointer to an interesting article: WPF goes to work.

Assuming you have WPF experience, what do you think?

.NET Framework v2.0 SP1

Tue, December 18, 2007, 03:26 AM under dotNET
At the same time as .NET Framework 3.5 we shipped Service Pack 1 for .NET Framework v2.0. There is a standalone update for v2.0 SP1 for Windows XP and Windows Server 2003 and it ships out of the box with Windows Server 2008. For Windows Vista, it is included with Vista SP1. Of course, Fx v3. 5 depends on v2.0 SP1 so the installer for v3.5 will also install v2.0 SP1 if it is not on the machine already.

So what is in Fx v2.0 Service Pack 1?

1. Tons of bug fixes. See the list here.

2. Performance Improvements.
+ NGEN boosts.

3. Some new public APIs:
+ New members on the GC class.
+ DateTimeOffset class (this has also propagated to many other APIs that in addition to DateTime now use this new type e.g. XmlConvert.ToDateTimeOffset()).
+ Addition of the ShieldIcon for drawing.
+ IME additions.
+ File Dialog Enhancements.

4. Internal changes:
+ Increase of ThreadPool limit.

Some devs are aiming at using VS2008 to target Fx v2.0. Please note that you are really targeting v2.0 SP1. I blogged a pointer before on potentially addressing this goal, but it is important to stress again that the supported framework for VS2008 is v2.0 SP1 and not v2.0 RTM.

By the way, at the same time, the .NET Framework v3.0 SP1 was also released. Everything I wrote in the first paragraph of this blog post for v2.0 SP1 applies to v3.0 SP1 too. The list of fixes for v3.0 SP1 is in this KB article.

FileDialog additions in SP1

Mon, December 17, 2007, 04:32 AM under dotNET
In Windows Forms, the OpenFileDialog and SaveFileDialog inherit from the abstract FileDialog class. If you work with these classes you should know that there have been a few additions in this area as highlighted by the following class diagram.

There are broadly 3 enhancements:

1. The OpenFileDialog has a new property (SafeFileName) that returns just the name and extension of the selected file without the path information. Because of that it is accessible to applications running with limited trust. Should the selected files be more than one, you can use the corresponding SafeFileNames property that returns an array of strings.

2. There is a new property for both Save- and OpenFileDialog that is only applicable on Windows Vista (it gets ignored on XP): AutoUpgradeEnabled. The default is true, and it means that the dialogs will have the Vista look and feel when run on Vista.

3. If you have not opted out from auto upgrading for Vista just discussed, then another property of the 2 dialogs may be useful: CustomPlaces (of type FileDialogCustomPlacesCollection). You can use this to add strings (or Known Folder GUIDs) that represent locations on your disc that are then easily accessible to the user of the dialog you are showing via the "Favorite Links" section.

To see the Vista dialog in action with custom places, watch my screencast.


Sun, December 16, 2007, 11:04 AM under dotNET
It should be a known fact that Windows Forms controls support Input Method Editors out of the box, so you don't have to do anything special.

However, if you are writing your own control (e.g. UserControl) then with v2.0 of the Framework you may run into issues. This is a fairly niche scenario but if you are facing it then you should obtain hotfix 934197 that solves the issue by providing a property you can override in your control: CanEnableIme.

Also read the MSDN docs for the protected property.

Hopefully the details on the links above make it clear, but I also found this connect feedback item that makes it crystal clear with a repro. As per the MSFT response there, this property is now available in v2.0 Service Pack 1 on all controls. Maybe now that it is in an SP this angry guy here won't have such an issue distributing the fix...

Bonus IME topic: Also in v2.0 SP1, relevant to Chinese IMEs only, you will find a new member for the ImeMode enumeration for HalfShape: OnHalf.


Sat, December 15, 2007, 06:42 AM under dotNET | Windows | Vista | UAC
Seasoned Windows Forms developers will be familiar with the System.Drawing.SystemIcons class that has a bunch of properties returning standard system icons. For example, if you throw on a form a picturebox and a button with its event handler the following code shows how you can take advantage of SystemIcons:
void button1_Click(System.Object sender, System.EventArgs e)
// use Error
pictureBox1.Image = Bitmap.FromHicon(SystemIcons.Error.Handle);

// use Warning
this.Icon = SystemIcons.Warning;

// use Information
int h = button1.ClientSize.Height / 2;
Icon ico = new Icon(SystemIcons.Information, h, h);
Bitmap bitmap1 = Bitmap.FromHicon(ico.Handle);
button1.Image = bitmap1;
button1.ImageAlign = ContentAlignment.MiddleLeft;
With Service Pack 1 of .NET Framework v2.0 we get a new member of that class: SystemIcons.Shield. So, if you are on Windows Vista and you are working with User Account Control, you may find it a useful icon to use (for example on a menu).

Below is a screenshot of what the code above looks like side by side, before clicking the button and after. The screenshot at the bottom is after replacing the 3 icons with shield:

Be consistent with DllImports

Fri, December 14, 2007, 01:37 AM under dotNET
Most .NET developers have at some point in their career called into a native library (whether it was C\C++ DLL part of the Windows OS or one of their own or one from a 3rd party is irrelevant). To call native functions form .NET you use the DLLImport attribute and even though it has various overloads, the main thing we do is point it to the native DLL name (the Declare statement in VB does the same thing although I use DllImport in VB projects as well).

The subtle point is that there is no convention for how we specify the name so, for example, all of the following are valid and will work at runtime:
static extern ...;
static extern ...;
static extern ...;
static extern ...;
static extern ...;
Now, I know that there is no convention, but if you used the above this is what you would see in ILDASM (and indeed in reflector too):

You may think: "So what, isn't just one kernel32 loaded in my process at the end of the day?". Well, yes, but there is a slight performance penalty because the loader treats each separate string as a separate module!

Some of you are thinking that "for that performance reason alone" you should standardize on a convention e.g. always use lowercase and include the ".dll" extension... or something like that. Others are probably thinking that "the performance gains are negligible" for you to make a rule for this.

In my opinion (and it is my blog so don't be shocked that these are my opinions here!), you are both wrong and right at the same time. For me, this is an issue of hygiene. Once I learnt about this fact (many moons ago :-), I couldn't live with myself if in my assemblies I didn't define a standard for pinvoking. Nothing to do with performance – it just feels dirty to me not to use the same DLL name throughout my assembly. That's just the kind of person I am...

So I sighed when I discovered that System.Core.dll is the first and only assembly so far in v2.0/3.0/3.5 of the framework not to follow the "all lower case with .dll appended" convention, as viewing it through a disassembler proves :-(

MSDN Flash 12 Dec

Thu, December 13, 2007, 02:47 AM under Links
The MSDN Flash was delivered yesterday (subscribe here).

In every issue we have a feature article on a specific topic and this time it is about building Silverlight Sidebar Gadgets – you can view this issue online now.

Task and TaskManager

Wed, December 12, 2007, 02:42 PM under dotNET
In this 4th video in my series on the ParallelFX, I explore what it is like to program against the lowest level API in the Task Parallel Library: the Task and TaskManager. Watch it or download it from here.

DataSet enhancements in VS2008

Tue, December 11, 2007, 03:12 PM under dotNET | Orcas
For those of you using the DataSet, you'll be happy to know that there are enhancements to it, including these three:

1. Hierarchical Updates: Read.
2. Separate DataSets and TableAdapters for multi-tier solutions: Read, watch.
3. LINQ to DataSet: Read and Watch.

Bonus: Read a nice collection of How To and walkthrough articles for the dataset designer.

How do you start a user group

Tue, December 11, 2007, 03:20 AM under Events
Via Christopher, I found the post by Blogus Maximus on his 21 thoughts for running user groups.

I am finding it tough to add to the list above, can you? If you are already running a user group I'd love to read your thoughts (e.g. on your blog)...

If you are in the UK and are thinking of starting a user group read that useful list. When you are ready, follow his advice on #3 and shoot me an email!

LINQ to DataSet video

Mon, December 10, 2007, 09:07 AM under dotNET | Orcas | LINQ
I have already written about LINQ to DataSet, and for more details than what I provided you should follow the links I suggested on that post. In addition, I just published an 18' video on LINQ to DataSet which I hope you enjoy...

ParallelFX class diagrams

Sun, December 9, 2007, 04:22 PM under dotNET
In my videos on Parallel FX, I showed some class diagrams. I thought that you may want to see those independently of the video. Click on the links below to open the respective class diagram png image (or "save as"):

+ Parallel LINQ.
+ Parallel class and friends.
+ TaskManager, Task, Future and friends.

Me too links of today

Thu, December 6, 2007, 06:32 PM under Links
- How can a truly innocent blog post on Internet Explorer 8, attract over 100 comments!

- ScotGu states in a reply to his own post that the ASP.NET 3.5 Extensions Preview will be available tomorrow.

- PDC08 announced (hopefully it won't be rescheduled again). Can you believe that I've never been to one?!

- There are 5 (five!) other announcements made in the last 36 hours, but rather than list them here I'll include them in the upcoming Flash as usual. Please subscribe now.

Code snippet for property old style

Tue, December 4, 2007, 03:18 AM under Orcas | VisualStudio
When I first realised that the prop snippet now generates automatic properties in C# I thought it was cool. The more I write code in VS2008 though I find that when I need to write a property with get/setters that are explicitly implemented, it becomes tedious – I used to rely on prop in VS2005 too much I guess. This is easily rectified by adding a new code snippet of course.

Creating snippet is so easy it should be illegal :), but to save you the manual work here is the one I created for this purpose (propOld). Simply "save as" to your local drive and then from Visual Studio 2008 menu "Tools->Code Snippet Manager" click the "Import" button; then in a C# file type proOLD and hit TAB (instead of prop+TAB).

December UK Events

Sun, December 2, 2007, 03:48 AM under Events
Thank you to everyone that came to my September, October and November events. In total from 18 September to 29 November I delivered 21 sessions. December is always the quiet month from an in-person events perspective and especially for me since I'll be taking 1/3 of it off for holiday. That also means that (with January round the corner) I better spend time figuring out what the heck I am going to say/show for my upcoming Windows Server 2008 developer sessions (all ideas welcome!).

So, this is my short (useless) December list:
  • 6th, London, MSDN web event [sorry, this event is full inc. the waitlist]
  • 12th, Southampton, MS VS event [sorry, there is no public registration for this event]
We have just started a new scheme of online chats that you are welcome to join IF you attended one of our MSDN events in the UK this year.
FYI, for UK events (inc. ones that have nothing to do with me) monitor this developerfusion feed.