RegistryHandle.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Tools / xws_reg / System / ServiceModel / Install / RegistryHandle.cs / 1 / RegistryHandle.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Install 
{
    using Microsoft.Win32.SafeHandles; 
    using System.ComponentModel; 
    using System.Text;
 
    class RegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        static readonly RegistryHandle HKEY_LOCAL_MACHINE =
            new RegistryHandle(new IntPtr(unchecked((int)0x80000002)), false); 

        const uint KEY_QUERY_VALUE = 0x0001; 
        const uint KEY_WOW64_64KEY = 0x0100; 
        const int REG_SZ = 1;
 
        RegistryHandle(IntPtr hKey, bool ownHandle)
            : base(ownHandle)
        {
            handle = hKey; 
        }
 
        RegistryHandle() 
            : base(true)
        { 
            // empty
        }

        public static RegistryHandle OpenNativeHKLMSubkey(string subKey) 
        {
            RegistryHandle result; 
            const uint samDesired = KEY_QUERY_VALUE | KEY_WOW64_64KEY; 
            int status =
                NativeMethods.RegOpenKeyEx(RegistryHandle.HKEY_LOCAL_MACHINE, subKey, 0, samDesired, out result); 
            if (status != ErrorCodes.ERROR_SUCCESS)
            {
                throw new Win32Exception(status);
            } 
            return result;
        } 
 
        public string QueryStringValue(string value)
        { 
            int type;
            int sizeInBytes = 0;

            int status = NativeMethods.RegQueryValueEx(this, value, null, out type, null, ref sizeInBytes); 
            if (status != ErrorCodes.ERROR_SUCCESS)
            { 
                throw new Win32Exception(status); 
            }
            if (type != REG_SZ) 
            {
                throw new InvalidOperationException(SR.GetString(SR.QueryStringValueTypeMismatch, value, type));
            }
 
            StringBuilder builder = new StringBuilder(sizeInBytes / sizeof(char));
            status = NativeMethods.RegQueryValueEx(this, value, null, out type, builder, ref sizeInBytes); 
            if (status != ErrorCodes.ERROR_SUCCESS) 
            {
                throw new Win32Exception(status); 
            }

            return builder.ToString();
        } 

        protected override bool ReleaseHandle() 
        { 
            return (NativeMethods.RegCloseKey(handle) == ErrorCodes.ERROR_SUCCESS);
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK