From 1b3b510376465136adba1e4e976762426be74260 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Thu, 9 Jan 2014 00:10:41 +0100 Subject: [PATCH] [Win] Check registry keys before accessing Fixes crashes when using OpenTK over the Remote Desktop Client for Mac (version 2010). --- Source/OpenTK/Platform/Windows/WinRawKeyboard.cs | 10 ++++++++-- Source/OpenTK/Platform/Windows/WinRawMouse.cs | 13 ++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs index 81f6e6e..84b17a1 100644 --- a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs +++ b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs @@ -103,10 +103,11 @@ namespace OpenTK.Platform.Windows // This is a keyboard or USB keyboard device. In the latter case, discover if it really is a // keyboard device by qeurying the registry. RegistryKey regkey = GetRegistryKey(name); - string deviceDesc = (string)regkey.GetValue("DeviceDesc"); + if (regkey == null) + continue; + string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class"); - string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); // for windows 8 support via OpenTK issue 3198 // making a guess at backwards compatability. Not sure what older windows returns in these cases... @@ -205,10 +206,15 @@ namespace OpenTK.Platform.Windows static RegistryKey GetRegistryKey(string name) { + if (name.Length < 4) + return null; + // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); + if (split.Length < 3) + return null; string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) diff --git a/Source/OpenTK/Platform/Windows/WinRawMouse.cs b/Source/OpenTK/Platform/Windows/WinRawMouse.cs index 727d3b6..15ae043 100644 --- a/Source/OpenTK/Platform/Windows/WinRawMouse.cs +++ b/Source/OpenTK/Platform/Windows/WinRawMouse.cs @@ -110,11 +110,13 @@ namespace OpenTK.Platform.Windows // This is a mouse or a USB mouse device. In the latter case, discover if it really is a // mouse device by qeurying the registry. RegistryKey regkey = FindRegistryKey(name); - string deviceDesc = (string)regkey.GetValue("DeviceDesc"); + if (regkey == null) + continue; - + string deviceDesc = (string)regkey.GetValue("DeviceDesc"); string deviceClass = (string)regkey.GetValue("Class") as string; - if(deviceClass == null){ + if(deviceClass == null) + { // Added to address OpenTK issue 3198 with mouse on Windows 8 string deviceClassGUID = (string)regkey.GetValue("ClassGUID"); RegistryKey classGUIDKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + deviceClassGUID); @@ -266,10 +268,15 @@ namespace OpenTK.Platform.Windows static RegistryKey FindRegistryKey(string name) { + if (name.Length < 4) + return null; + // remove the \??\ name = name.Substring(4); string[] split = name.Split('#'); + if (split.Length < 3) + return null; string id_01 = split[0]; // ACPI (Class code) string id_02 = split[1]; // PNP0303 (SubClass code) -- 2.7.4