2014-04-30

Help.ShowHelp Fails Silently

I was trying the .NET Framework command to show help, System.Windows.Forms.Help.ShowHelp(), and passed a file name of a file that didn't exist. I was expecting it to throw an exception, except nothing happened. It just failed silently. Looking at the .NET Framework sources, the code just does:

SafeNativeMethod.HtmlHelp(...);

where HtmlHelp is defined as

[DllImport("hhctrl.ocx", CharSet=CharSet.Auto)]
public static extern int HtmlHelp(HandleRef hwndCaller,
 [MarshalAs(UnmanagedType.LPTStr)] string pszFile, int uCommand,
 [MarshalAs(UnmanagedType.LPStruct)] NativeMethods.HH_FTS_QUERY dwData);

You'll notice that it does nothing with the int return value. To get around this failure on Microsoft's part, you can do one of two things. The cheap technique is to check for the existence of the file using System.IO.File.Exists() before making the call to Help.ShowHelp(). However, this doesn't account for other problems with the help file such as permission problems or file corruption. The other technique is to call the native method yourself, using the above PInvoke declaration. You can then check the return value. The return value should be the handle of the help window when it is created. It should return zero for failure.

Microsoft really needs to provide a different call to bring up help that throws exceptions on failure. This call is in the Windows Forms library anyway, which really shouldn't be called from WPF.

No comments :

Post a Comment