Skip to content

Commit bbe8a32

Browse files
authored
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.
1 parent 568e79f commit bbe8a32

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Source/ExcelDna.IntelliSense/Win32Helper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static IntPtr GetFocusedWindowHandle()
133133
var info = new GUITHREADINFO();
134134
info.cbSize = Marshal.SizeOf(info);
135135
if (!GetGUIThreadInfo(0, ref info))
136-
throw new Win32Exception();
136+
return IntPtr.Zero;
137137

138138
var focusedWindow = info.hwndFocus;
139139
if (focusedWindow == IntPtr.Zero)
@@ -142,7 +142,7 @@ public static IntPtr GetFocusedWindowHandle()
142142
uint processId;
143143
uint threadId = GetWindowThreadProcessId(focusedWindow, out processId);
144144
if (threadId == 0)
145-
throw new Win32Exception();
145+
return IntPtr.Zero;
146146

147147
uint currentProcessId = GetCurrentProcessId();
148148
if (processId == currentProcessId)

0 commit comments

Comments
 (0)