Home | Blog | Publications | Photos | Services | About | Contact

Blog Archive - December, 2006

Back to Blog index.

Sat, 30 Dec 2006 13:18:21 GMT:
Happy New Year 2007, and remember...

As the year 2006 is turning to an end, it is time to wish everybody Happy and Prosperous New Year 2007!

As in the previous years, the URL for my latest RSS feed is going to change. I'm sorry about the inconvenience, but my ISP doesn't allow databases or dynamic file modification. So, the new RSS feed URL for the next year will be:

http://www.saunalahti.fi/janij/blog/2007.xml

Thanks for reading!

Fri, 29 Dec 2006 10:40:11 GMT:
Changing the default method stub generated by Visual Studio 2005's Generate Method Stub refactoring to use NotImplementedException

You might have noticed while writing your C# code in Visual Studio 2005 that whenever you write a name of a method that doesn't yet exist in your class, Visual Studio suggest to generate a method stub for your when you press Shift+Alt+F10,M while your cursor (caret) is on the name of the (yet) non-existing method.

Now, the default-generated method uses the basic System.Exception class to throw an exception with the message "The method or operation is not implemented." because you haven't in fact yet written any code to the method's body. However, the .NET Base Class Library also contains a specific exception type for this purpose, named the System.NotImplementedException. This brings into mind an idea: would it be possible to change the default method stub generated by Visual Studio? Luckily, the answer is yes.

About a year ago, Microsoft's Anson Horton (the Program Manager for the Visual C# IDE) blogged about this, and showed how to edit the "MethodStubBody.snippet" file under the default directory "C:\Program Files\Microsoft Visual Studio 2005\VC#\Snippets\1033\Refactoring" so that the generated code will throw a NotImplementedException instead of a regular Exception.

Having made the change myself, I quickly noticed that it would be great if the error message itself would display the name of the method in question, because the default error message shown is simply "The method or operation is not implemented." even with NotImplementedException, and thus requires you to check the call stack (or use the debugger) to see which method was not implemented. However, if you tweak the MethodStubBody.snippet file just a little bit more, you can easily get this feature done. By using the special "$signature$" macro (for a lack of a better word), you can insert the name of the method to the error message. So in the end, I ended up editing the MethodStubBody.snippet file like Anson Horton described, but also by changing the actual code to:

<Code Language="csharp">
<![CDATA[$signature$
{
  $end$throw new $Exception$("The method or operation " +
  "is not implemented: $signature$");
}]]>
</Code>

This change has proved to be just perfect for my needs. Try it and see what you think. Oh by the way, this suggestion has already been made to the Visual Studio Connect site with the ID 92654.

Fri, 29 Dec 2006 05:38:52 GMT:
Microsoft Expression Web is now ready

Microsoft's creativity product suite called Expression is finally ready for first product launches. The company just recently announced the availability of Expression Web, the firstly-finished member of the suite. Expression Web is a hybrid between FrontPage and Visual Studio's web design features, and allows your to write documents and develop web applications using technologies such as XML, ASP.NET and XHTML. A free trial version is now also available for the product with a suggested retail price of USD $299. Personally, I'm waiting for the Expression Design product, which would allow me the write XAML/Windows Presentation Foundation (WPF) applications. Expression Design is currently available as a Community Technology Preview (CTP).

Wed, 27 Dec 2006 06:45:28 GMT:
Could your application look like Office 2007?

After a Christmas holiday, it's again time to get on track with some development stuff. I'm sure you are aware of the new Office 2007 products that were launched in November. Word, Excel and PowerPoint all have the new "2007 look", which is something many, many software vendors try to mimic in the coming years, just like it happened with Office 2003. However, previously ISVs had to do much of the work themselves, but with the 2007 Office System this is changing.

With the new Office version, Microsoft is offering to license the user interface (UI) to developers and ISVs. There's a site on MSDN devoted to this, and you can also download a document titled "2007 Microsoft Office System User Interface Licensing Program Frequently Asked Questions". The license is free to use, and does not contain any royalty fees. The license grants you access to "the intellectual property rights in the Office UI as well as Design Guidelines that describe the look and behavior of the UI" as specified by the FAQ document. No code is licensed, but the design guidelines should get you far enough on the path to Office 2007 like applications.

Thu, 21 Dec 2006 15:26:41 GMT:
About IE 7, ClearType and custom searches

By now, you've probably started using Internet Explorer 7 even if you are still using Windows XP and not Windows Vista. I've used IE 7 starting from the early Vista beta versions, and although I was a bit skeptical at first (I always liked IE 6), I quickly found tabbed browsing and the new search facility very useful.

Now, when I talk to people about IE 7, they often complain about the new interface (of course), but also about the font-smoothing ClearType technology, which is enabled by default. I believe it is enabled by default because many (most?) users already are using LCD screens instead of CRTs, but I could be wrong, too. Then again, I noticed that I don't personally enjoy ClearType on my 1600x1200 Nokia CRT screen, and so switched it off. However, on my laptop, I found the results to be better than without it.

Here's a tip if you are using LCD screens: download the free ClearType powertoy, which allows you to calibrate ClearType settings and thus improve the results. You can download it from Microsoft typography web pages.

My second tip is about adding custom search providers into IE 7's search box at top-right. Firstly, click the down-arrow button next to the search button, and choose Find More Providers. Notice the yellow box on the righthand side. Here, you can easily add a custom search engine to your IE, for example MSDN. This can be very useful.

A technical note about the custom search provides: these are specified using XML files (you guessed it) obeying the OpenSearch specification. For example, the custom search provider for MSDN would be:

<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
  <ShortName>MSDN</ShortName> 
  <Description>MSDN provider</Description> 
  <InputEncoding>UTF-8</InputEncoding> 
  <Url type="text/html"
  template="http://search.msdn.microsoft.com/search/
  default.aspx?siteId=0&tab=0&query={searchTerms}" /> 
</OpenSearchDescription>

Because adding custom search providres is so easy, I immediately added Wikipedia as well.

Mon, 18 Dec 2006 15:40:03 GMT:
Why can't I run a Vista executable on Windows XP?

Windows Vista contains many nice .EXE files, and it is tempting to copy those EXE files to your Windows XP box and to try run them there. Well, it turns out very quickly that this just won't work, because you will get a rather blunt error message in Windows XP saying:

C:\Tests\Calc.exe is not a valid Win32 application.

But why is this error message displayed? It is displayed by the OS application loader, and each PE file (Portable Executable) has a header which tells the OS the minimum OS version on which the application will run. In Vista, the version is 6.0, and on Windows XP 5.1, and so on. If the version stamped in the EXE file is larger than the current OS version, the application won't run.

Now then, how would you check an EXE file's minimum version? By parsing the PE header, or more precisely the IMAGE_OPTIONAL_HEADER header and its MajorOperatingSystemVersion and MinorOperatingSystemVersion fields. Here's a quick and dirty C# code to return these fields given an EXE filename:

private Version GetPEFileMinimumOSVersion(string filename)
{
  FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  const int BUFFER_SIZE = 100;
  const int WORD_SIZE = 2; // sizeof(short) on 32-bit
  byte[] peHeader = new byte[BUFFER_SIZE];
  try
  {
    // read IMAGE_DOS_HEADER
    const int DOS_HEADER_LFANEW = 0x3C;
    byte[] peOffsetRVA = new byte[WORD_SIZE];
    fs.Seek(DOS_HEADER_LFANEW, SeekOrigin.Begin);
    fs.Read(peOffsetRVA, 0, WORD_SIZE);
    int peOffset = (peOffsetRVA[0] + (peOffsetRVA[1] << 8));
    // read IMAGE_NT_HEADERS
    fs.Seek(peOffset, SeekOrigin.Begin);
    fs.Read(peHeader, 0, BUFFER_SIZE);
    string header = Encoding.ASCII.GetString(peHeader, 0, 2);
    if (header != "PE")
    {
      throw new Exception("The EXE file \"" + filename +
        "\" does not seem to be a valid " +
        "Win32 PE executable file.");
    }
  }
  finally
  {
    fs.Close();
  }
  // parse IMAGE_OPTIONAL_HEADER fields
  const int MAJOR_VERSION_OFFSET = 0x40;
  int pos = MAJOR_VERSION_OFFSET;
  int major = (peHeader[pos] + (peHeader[pos + 1] << 8));
  pos += WORD_SIZE;
  int minor = (peHeader[pos] + (peHeader[pos + 1] << 8));
  return new Version(major, minor);
}

private void button1_Click(object sender, EventArgs e)
{
  string filename = @"C:\Windows\System32\calc.exe";
  Version minimumVersion = GetPEFileMinimumOSVersion(filename);
  MessageBox.Show(minimumVersion.ToString());
}

I won't go through the gory details of the PE (and MS-DOS) file headers here, but I can instead point you to Matt Pietrek's two-part article on MSDN from 2002. Happy hacking!

Sat, 16 Dec 2006 09:02:19 GMT:
Videos and even more videos from TechEd 2006 on MSDN Showtime

This year's TechEd was held in November in Barcelona, Spain. Even if you weren't part of the 3700+ developers attending the event, not all is lost. The good news is that MSDN's Showtime has a compilation of videos from most (if not all) sessions that were held there. To see them, you can go to MSDN (link above) and search for the favorite product, technology or speaker, and then click the "Watch the entire show" link on the right to see a new popup window and your video. Cool, expect that I haven't found a way to see the videos without the popup (I want to see them in my Media Player and not in some tiny, non-resizeable popup).

In addition to Showtime, TechEd also has the "Virtual Side", which is available at MSEventsEurope.com. For your convenience, I've compiled a set of direct links to the videos.

Enjoy!

Fri, 15 Dec 2006 16:23:22 GMT:
C# tip: reading a shortcut's target

In the C# newsgroups somebody asked how to read the target of a Windows shortcut programmatically. That is, he wanted to get the name of the application/file a shortcut (.LNK file) points to. Here's the solution.

But first some background. Shortcuts (or "shell links" as they are also called) can be manipulated using the COM objects in SHELL32.DLL, and there exists the IShellLink interface and the ShellLinkObject object, the latter of which is very useful. If you are using Visual Studio, you can simply add a reference to the COM library "Microsoft Shell Control And Automation" or to SHELL32.DLL directly, and then use the following code:

public string GetShortcutTargetFile(string shortcutFilename)
{
  string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
  string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

  Shell32.Shell shell = new Shell32.ShellClass();
  Shell32.Folder folder = shell.NameSpace(pathOnly);
  Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
  if (folderItem != null)
  {
    Shell32.ShellLinkObject link = 
(Shell32.ShellLinkObject)folderItem.GetLink;
    return link.Path;
  }
  return ""; // not found
}

Then, just call the code like this:

private void button1_Click(object sender, EventArgs e)
{
  string shortcut = "C:\\Shortcut to notepad.exe.lnk";
  MessageBox.Show(GetShortcutTargetFile(shortcut));
}

One of the reasons I wanted to share this solution here also is that I found it difficult to find simple enough code to do the job. Most code I found defined the IPersistFile and IShellLink interfaces themselves, which I find somewhat ugly, especially all that is needed is a "quick and simple" solution like the original poster.

Fri, 15 Dec 2006 14:32:55 GMT:
Visual Studio 2005 SP1 now available

Microsoft has earlier today announced the availability of Visual Studio 2005 (VS2005) Service Pack 1 (SP1). The update can be freely downloaded from Microsoft's web pages. There are over 70 fixes and improvements in the service pack, so it is worth downloading.

Fri, 15 Dec 2006 06:02:35 GMT:
Starting Active Directory management tools from the command-line

I needed to manage a Windows Server 2003 member server, but the Active Directory management shortcut icons were missing from the Start menu and Administrative Tools group. If you run into a similar situation, it is helpful to know that you can still start the tools you need (such as Active Directory Users and Computers) from the command-line. These tools are Microsoft Management Console (MMC) snap-ins, and thus .MSC files (which are XML files in Windows Server 2003, by the way).

Here's a list of the three Active Directory tools you often need:

You can easily launch these tools from the Start/Run command, or from the command-line. Here's another tip for you: if you want to quickly locate where a program is in the current Path, use the Where.exe command. In the command-line, simply type:

where dsa.msc

This returns a simple line, such as:

C:\WINDOWS\system32\dsa.msc

The Where command also supports wildcards.

Wed, 13 Dec 2006 15:13:01 GMT:
Hotfixes for Visual Studio now publicly available

Although Visual Studio 2005 is very usable out of the box, it isn't perfect. Previously, service packs became available seldomly, and to get fixes in between, you had to contact Microsoft's Professional Services (PS) and tell them about you issue.

Now, Microsoft's Developer Division has started a new program, called the "Hotfix Public Availability Pilot Program", which means that from now on (or, at least during the pilot), you can download the current hotfixes and use them at your own risk. It is important to note that the fixes are not fully tested, so mishaps can happen. Nonetheless, I think it is good to have the fixes available to all developers.

Tue, 12 Dec 2006 17:31:10 GMT:
Windows Photo Gallery database backups, how to do it

Windows Vista comes with a neat and slick little application called the Windows Photo Gallery, which is an utility aimed for showing and organizing your digital photos based on EXIF metadata and keyword or tags.

Now, I have about 10,000 photos only with my year-old DSLR (which isn't a lot, even) and organizing them can be difficult. I'm thinking of using Vista's Photo Gallery (WPG) for this task, but them again I'm always worried about backups. So I did a little investigation, and found our that WPG stores its settings in the following path:

C:\Users\Jani Järvinen\AppData\Local\
Microsoft\Windows Photo Gallery

This directory contains two files: NavTree.state and Pictures.pd4. These files are both binary, and I don't know what kind of database files they are, but it seems that if you take backups of these files (and of course of your photos!) *and* the locations of the photos, you should be fine. Disclaimer: I haven't yet tested if this really works as I expect it to work.

Mon, 11 Dec 2006 15:46:26 GMT:
Use Visual Studio's Code Definition Window to your advantage

When I'm developing C# code with Visual Studio 2005, I find the "Go To Definition" command very handy. By right clicking a symbol in the code editor, and then choosing "Go To Definition" from the popup menu, I can see the source code file (and line) where the symbol is defined:

Or, in the case of a .NET framework symbol, such as the EventArgs class, Visual Studio displays a read-only representation of the original source code, generated from metadata:

This command is useful, but there's more especially if you find yourself using the Go To Definition command often. By selecting the View/Code Definition Window command, you can open up a window, which updates it's contents while you move your editor cursor or navigate your code. A very useful window indeed.

Sat, 09 Dec 2006 13:44:23 GMT:
Elevating EXEs in Windows Vista with UAC on

Windows Vista gives you two options for elevation: you can either start another process (EXE) with elevated rights ("admin rights") or you can invoke a COM object with a so-called elevation moniker. Now, sometimes you just want to launch a process elevated, the same way as you can right-click for instance a shortcut on Vista, and select "Run as Administrator" from the popup menu. But how would you do the same programmatically?

The answer is the familiar ShellExecute API function, which supports certain verbs, such as "open", "print" and "find". With Vista, it also supports a new verb called "runas", which is precisely what you need. If you use the "runas" verb, the screen will go blank, and the user is prompted for his or her concent for the operation. You cannot programmatically override or skip this step, it is how Vista works. Similarly, you cannot use CreateProcess API for this task. Here's an example code with C#:

using System.Runtime.InteropServices;
...
public const int SW_SHOWNORMAL = 1;     
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
  IntPtr hwnd,
  string lpOperation,
  string lpFile,
  string lpParameters,
  string lpDirectory,
  int nShowCmd);
...
private void launchNotepadButton_Click(object sender, EventArgs e)
{
  ShellExecute(this.Handle, "runas", "notepad.exe", "", "", SW_SHOWNORMAL);
}

In this example, Notepad is launched, but of course the process could be any process you lile. Note also that the MSDN documentation of ShellExecute is not yet up to date, but will soon be.

Fri, 08 Dec 2006 04:55:48 GMT:
Office Open XML file format now an ECMA standard

This is a good day to all Office users: Microsoft's de-facto standard for business document creation has now an ECMA standard for the company's latest Office 2007 XML based file formats. I've heard that only IBM voted against the approval, but somehow I wasn't surprised. An ISO standard is also in the works.

As for us developers, this means that it will be easier from now on to sell solutions that use Word documents and Excel spreadsheets as a document format/store. And for users, probably we can now stop converting those documents to PDF (or PDF/A) because we were afraid that those documents couldn't be opened in 25 years. The sad thing of course is that you will need to get Office 2007, but after five to ten years from now, probably most everybody (in business, at least) is using the new XML based file formats. Congrats, Office team!

Tue, 05 Dec 2006 14:59:55 GMT:
Delphi IDE keyboard shortcut tips

Delphi 2006 (or, Borland Developer Studio a.k.a. BDS) has tons of useful keyboard shortcuts, but not everybody master them all. BDN has published a list of useful, but less know keyboard shortcuts. Of these, I found Ctrl+Alt+F12 a new one to me. Which ones are new to you?

Sun, 03 Dec 2006 15:21:20 GMT:
An under-the-hood view to x64 development

When I two days ago reported about a rather light Intel FAQ, today I had the time to enjoy Matt Pietrek's x64 development article on MSDN Magazine. The article is originally from May, but somehow this has slipped me until now.

Even though you are a C# managed code developer, this article is worth some study. It is always a pleasure to read Matt's texts.

Fri, 01 Dec 2006 15:07:06 GMT:
A light but useful virtualization FAQ from Intel

I noticed that Intel had posted a five-page FAQ about virtualization. The questions and answers are quite light from technical perspective, but nonetheless I found the FAQ worth browsing through. Maybe you do, too.

 

› Blog Archive