From bbe8a32e587bd1f5050b0be6e98e9e1e216cf0fb Mon Sep 17 00:00:00 2001 From: Jean-Jacques Lafay Date: Tue, 6 Feb 2018 14:19:36 +0100 Subject: [PATCH] Do not throw from Win32Helper.GetFocusedWindowHandle() This function is called during initialization, from a background thread. An exception there will crash Excel if not handled. Note that there is at least one case where this happens : when Excel launches on a locked desktop (typically through a scheduled task). Since there are already some cases where this function will return IntPtr.Zero, it makes sense to default to this, rather than catch at the calling site. --- Source/ExcelDna.IntelliSense/Win32Helper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/ExcelDna.IntelliSense/Win32Helper.cs b/Source/ExcelDna.IntelliSense/Win32Helper.cs index a4cdc9d..4d2716a 100644 --- a/Source/ExcelDna.IntelliSense/Win32Helper.cs +++ b/Source/ExcelDna.IntelliSense/Win32Helper.cs @@ -133,7 +133,7 @@ public static IntPtr GetFocusedWindowHandle() var info = new GUITHREADINFO(); info.cbSize = Marshal.SizeOf(info); if (!GetGUIThreadInfo(0, ref info)) - throw new Win32Exception(); + return IntPtr.Zero; var focusedWindow = info.hwndFocus; if (focusedWindow == IntPtr.Zero) @@ -142,7 +142,7 @@ public static IntPtr GetFocusedWindowHandle() uint processId; uint threadId = GetWindowThreadProcessId(focusedWindow, out processId); if (threadId == 0) - throw new Win32Exception(); + return IntPtr.Zero; uint currentProcessId = GetCurrentProcessId(); if (processId == currentProcessId)