[Mono-bugs] [Bug 77212][Cri] New - Exception when using RegistryKey under Windows

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Wed Jan 11 12:23:40 EST 2006

Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by don at edvalson.net.


--- shadow/77212	2006-01-11 12:23:40.000000000 -0500
+++ shadow/77212.tmp.17659	2006-01-11 12:23:40.000000000 -0500
@@ -0,0 +1,82 @@
+Bug#: 77212
+Product: Mono: Class Libraries
+Version: 1.1
+OS Details: Windows XP SP2
+Status: NEW   
+Priority: Critical
+Component: System
+AssignedTo: mono-bugs at ximian.com                            
+ReportedBy: don at edvalson.net               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+Summary: Exception when using RegistryKey under Windows
+Description of Problem:
+Exception when using RegistryKey.OpenSubKey
+Steps to reproduce the problem:
+Create a simple test program including the following line of c# code.
+private const string SOFTWARE_KEY = "Software";
+private const string COMPANY_NAME = "GamesForLoving";
+RegistryKey rkCompany;
+rkCompany = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY,false).OpenSubKey
+(COMPANY_NAME, false);
+Actual Results:
+Invalid Cast Exception
+Expected Results:
+For it to work.
+How often does this happen? always
+Additional Information:
+I traced this down into the source code and fixed it. I do not have check 
+in privileges, but I will describe what I did. If you want, let me know 
+and I can send in the fixed source files.
+The basic problem lies in the difference between root keys and subkeys. 
+Both of them have something in the Data member, but in one case it is an 
+IntPtr and in the other case it is an object. Thus, you need to cast it 
+differently depending on if the key is a root key or a subkey.
+I added the following method to the class RegistryKey
+		/// <summary>
+		///	Get the Handle (Data) as an IntPtr safely.
+		/// </summary>
+		public IntPtr Handle {
+			get {
+				if (isRoot)
+					return new IntPtr ((int)Data);
+				else
+					return (IntPtr) Data;
+			}
+		}
+Then in Win32RegistryApi.cs, I replaced all instances of either
+ IntPtr handle = (IntPtr)rkey.Data; // this cast works only with a subkey
+IntPtr handle = new IntPtr ((int) rkey.Data); // this cast works only 
+with a root key
+            IntPtr handle = rkey.Handle;
+This caused the correct cast to be executed at all times
+When I remade mono, this worked properly.

More information about the mono-bugs mailing list