Do not call the EndDialog function from the hook procedure.
It happened that I also came across the HookProc implementation in FileDialog, using the .Net Reflector:
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) {
// ......
} catch {
if (this.dialogHWnd != IntPtr.Zero) {
UnsafeNativeMethods.EndDialog(new HandleRef(this, this.dialogHWnd), IntPtr.Zero);
}
throw;
}
// ......
}
May be the .net team know win32 in and out that outsider rules don't apply to them; or may be sometimes it is acceptable to ignore MSDN.