Beispiel #1
0
        public static object RegGetValue(RegistryHive hive, string key, string sValue, out int type)
        {
            type = 0;
            if (string.IsNullOrEmpty(sHostName))
            {
                return(null);
            }

            const int ErrorMoreDataIsAvailable = 234;

            byte[] buffer = null;
            int    size = 0;
            IntPtr hKey = (IntPtr)0, hSubKey = (IntPtr)0, data = (IntPtr)0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    if ((RegistryInteropWindows.RegOpenKey(hKey, key, out hSubKey)) == 0)
                    {
                        buffer = new byte[512];
                        size   = buffer.Length;
                        if ((RegistryInteropWindows.RegQueryValueEx(hSubKey, sValue, 0, out type, buffer, ref size)) == ErrorMoreDataIsAvailable)
                        {
                            // Resize buffer and perform query again
                            Array.Resize <byte>(ref buffer, size);
                            size = buffer.Length;
                            RegistryInteropWindows.RegQueryValueEx(hSubKey, sValue, 0, out type, buffer, ref size);
                        }
                    }
                }
                finally
                {
                    if ((int)hSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(hSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }
                }
            }

            return(buffer);
        }
Beispiel #2
0
        public static int RegSetValue(RegistryHive hive, string key, string sValue, object data)
        {
            int    ret = -1;
            int    cData;
            IntPtr hKey = (IntPtr)0, phSubKey = (IntPtr)0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    if ((RegistryInteropWindows.RegOpenKey(hKey, key, out phSubKey)) == 0)
                    {
                        byte[] buffer = data as byte[];
                        cData = buffer.Length;
                        ret   = RegistryInteropWindows.RegSetValueEx(phSubKey, sValue, 0,
                                                                     RegistryValueKind.Unknown, buffer, cData);
                    }
                }
                finally
                {
                    if ((int)phSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(phSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }
                }

                return(ret);
            }

            return(ret);
        }