dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/Microsoft/Win32 IRegistryKeyP


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/Microsoft/Win32 IRegistryKeyProvider.cs,1.1,1.2 MemoryKeyProvider.cs,1.1,1.2 Registry.cs,1.1,1.2 RegistryKey.cs,1.1,1.2Win32KeyProvider.cs,1.1,1.2
Date: Thu, 03 Apr 2003 21:50:06 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32
In directory subversions:/tmp/cvs-serv8444/runtime/Microsoft/Win32

Modified Files:
        IRegistryKeyProvider.cs MemoryKeyProvider.cs Registry.cs 
        RegistryKey.cs Win32KeyProvider.cs 
Log Message:


Implement the Win32 version of the registry routines.


Index: IRegistryKeyProvider.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32/IRegistryKeyProvider.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** IRegistryKeyProvider.cs     3 Apr 2003 13:37:32 -0000       1.1
--- IRegistryKeyProvider.cs     4 Apr 2003 02:50:00 -0000       1.2
***************
*** 39,44 ****
--- 39,54 ----
        IRegistryKeyProvider CreateSubKey(String subkey);
  
+       // Returns true if we should delete subkeys from their parents.
+       bool DeleteFromParents { get; }
+ 
+       // Delete a subkey of this key entry.  Returns false if not present.
+       bool DeleteSubKey(String name);
+ 
        // Delete this key entry.
        void Delete();
+ 
+       // Delete a subkey entry and all of its descendents.
+       // Returns false if not present.
+       bool DeleteSubKeyTree(String name);
  
        // Delete this key entry and all of its descendents.

Index: MemoryKeyProvider.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32/MemoryKeyProvider.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** MemoryKeyProvider.cs        3 Apr 2003 13:37:32 -0000       1.1
--- MemoryKeyProvider.cs        4 Apr 2003 02:50:00 -0000       1.2
***************
*** 86,89 ****
--- 86,106 ----
                        }
  
+       // Returns true if we should delete subkeys from their parents.
+       public bool DeleteFromParents
+                       {
+                               get
+                               {
+                                       // Use "Delete" and "DeleteTree".
+                                       return false;
+                               }
+                       }
+ 
+       // Delete a subkey of this key entry.  Returns false if not present.
+       public bool DeleteSubKey(String name)
+                       {
+                               // Not used for memory-based registries.
+                               return false;
+                       }
+ 
        // Delete this key entry.
        public void Delete()
***************
*** 129,132 ****
--- 146,157 ----
                                        }
                                }
+                       }
+ 
+       // Delete a subkey entry and all of its descendents.
+       // Returns false if not present.
+       public bool DeleteSubKeyTree(String name)
+                       {
+                               // Not used for memory-based registries.
+                               return false;
                        }
  

Index: Registry.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32/Registry.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** Registry.cs 3 Apr 2003 13:37:32 -0000       1.1
--- Registry.cs 4 Apr 2003 02:50:00 -0000       1.2
***************
*** 87,91 ****
                                        if(Win32KeyProvider.IsWin32())
                                        {
!                                               providers[index] = new 
Win32KeyProvider(name);
                                                return providers[index];
                                        }
--- 87,92 ----
                                        if(Win32KeyProvider.IsWin32())
                                        {
!                                               providers[index] = new 
Win32KeyProvider
!                                                       (name, 
Win32KeyProvider.HiveToHKey(hKey));
                                                return providers[index];
                                        }

Index: RegistryKey.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32/RegistryKey.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** RegistryKey.cs      3 Apr 2003 13:37:32 -0000       1.1
--- RegistryKey.cs      4 Apr 2003 02:50:00 -0000       1.2
***************
*** 84,87 ****
--- 84,89 ----
                                String temp;
                                IRegistryKeyProvider key;
+                               RegistryKey savedStart = start;
+                               RegistryKey prevStart;
  
                                while((index = subkey.IndexOf('\\')) != -1 ||
***************
*** 99,102 ****
--- 101,105 ----
  
                                        // Create or open a new component.
+                                       prevStart = start;
                                        if(create)
                                        {
***************
*** 117,120 ****
--- 120,129 ----
                                                start = new RegistryKey(key, 
false);
                                        }
+                                       if(prevStart != savedStart)
+                                       {
+                                               // Intermediate registry key 
that we won't be needing
+                                               // any more, so clean up its 
resources.
+                                               prevStart.Close();
+                                       }
                                }
                                last = subkey;
***************
*** 172,175 ****
--- 181,195 ----
                                }
  
+                               // Take a shortcut if we need to "delete from 
parents".
+                               if(provider.DeleteFromParents)
+                               {
+                                       if(!provider.DeleteSubKey(subkey) && 
throwOnMissingSubKey)
+                                       {
+                                               throw new ArgumentException
+                                                       
(_("IO_RegistryKeyNotExist"));
+                                       }
+                                       return;
+                               }
+ 
                                // Resolve the subkey to a parent and last 
component.
                                String last;
***************
*** 219,222 ****
--- 239,253 ----
                                }
  
+                               // Take a shortcut if we need to "delete from 
parents".
+                               if(provider.DeleteFromParents)
+                               {
+                                       if(!provider.DeleteSubKeyTree(subkey))
+                                       {
+                                               throw new ArgumentException
+                                                       
(_("IO_RegistryKeyNotExist"));
+                                       }
+                                       return;
+                               }
+ 
                                // Resolve the subkey to a parent and last 
component.
                                String last;
***************
*** 342,357 ****
                                        throw new 
ArgumentNullException("machineName");
                                }
-                               else if(machineName != String.Empty)
-                               {
-                                       // Accessing remote registries is a big 
security risk.
-                                       // Always deny the request.
-                                       throw new 
SecurityException(_("Invalid_RemoteRegistry"));
-                               }
  
!                               // Open the local hive.
                                String name = hiveNames
                                        [((int)hKey) - 
(int)(RegistryHive.ClassesRoot)];
                                return new RegistryKey
!                                       (Registry.GetProvider(hKey, name), 
false);
                        }
  
--- 373,410 ----
                                        throw new 
ArgumentNullException("machineName");
                                }
  
!                               // Get the name of the hive to be accessed.
                                String name = hiveNames
                                        [((int)hKey) - 
(int)(RegistryHive.ClassesRoot)];
+ 
+                               // Is this a remote hive reference?
+                               if(machineName != String.Empty)
+                               {
+                                       if(Win32KeyProvider.IsWin32())
+                                       {
+                                               // Attempt to connect to the 
remote registry.
+                                               IntPtr newKey;
+                                               
if(Win32KeyProvider.RegConnectRegistry
+                                                               (machineName,
+                                                                
Win32KeyProvider.HiveToHKey(hKey),
+                                                                out newKey) != 
0)
+                                               {
+                                                       throw new 
SecurityException
+                                                               
(_("Invalid_RemoteRegistry"));
+                                               }
+                                               return new RegistryKey
+                                                       (new 
Win32KeyProvider(name, newKey), true);
+                                       }
+                                       else
+                                       {
+                                               // Not Win32 - cannot access 
remote registries.
+                                               throw new SecurityException
+                                                       
(_("Invalid_RemoteRegistry"));
+                                       }
+                               }
+ 
+                               // Open a local hive.
                                return new RegistryKey
!                                       (Registry.GetProvider(hKey, name), 
true);
                        }
  

Index: Win32KeyProvider.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/Microsoft/Win32/Win32KeyProvider.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** Win32KeyProvider.cs 3 Apr 2003 13:37:32 -0000       1.1
--- Win32KeyProvider.cs 4 Apr 2003 02:50:00 -0000       1.2
***************
*** 26,29 ****
--- 26,32 ----
  
  using System;
+ using System.Text;
+ using System.Collections;
+ using System.Runtime.InteropServices;
  
  // This class implements registry functionality for Win32 systems.
***************
*** 33,41 ****
        // Internal state.
        private String name;
  
!       // Constructor.
!       public Win32KeyProvider(String name)
                        {
                                this.name = name;
                        }
  
--- 36,52 ----
        // Internal state.
        private String name;
+       private IntPtr hKey;
  
!       // Constructors.
!       public Win32KeyProvider(String name, IntPtr hKey)
                        {
                                this.name = name;
+                               this.hKey = hKey;
+                       }
+ 
+       // Destructor.
+       ~Win32KeyProvider()
+                       {
+                               Close(false);
                        }
  
***************
*** 43,48 ****
        public static bool IsWin32()
                        {
!                               // TODO
!                               return false;
                        }
  
--- 54,78 ----
        public static bool IsWin32()
                        {
!                               try
!                               {
!                                       // We query the information about the 
LocalMachine
!                                       // hive to determine if the Win32 
registry routines
!                                       // are present or not.  The runtime 
engine will
!                                       // throw a "MissingMethodException" if 
it was unable
!                                       // to resolve the function (i.e. on 
non-Win32 platforms),
!                                       // or if "winapi" calling conventions 
are not supported.
!                                       uint numSubKeys, numValues;
!                                       
RegQueryInfoKey(HiveToHKey(RegistryHive.LocalMachine),
!                                                                       null, 
IntPtr.Zero, IntPtr.Zero,
!                                                                       out 
numSubKeys, IntPtr.Zero,
!                                                                       
IntPtr.Zero, out numValues,
!                                                                       
IntPtr.Zero, IntPtr.Zero,
!                                                                       
IntPtr.Zero, IntPtr.Zero);
!                                       return true;
!                               }
!                               catch(MissingMethodException)
!                               {
!                                       return false;
!                               }
                        }
  
***************
*** 50,54 ****
        public void Close(bool writable)
                        {
!                               // TODO
                        }
  
--- 80,96 ----
        public void Close(bool writable)
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               long key = hKey.ToInt64();
!                                               if(key < 
(long)(int)(RegistryHive.ClassesRoot) ||
!                                                  key > 
(long)(int)(RegistryHive.DynData))
!                                               {
!                                                       RegCloseKey(hKey);
!                                               }
!                                               hKey = IntPtr.Zero;
!                                       }
!                               }
                        }
  
***************
*** 56,61 ****
        public IRegistryKeyProvider CreateSubKey(String subkey)
                        {
!                               // TODO
!                               return null;
                        }
  
--- 98,155 ----
        public IRegistryKeyProvider CreateSubKey(String subkey)
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               IntPtr newKey;
!                                               if(RegCreateKey(hKey, subkey, 
out newKey) == 0)
!                                               {
!                                                       return new 
Win32KeyProvider
!                                                               (name + "\\" + 
subkey, newKey);
!                                               }
!                                       }
!                               }
!                               throw new 
ArgumentException(_("IO_RegistryKeyNotExist"));
!                       }
! 
!       // Returns true if we should delete subkeys from their parents.
!       public bool DeleteFromParents
!                       {
!                               get
!                               {
!                                       // Use "DeleteSubKey" and 
"DeleteSubKeyTree".
!                                       return true;
!                               }
!                       }
! 
!       // Delete a subkey of this key entry.  Returns false if not present.
!       public bool DeleteSubKey(String name)
!                       {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               // Make sure that we don't have 
any subkeys.
!                                               // This is to work around an 
API discontinuity:
!                                               // Win95 deletes subtrees, but 
WinNT doesn't.
!                                               uint numSubKeys, numValues;
!                                               RegQueryInfoKey(hKey, null,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
out numSubKeys, IntPtr.Zero,
!                                                                               
IntPtr.Zero, out numValues,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
IntPtr.Zero, IntPtr.Zero);
!                                               if(numSubKeys != 0)
!                                               {
!                                                       throw new 
InvalidOperationException
!                                                               
(_("IO_RegistryHasSubKeys"));
!                                               }
!                                               return (RegDeleteKey(hKey, 
name) == 0);
!                                       }
!                                       else
!                                       {
!                                               return false;
!                                       }
!                               }
                        }
  
***************
*** 63,67 ****
        public void Delete()
                        {
!                               // TODO
                        }
  
--- 157,229 ----
        public void Delete()
                        {
!                               // This version is not used under Win32.
!                       }
! 
!       // Internal recursive version of "DeleteSubKeyTree".
!       private static bool DeleteSubKeyTree(IntPtr hKey, String name)
!                       {
!                               IntPtr subTree;
! 
!                               // Open the subkey tree.
!                               if(RegOpenKeyEx(hKey, name, 0, KEY_ALL_ACCESS,
!                                                               out subTree) != 
0)
!                               {
!                                       return false;
!                               }
! 
!                               // Collect up the names of all subkeys under 
"subTree".
!                               uint numSubKeys, numValues;
!                               RegQueryInfoKey(subTree, null,
!                                                               IntPtr.Zero, 
IntPtr.Zero,
!                                                               out numSubKeys, 
IntPtr.Zero,
!                                                               IntPtr.Zero, 
out numValues,
!                                                               IntPtr.Zero, 
IntPtr.Zero,
!                                                               IntPtr.Zero, 
IntPtr.Zero);
!                               String[] names = new String [numSubKeys];
!                               uint index = 0;
!                               char[] nameBuf = new char [1024];
!                               uint nameLen;
!                               long writeTime;
!                               while(index < numSubKeys)
!                               {
!                                       nameBuf.Initialize();
!                                       nameLen = (uint)(name.Length);
!                                       if(RegEnumKeyEx(subTree, index, 
nameBuf, ref nameLen,
!                                                                       
IntPtr.Zero, IntPtr.Zero,
!                                                                       
IntPtr.Zero, out writeTime) != 0)
!                                       {
!                                               break;
!                                       }
!                                       names[(int)(index++)] = 
ArrayToString(nameBuf);
!                               }
! 
!                               // Recursively delete the subtrees.
!                               foreach(String n in names)
!                               {
!                                       DeleteSubKeyTree(subTree, n);
!                               }
! 
!                               // Close the subtree.
!                               RegCloseKey(subTree);
! 
!                               // Delete the subkey that corresponds to the 
subtree.
!                               return (RegDeleteKey(hKey, name) == 0);
!                       }
! 
!       // Delete a subkey entry and all of its descendents.
!       // Returns false if not present.
!       public bool DeleteSubKeyTree(String name)
!                       {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               return DeleteSubKeyTree(hKey, 
name);
!                                       }
!                                       else
!                                       {
!                                               return false;
!                                       }
!                               }
                        }
  
***************
*** 69,73 ****
        public void DeleteTree()
                        {
!                               // TODO
                        }
  
--- 231,235 ----
        public void DeleteTree()
                        {
!                               // This version is not used under Win32.
                        }
  
***************
*** 76,81 ****
        public bool DeleteValue(String name)
                        {
!                               // TODO
!                               return false;
                        }
  
--- 238,252 ----
        public bool DeleteValue(String name)
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               if(RegDeleteValue(hKey, name) 
== 0)
!                                               {
!                                                       return true;
!                                               }
!                                       }
!                                       return false;
!                               }
                        }
  
***************
*** 83,87 ****
        public void Flush()
                        {
!                               // TODO
                        }
  
--- 254,385 ----
        public void Flush()
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               RegFlushKey(hKey);
!                                       }
!                               }
!                       }
! 
!       // Convert a null-terminated "char[]" array into a "String".
!       private static String ArrayToString(char[] array)
!                       {
!                               int index = 0;
!                               while(index < array.Length && array[index] != 
'\0')
!                               {
!                                       ++index;
!                               }
!                               return new String(array, 0, index);
!                       }
! 
!       // Convert a null-terminated "byte[]" array into a "String",
!       // where the array contains 16-bit character values.
!       private static String ArrayToString(byte[] array, ref int index)
!                       {
!                               int len = 0;
!                               while((index + len + 1) < array.Length &&
!                                     (array[index + len] != 0 ||
!                                          array[index + len + 1] != 0))
!                               {
!                                       len += 2;
!                               }
!                               StringBuilder builder = new StringBuilder(len / 
2);
!                               len = 0;
!                               while((index + len + 1) < array.Length &&
!                                     (array[index + len] != 0 ||
!                                          array[index + len + 1] != 0))
!                               {
!                                       builder.Append((char)(((int)array[index 
+ len]) |
!                                                                               
  (((int)array[index + len + 1])
!                                                                               
                << 8)));
!                                       len += 2;
!                               }
!                               index += len + 2;
!                               return builder.ToString();
!                       }
!       private static String ArrayToString(byte[] array)
!                       {
!                               int index = 0;
!                               return ArrayToString(array, ref index);
!                       }
! 
!       // Convert a "byte[]" array into an array of "String" values.
!       private static String[] ArrayToStringArray(byte[] array)
!                       {
!                               ArrayList list = new ArrayList();
!                               String value;
!                               int index = 0;
!                               for(;;)
!                               {
!                                       value = ArrayToString(array, ref index);
!                                       if(value.Length == 0)
!                                       {
!                                               break;
!                                       }
!                                       list.Add(value);
!                               }
!                               return (String[])(list.ToArray(typeof(String)));
!                       }
! 
!       // Convert a "String" into a "byte[]" array.
!       private static byte[] StringToArray(String value)
!                       {
!                               byte[] data = new byte [value.Length * 2];
!                               int index;
!                               for(index = 0; index < value.Length; ++index)
!                               {
!                                       data[index * 2] = (byte)(value[index]);
!                                       data[index * 2 + 1] = 
(byte)(value[index] >> 8);
!                               }
!                               return data;
!                       }
! 
!       // Convert a "String[]" array into a "byte[]" array.
!       private static byte[] StringArrayToArray(String[] value)
!                       {
!                               // Determine the total length of the "byte[]" 
array.
!                               int len = 0;
!                               int index, posn;
!                               String str;
!                               for(index = 0; index < value.Length; ++index)
!                               {
!                                       str = value[index];
!                                       if(str != null)
!                                       {
!                                               len += str.Length * 2 + 2;
!                                       }
!                                       else
!                                       {
!                                               len += 2;
!                                       }
!                               }
!                               len += 2;
! 
!                               // Create the "byte[]" array.
!                               byte[] data = new byte [len];
! 
!                               // Write the strings into the "byte[]" array.
!                               len = 0;
!                               for(index = 0; index < value.Length; ++index)
!                               {
!                                       str = value[index];
!                                       if(str != null)
!                                       {
!                                               for(posn = 0; posn < 
str.Length; ++posn)
!                                               {
!                                                       data[len++] = 
(byte)(str[posn]);
!                                                       data[len++] = 
(byte)(str[posn] >> 8);
!                                               }
!                                               len += 2;
!                                       }
!                                       else
!                                       {
!                                               len += 2;
!                                       }
!                               }
! 
!                               // Return the final encoded array to the caller.
!                               return data;
                        }
  
***************
*** 89,94 ****
        public String[] GetSubKeyNames()
                        {
!                               // TODO
!                               return null;
                        }
  
--- 387,429 ----
        public String[] GetSubKeyNames()
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               // Get the number of subkey 
names under the key.
!                                               uint numSubKeys, numValues;
!                                               RegQueryInfoKey(hKey, null,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
out numSubKeys, IntPtr.Zero,
!                                                                               
IntPtr.Zero, out numValues,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
IntPtr.Zero, IntPtr.Zero);
! 
!                                               // Create an array to hold the 
names.
!                                               String[] names = new String 
[numSubKeys];
! 
!                                               // Enumerate the names into the 
array.
!                                               uint index = 0;
!                                               char[] name = new char [1024];
!                                               uint nameLen;
!                                               long writeTime;
!                                               while(index < numSubKeys)
!                                               {
!                                                       name.Initialize();
!                                                       nameLen = 
(uint)(name.Length);
!                                                       if(RegEnumKeyEx(hKey, 
index, name, ref nameLen,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, out writeTime) != 0)
!                                                       {
!                                                               break;
!                                                       }
!                                                       names[(int)(index++)] = 
ArrayToString(name);
!                                               }
! 
!                                               // Return the final name array 
to the caller.
!                                               return names;
!                                       }
!                                       return new String [0];
!                               }
                        }
  
***************
*** 96,100 ****
        public Object GetValue(String name, Object defaultValue)
                        {
!                               // TODO
                                return defaultValue;
                        }
--- 431,531 ----
        public Object GetValue(String name, Object defaultValue)
                        {
!                               uint type = REG_NONE;
!                               byte[] data = null;
!                               uint dataLen;
! 
!                               // Query the value from the registry.
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               // Determine how big the buffer 
needs to be first.
!                                               dataLen = 0;
!                                               if(RegQueryValueEx(hKey, name, 
IntPtr.Zero,
!                                                                               
   out type, IntPtr.Zero, ref dataLen)
!                                                               != 0)
!                                               {
!                                                       return defaultValue;
!                                               }
! 
!                                               // Allocate a buffer to hold 
the data.
!                                               data = new byte [dataLen];
! 
!                                               // Now query the actual value.
!                                               if(RegQueryValueEx(hKey, name, 
IntPtr.Zero,
!                                                                               
   out type, data, ref dataLen)
!                                                               != 0)
!                                               {
!                                                       return defaultValue;
!                                               }
!                                       }
!                                       else
!                                       {
!                                               return defaultValue;
!                                       }
!                               }
! 
!                               // Decode the value into something that we can 
return.
!                               switch(type)
!                               {
!                                       case REG_BINARY:                return 
data;
! 
!                                       case REG_DWORD_LITTLE_ENDIAN:
!                                       {
!                                               if(data.Length == 4)
!                                               {
!                                                       return ((int)(data[0])) 
|
!                                                                  
(((int)(data[1])) << 8) |
!                                                                  
(((int)(data[2])) << 16) |
!                                                                  
(((int)(data[3])) << 24);
!                                               }
!                                       }
!                                       break;
! 
!                                       case REG_DWORD_BIG_ENDIAN:
!                                       {
!                                               if(data.Length == 4)
!                                               {
!                                                       return ((int)(data[3])) 
|
!                                                                  
(((int)(data[2])) << 8) |
!                                                                  
(((int)(data[1])) << 16) |
!                                                                  
(((int)(data[0])) << 24);
!                                               }
!                                       }
!                                       break;
! 
!                                       case REG_SZ:
!                                       case REG_EXPAND_SZ:
!                                       {
!                                               return ArrayToString(data);
!                                       }
!                                       // Not reached.
! 
!                                       case REG_MULTI_SZ:
!                                       {
!                                               return ArrayToStringArray(data);
!                                       }
!                                       // Not reached.
! 
!                                       case REG_QWORD_LITTLE_ENDIAN:
!                                       {
!                                               if(data.Length == 8)
!                                               {
!                                                       return 
((long)(data[0])) |
!                                                                  
(((long)(data[1])) << 8) |
!                                                                  
(((long)(data[2])) << 16) |
!                                                                  
(((long)(data[3])) << 24) |
!                                                                  
(((long)(data[4])) << 32) |
!                                                                  
(((long)(data[5])) << 40) |
!                                                                  
(((long)(data[6])) << 48) |
!                                                                  
(((long)(data[7])) << 56);
!                                               }
!                                       }
!                                       break;
! 
!                                       default: break;
!                               }
! 
!                               // If we get here, then we don't know how to 
decode the data.
                                return defaultValue;
                        }
***************
*** 103,108 ****
        public String[] GetValueNames()
                        {
!                               // TODO
!                               return null;
                        }
  
--- 534,575 ----
        public String[] GetValueNames()
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               // Get the number of value 
names under the key.
!                                               uint numSubKeys, numValues;
!                                               RegQueryInfoKey(hKey, null,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
out numSubKeys, IntPtr.Zero,
!                                                                               
IntPtr.Zero, out numValues,
!                                                                               
IntPtr.Zero, IntPtr.Zero,
!                                                                               
IntPtr.Zero, IntPtr.Zero);
! 
!                                               // Create an array to hold the 
names.
!                                               String[] names = new String 
[numValues];
! 
!                                               // Enumerate the names into the 
array.
!                                               uint index = 0;
!                                               char[] name = new char [1024];
!                                               uint nameLen;
!                                               while(index < numValues)
!                                               {
!                                                       name.Initialize();
!                                                       nameLen = 
(uint)(name.Length);
!                                                       if(RegEnumValue(hKey, 
index, name, ref nameLen,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, IntPtr.Zero) != 0)
!                                                       {
!                                                               break;
!                                                       }
!                                                       names[(int)(index++)] = 
ArrayToString(name);
!                                               }
! 
!                                               // Return the final name array 
to the caller.
!                                               return names;
!                                       }
!                                       return new String [0];
!                               }
                        }
  
***************
*** 110,115 ****
        public IRegistryKeyProvider OpenSubKey(String name, bool writable)
                        {
!                               // TODO
!                               return null;
                        }
  
--- 577,600 ----
        public IRegistryKeyProvider OpenSubKey(String name, bool writable)
                        {
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               IntPtr newKey;
!                                               if(RegOpenKeyEx
!                                                               (hKey, name, 0,
!                                                                (writable ? 
KEY_ALL_ACCESS : KEY_READ),
!                                                                out newKey) != 
0)
!                                               {
!                                                       return null;
!                                               }
!                                               return new Win32KeyProvider
!                                                       (this.name + "\\" + 
name, newKey);
!                                       }
!                                       else
!                                       {
!                                               return null;
!                                       }
!                               }
                        }
  
***************
*** 117,121 ****
        public void SetValue(String name, Object value)
                        {
!                               // TODO
                        }
  
--- 602,695 ----
        public void SetValue(String name, Object value)
                        {
!                               uint type;
!                               byte[] data;
! 
!                               // Convert the value into a byte array and type.
!                               if(value is String)
!                               {
!                                       // Convert a string.
!                                       type = REG_SZ;
!                                       data = StringToArray((String)value);
!                               }
!                               else if(value is int)
!                               {
!                                       // Convert a signed integer.
!                                       int ivalue = (int)value;
!                                       type = REG_DWORD_LITTLE_ENDIAN;
!                                       data = new byte [4];
!                                       data[0] = (byte)ivalue;
!                                       data[1] = (byte)(ivalue >> 8);
!                                       data[2] = (byte)(ivalue >> 16);
!                                       data[3] = (byte)(ivalue >> 24);
!                               }
!                               else if(value is uint)
!                               {
!                                       // Convert an unsigned integer.
!                                       uint uivalue = (uint)value;
!                                       type = REG_DWORD_LITTLE_ENDIAN;
!                                       data = new byte [4];
!                                       data[0] = (byte)uivalue;
!                                       data[1] = (byte)(uivalue >> 8);
!                                       data[2] = (byte)(uivalue >> 16);
!                                       data[3] = (byte)(uivalue >> 24);
!                               }
!                               else if(value is long)
!                               {
!                                       // Convert a signed long integer.
!                                       long lvalue = (long)value;
!                                       type = REG_QWORD_LITTLE_ENDIAN;
!                                       data = new byte [8];
!                                       data[0] = (byte)lvalue;
!                                       data[1] = (byte)(lvalue >> 8);
!                                       data[2] = (byte)(lvalue >> 16);
!                                       data[3] = (byte)(lvalue >> 24);
!                                       data[4] = (byte)(lvalue >> 32);
!                                       data[5] = (byte)(lvalue >> 40);
!                                       data[6] = (byte)(lvalue >> 48);
!                                       data[7] = (byte)(lvalue >> 56);
!                               }
!                               else if(value is ulong)
!                               {
!                                       // Convert an unsigned long integer.
!                                       ulong ulvalue = (ulong)value;
!                                       type = REG_QWORD_LITTLE_ENDIAN;
!                                       data = new byte [8];
!                                       data[0] = (byte)ulvalue;
!                                       data[1] = (byte)(ulvalue >> 8);
!                                       data[2] = (byte)(ulvalue >> 16);
!                                       data[3] = (byte)(ulvalue >> 24);
!                                       data[4] = (byte)(ulvalue >> 32);
!                                       data[5] = (byte)(ulvalue >> 40);
!                                       data[6] = (byte)(ulvalue >> 48);
!                                       data[7] = (byte)(ulvalue >> 56);
!                               }
!                               else if(value is byte[])
!                               {
!                                       // Convert a raw binary byte array.
!                                       type = REG_BINARY;
!                                       data = (byte[])value;
!                               }
!                               else if(value is String[])
!                               {
!                                       // Convert an array of strings.
!                                       type = REG_MULTI_SZ;
!                                       data = 
StringArrayToArray((String[])value);
!                               }
!                               else
!                               {
!                                       // Last ditch attempt: use the string 
form of the value.
!                                       type = REG_SZ;
!                                       data = StringToArray(value.ToString());
!                               }
! 
!                               // Set the value within the registry.
!                               lock(this)
!                               {
!                                       if(hKey != IntPtr.Zero)
!                                       {
!                                               RegSetValueEx(hKey, name, 0, 
type, data,
!                                                                         
(uint)(data.Length));
!                                       }
!                               }
                        }
  
***************
*** 134,138 ****
                                get
                                {
!                                       return 0;
                                }
                        }
--- 708,726 ----
                                get
                                {
!                                       lock(this)
!                                       {
!                                               if(hKey != IntPtr.Zero)
!                                               {
!                                                       uint numSubKeys, 
numValues;
!                                                       RegQueryInfoKey(hKey, 
null,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        out numSubKeys, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, out numValues,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, IntPtr.Zero);
!                                                       return (int)numSubKeys;
!                                               }
!                                               return 0;
!                                       }
                                }
                        }
***************
*** 143,149 ****
                                get
                                {
!                                       return 0;
                                }
                        }
  
  }; // class Win32KeyProvider
--- 731,889 ----
                                get
                                {
!                                       lock(this)
!                                       {
!                                               if(hKey != IntPtr.Zero)
!                                               {
!                                                       uint numSubKeys, 
numValues;
!                                                       RegQueryInfoKey(hKey, 
null,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        out numSubKeys, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, out numValues,
!                                                                               
        IntPtr.Zero, IntPtr.Zero,
!                                                                               
        IntPtr.Zero, IntPtr.Zero);
!                                                       return (int)numValues;
!                                               }
!                                               return 0;
!                                       }
                                }
                        }
+ 
+       // Convert a hive value into a HKEY value.
+       public static IntPtr HiveToHKey(RegistryHive hive)
+                       {
+                               return new IntPtr((int)hive);
+                       }
+ 
+       // Import the Win32 registry functions from "advapi32.dll".
+ 
+       [DllImport("advapi32.dll",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegCloseKey(IntPtr hKey);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegConnectRegistryW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern public static int RegConnectRegistry
+                               ([MarshalAs(UnmanagedType.LPWStr)] String 
lpMachineName,
+                                IntPtr hKey, out IntPtr phkResult);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegCreateKeyW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegCreateKey
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpSubKey,
+                                out IntPtr phkResult);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegDeleteKeyW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegDeleteKey
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpSubKey);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegDeleteValueW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegDeleteValue
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpValueName);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegEnumKeyExW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegEnumKeyEx
+                               (IntPtr hkey, uint index,
+                                char[] lpName, ref uint lpcbName,
+                                IntPtr reserved, IntPtr lpClass, IntPtr 
lpcbClass,
+                                out long lpftLastWriteTime);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegEnumValueW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegEnumValue
+                               (IntPtr hkey, uint index,
+                                char[] lpValueName, ref uint lpcbValueName,
+                                IntPtr reserved, IntPtr lpType,
+                                IntPtr lpData, IntPtr lpcbData);
+ 
+       [DllImport("advapi32.dll",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegFlushKey(IntPtr hKey);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegOpenKeyExW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegOpenKeyEx
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpSubKey,
+                                uint ulOptions, uint samDesired, out IntPtr 
phkResult);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegQueryInfoKeyW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegQueryInfoKey
+                               (IntPtr hkey,
+                                byte[] lpClass, IntPtr lpcbClass, IntPtr 
lpReserved,
+                                out uint lpcSubKeys, IntPtr lpcbMaxSubKeyLen,
+                                IntPtr lpcbMaxClassLen, out uint lpcValues,
+                                IntPtr lpcbMaxValueNameLen, IntPtr 
lpcbMaxValueLen,
+                                IntPtr lpcbSecurityDescriptor, IntPtr 
lpftLastWriteTime);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegQueryValueExW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegQueryValueEx
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpValueName,
+                                IntPtr lpReserved, out uint lpType,
+                                byte[] lpData, ref uint lpcbData);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegQueryValueExW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegQueryValueEx
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpValueName,
+                                IntPtr lpReserved, out uint lpType,
+                                IntPtr lpData, ref uint lpcbData);
+ 
+       [DllImport("advapi32.dll", EntryPoint="RegSetValueExW",
+                          CallingConvention=CallingConvention.Winapi,
+                          PreserveSig=true)]
+       extern private static int RegSetValueEx
+                               (IntPtr hkey,
+                                [MarshalAs(UnmanagedType.LPWStr)] String 
lpValueName,
+                                uint Reserved, uint dwType,
+                                byte[] lpData, uint cbData);
+ 
+       // Type codes for values that can be stored in the registry.
+ 
+       private const uint REG_NONE                       = 0;
+       private const uint REG_SZ                         = 1;
+       private const uint REG_EXPAND_SZ                  = 2;
+       private const uint REG_BINARY                     = 3;
+       private const uint REG_DWORD_LITTLE_ENDIAN        = 4;
+       private const uint REG_DWORD_BIG_ENDIAN           = 5;
+       private const uint REG_LINK                       = 6;
+       private const uint REG_MULTI_SZ                   = 7;
+       private const uint REG_RESOURCE_LIST              = 8;
+       private const uint REG_FULL_RESOURCE_DESCRIPTOR   = 9;
+       private const uint REG_RESOURCE_REQUIREMENTS_LIST = 10;
+       private const uint REG_QWORD_LITTLE_ENDIAN        = 11;
+ 
+       // Access types for the "samDesired" parameter of "RegOpenKeyEx".
+ 
+       private const uint KEY_QUERY_VALUE                = 1;
+       private const uint KEY_SET_VALUE                  = 2;
+       private const uint KEY_CREATE_SUB_KEY             = 4;
+       private const uint KEY_ENUMERATE_SUB_KEYS         = 8;
+       private const uint KEY_NOTIFY                     = 16;
+       private const uint KEY_CREATE_LINK                = 32;
+       private const uint KEY_WRITE                      = 0x20006;
+       private const uint KEY_READ                       = 0x20019;
+       private const uint KEY_ALL_ACCESS                 = 0xF003F;
  
  }; // class Win32KeyProvider





reply via email to

[Prev in Thread] Current Thread [Next in Thread]