SafeNativeMethods.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 / comsvcutil / SafeNativeMethods.cs / 1 / SafeNativeMethods.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.Tools.ServiceModel.ComSvcConfig
{ 
    using System;
    using System.Collections; 
    using System.Runtime.InteropServices; 
    using System.Runtime.InteropServices.ComTypes;
    using System.Reflection; 
    using System.Security.Permissions;
    using Microsoft.Win32;
    using Microsoft.Win32.SafeHandles;
    using System.Resources; 
    using System.Text;
    using _SYSKIND = System.Runtime.InteropServices.ComTypes.SYSKIND; 
    using System.Globalization; 
    using Microsoft.Tools.ServiceModel;
    using Microsoft.Tools.ServiceModel.SvcUtil; 


    internal class RegistryHandle : SafeHandleZeroOrMinusOneIsInvalid
    { 
        internal static readonly RegistryHandle HKEY_CLASSES_ROOT         = new RegistryHandle (new IntPtr(unchecked((int)0x80000000)), false);
        internal static readonly RegistryHandle HKEY_CURRENT_USER         = new RegistryHandle (new IntPtr(unchecked((int)0x80000001)), false); 
        internal static readonly RegistryHandle HKEY_LOCAL_MACHINE        = new RegistryHandle (new IntPtr(unchecked((int)0x80000002)), false); 
        internal static readonly RegistryHandle HKEY_USERS                = new RegistryHandle (new IntPtr(unchecked((int)0x80000003)), false);
        internal static readonly RegistryHandle HKEY_PERFORMANCE_DATA     = new RegistryHandle (new IntPtr(unchecked((int)0x80000004)), false); 
        internal static readonly RegistryHandle HKEY_CURRENT_CONFIG       = new RegistryHandle (new IntPtr(unchecked((int)0x80000005)), false);
        internal static readonly RegistryHandle HKEY_DYN_DATA             = new RegistryHandle (new IntPtr(unchecked((int)0x80000006)), false);

        static RegistryHandle GetHKCR () 
        {
            RegistryHandle regHandle = null; 
            int status = SafeNativeMethods.RegOpenKeyEx (HKEY_LOCAL_MACHINE, @"Software\Classes", 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle); 
            if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid)
            { 
                throw Tool.CreateException (SR.GetString (SR.FailedToOpenRegistryKey, ""), null);
            }
            return regHandle;
 
        }
 
        static RegistryHandle Get64bitHKCR () 
        {
           RegistryHandle regHandle = null; 
           int status = SafeNativeMethods.RegOpenKeyEx (HKEY_LOCAL_MACHINE, @"Software\Classes", 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_64KEY, out regHandle);
           if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid)
           {
                throw Tool.CreateException (SR.GetString (SR.FailedToOpenRegistryKey, ""), null); 
           }
           return regHandle; 
        } 

        static RegistryHandle Get32bitHKCR () 
        {
           RegistryHandle regHandle = null;
           int status = SafeNativeMethods.RegOpenKeyEx (HKEY_LOCAL_MACHINE, @"Software\Classes", 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_32KEY, out regHandle);
           if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid) 
           {
                throw Tool.CreateException (SR.GetString (SR.FailedToOpenRegistryKey, ""), null); 
           } 
           return regHandle;
        } 

        static RegistryHandle GetCorrectBitnessHive (bool is64bit)
        {
            if (is64bit && IntPtr.Size == 8) // No worries we are trying to open up a 64 bit hive just return 
                return GetHKCR ();
            else if (is64bit && IntPtr.Size == 4) // we are running under wow get the 64 bit hive 
                return Get64bitHKCR (); 
            else if (!is64bit && IntPtr.Size == 8) // we are running in 64 bit but need to open a 32 bit hive
                return Get32bitHKCR (); 
            else if (!is64bit && IntPtr.Size == 4)
                return GetHKCR ();

            throw Tool.CreateException(SR.GetString(SR.UnableToDetermineHiveBitness), null); 
        }
 
        public static RegistryHandle GetBitnessHKCR (bool is64bit) 
        {
            return GetCorrectBitnessHive (is64bit); 
        }


        public static RegistryHandle GetCorrectBitnessHKLMSubkey(bool is64bit, string key) 
        {
            if (is64bit && IntPtr.Size == 8) // No worries we are trying to open up a 64 bit hive just return 
                return GetHKLMSubkey(key); 
            else if (is64bit && IntPtr.Size == 4) // we are running under wow get the 64 bit hive
                return Get64bitHKLMSubkey(key); 
            else if (!is64bit && IntPtr.Size == 8) // we are running in 64 bit but need to open a 32 bit hive
                return Get32bitHKLMSubkey(key);
            else if (!is64bit && IntPtr.Size == 4)
                return GetHKLMSubkey(key); 

            throw Tool.CreateException(SR.GetString(SR.UnableToDetermineHiveBitness), null); 
        } 

 
        static RegistryHandle GetHKLMSubkey(string key)
        {
            RegistryHandle regHandle = null;
            int status = SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, out regHandle); 
            if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid)
            { 
                throw Tool.CreateException(SR.GetString(SR.FailedToOpenRegistryKey, ""), null); 
            }
            return regHandle; 

        }

        static RegistryHandle Get64bitHKLMSubkey(string key) 
        {
            RegistryHandle regHandle = null; 
            int status = SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_64KEY, out regHandle); 
            if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid)
            { 
                throw Tool.CreateException(SR.GetString(SR.FailedToOpenRegistryKey, ""), null);
            }
            return regHandle;
        } 

        static RegistryHandle Get32bitHKLMSubkey(string key) 
        { 
            RegistryHandle regHandle = null;
            int status = SafeNativeMethods.RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE | SafeNativeMethods.KEY_WOW64_32KEY, out regHandle); 
            if (status != SafeNativeMethods.ERROR_SUCCESS || null == regHandle || regHandle.IsInvalid)
            {
                throw Tool.CreateException(SR.GetString(SR.FailedToOpenRegistryKey, ""), null);
            } 
            return regHandle;
        } 
 

        public RegistryHandle (IntPtr hKey, bool ownHandle) : base (ownHandle) 
        {
            handle = hKey;
        }
 
        public RegistryHandle () : base (true)
        { 
 

 
        }

        public bool DeleteKey (string key)
        { 
            int status = SafeNativeMethods.RegDeleteKey (this, key);
            if (status == SafeNativeMethods.ERROR_SUCCESS) 
                return true; 
            else
                return false; 
        }

        public RegistryHandle CreateSubKey (string subKey)
        { 
            RegistryHandle regHandle = null;
            int    disposition; 
            int status = SafeNativeMethods.RegCreateKeyEx (this, subKey, 0, null, 0, SafeNativeMethods.KEY_READ | SafeNativeMethods.KEY_WRITE, IntPtr.Zero, out regHandle, out disposition ); 
            if (status != SafeNativeMethods.ERROR_SUCCESS || regHandle == null || regHandle.IsInvalid)
                Tool.CreateException (SR.GetString (SR.FailedToCreateSubKey, subKey), null  ); 
            return regHandle;
        }

        public void SetValue (string valName, string value) 
        {
            int status = SafeNativeMethods.RegSetValueEx (this, valName, 0, SafeNativeMethods.REG_SZ, value, (value.Length * 2) + 2); 
            if (status != SafeNativeMethods.ERROR_SUCCESS) 
                Tool.CreateException(SR.GetString(SR.FailedToSetValue, valName), null);
        } 

        public string GetStringValue (string valName)
        {
            int type = 0; 
            int datasize = 0;
            int ret = SafeNativeMethods.RegQueryValueEx(this, valName, null, ref type, (byte[])null, ref datasize); 
            if (ret == SafeNativeMethods.ERROR_SUCCESS) 
                if (type == SafeNativeMethods.REG_SZ)
                { 
                    byte [] blob = new byte [datasize];
                    ret = SafeNativeMethods.RegQueryValueEx(this, valName, null, ref type, (byte[]) blob, ref datasize);
                    UnicodeEncoding unicode = new UnicodeEncoding();
                    return unicode.GetString (blob); 
                }
            return null; 
        } 

        protected  override bool ReleaseHandle () 
        {
            if (SafeNativeMethods.RegCloseKey (handle) == SafeNativeMethods.ERROR_SUCCESS)
                return true;
            else 
                return false;
        } 
    } 

    internal static class SafeNativeMethods 
    {


            internal const int ERROR_MORE_DATA                 = 0xEA; 
            internal const int ERROR_SUCCESS                   = 0;
            internal const int READ_CONTROL                    = 0x00020000; 
            internal const int SYNCHRONIZE                     = 0x00100000; 
            internal const int STANDARD_RIGHTS_READ            = READ_CONTROL;
            internal const int STANDARD_RIGHTS_WRITE           = READ_CONTROL; 
            internal const int KEY_QUERY_VALUE        = 0x0001;
            internal const int KEY_SET_VALUE          = 0x0002;
            internal const int KEY_CREATE_SUB_KEY     = 0x0004;
            internal const int KEY_ENUMERATE_SUB_KEYS = 0x0008; 
            internal const int KEY_NOTIFY             = 0x0010;
            internal const int KEY_CREATE_LINK        = 0x0020; 
            internal const int KEY_READ               =((STANDARD_RIGHTS_READ       | 
                                                               KEY_QUERY_VALUE            |
                                                               KEY_ENUMERATE_SUB_KEYS     | 
                                                               KEY_NOTIFY)
                                                              &
                                                              (~SYNCHRONIZE));
 
            internal const int KEY_WRITE              =((STANDARD_RIGHTS_WRITE      |
                                                               KEY_SET_VALUE              | 
                                                               KEY_CREATE_SUB_KEY) 
                                                              &
                                                              (~SYNCHRONIZE)); 
            internal const int REG_NONE                    = 0;     // No value type
            internal const int REG_SZ                      = 1;     // Unicode nul terminated string
            internal const int REG_EXPAND_SZ               = 2;     // Unicode nul terminated string
            internal const int KEY_WOW64_32KEY             = (0x0200); 
            internal const int KEY_WOW64_64KEY             = (0x0100);
 
 
            // (with environment variable references)
            internal const int REG_BINARY                  = 3;     // Free form binary 
            internal const int REG_DWORD                   = 4;     // 32-bit number
            internal const int REG_DWORD_LITTLE_ENDIAN     = 4;     // 32-bit number (same as REG_DWORD)
            internal const int REG_DWORD_BIG_ENDIAN        = 5;     // 32-bit number
            internal const int REG_LINK                    = 6;     // Symbolic Link (unicode) 
            internal const int REG_MULTI_SZ                = 7;     // Multiple Unicode strings
            internal const int REG_RESOURCE_LIST           = 8;     // Resource list in the resource map 
            internal const int REG_FULL_RESOURCE_DESCRIPTOR  = 9;   // Resource list in the hardware description 
            internal const int REG_RESOURCE_REQUIREMENTS_LIST = 10;
            internal const int REG_QWORD                   = 11;    // 64-bit number 

            internal const int HWND_BROADCAST              = 0xffff;
            internal const int WM_SETTINGCHANGE            = 0x001A;
 
            internal const String KERNEL32 = "kernel32.dll";
            internal const String USER32   = "user32.dll"; 
            internal const String ADVAPI32 = "advapi32.dll"; 
            internal const String OLE32    = "ole32.dll";
            internal const String OLEAUT32 = "oleaut32.dll"; 
            internal const String SHFOLDER = "shfolder.dll";
            internal const String SHIM     = "mscoree.dll";
            internal const String CRYPT32  = "crypt32.dll";
            internal const String SECUR32  = "secur32.dll"; 
            internal const String MSCORWKS = "mscorwks.dll";
 
 
            [DllImport(OLE32, ExactSpelling = true, PreserveSig = false)]
            [return: MarshalAs(UnmanagedType.Interface)] 
            internal static extern object DllGetClassObject (
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
 

 
            [DllImport(ADVAPI32, CharSet=CharSet.Unicode, BestFitMapping=false)] 
            internal static extern int RegOpenKeyEx(RegistryHandle hKey, String lpSubKey,
                        int ulOptions, int samDesired, out RegistryHandle hkResult); 

            [DllImport(ADVAPI32, CharSet=CharSet.Unicode, BestFitMapping=false)]
            internal static extern int RegSetValueEx(RegistryHandle hKey, String lpValueName,
                        int Reserved, int dwType, String val, int cbData); 

            // Note: RegCreateKeyEx won't set the last error on failure - it returns 
            // an error code if it fails. 
            [DllImport(ADVAPI32, CharSet=CharSet.Unicode, BestFitMapping=false)]
            internal static extern int RegCreateKeyEx(RegistryHandle hKey, String lpSubKey, 
                        int Reserved, String lpClass, int dwOptions,
                        int samDesigner, IntPtr lpSecurityAttributes,
                        out RegistryHandle hkResult, out int lpdwDisposition);
 
            [DllImport(ADVAPI32, SetLastError=false)]
            internal static extern int RegCloseKey(IntPtr handle); 
 
            [DllImport(ADVAPI32, CharSet=CharSet.Unicode, BestFitMapping=false)]
            internal static extern int RegQueryValueEx(RegistryHandle hKey, String lpValueName, 
                        int[] lpReserved, ref int lpType, [Out] byte[] lpData,
                        ref int lpcbData);

            [DllImport(ADVAPI32, CharSet=CharSet.Unicode, BestFitMapping=false)] 
            internal static extern int RegDeleteKey(RegistryHandle hKey, String lpValueName);
            [DllImport(OLEAUT32, CharSet=CharSet.Unicode, BestFitMapping=false)] 
            internal static extern int CreateTypeLib2 (System.Runtime.InteropServices.ComTypes.SYSKIND sysKind, string szFile, out ICreateTypeLib ppctlib); 

            internal static ICreateTypeLib CreateTypeLib (string file) 
            {
                ICreateTypeLib typelib;
                int hr = CreateTypeLib2  (System.Runtime.InteropServices.ComTypes.SYSKIND.SYS_WIN32, file, out typelib);
                if (hr == 0) 
                    return typelib;
                else 
                    Marshal.ThrowExceptionForHR (hr); 
                return null;
            } 

    }
    [ComImport]
    [Guid("D5F569D0-593B-101A-B569-08002B2DBF7A")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IPSFactoryBuffer 
    { 
        void CreateProxy (IntPtr inner, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,  out object proxy, out object ppv);
        void CreateStub (); 
    }

    [ComImport]
    [Guid("00020406-0000-0000-C000-000000000046")] 
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface ICreateTypeLib 
    { 
        [return : MarshalAs(UnmanagedType.Interface)]
        ICreateTypeInfo CreateTypeInfo([In, MarshalAs(UnmanagedType.LPStr)] String szName, 
                              System.Runtime.InteropServices.ComTypes.TYPEKIND tkind);
        void SetName([In, MarshalAs(UnmanagedType.LPStr)] string szName);
        void SetVersion(short wMajorVerNum, short wMinorVerNum);
        void SetGuid([In, MarshalAs(UnmanagedType.LPStruct)] Guid guid); 
        void SetDocString([In, MarshalAs(UnmanagedType.LPStr)] String szDoc);
        void SetHelpFileName([In, MarshalAs(UnmanagedType.LPStr)] String szHelpFileName); 
        void SetHelpContext(int dwHelpContext); 
        void SetLcid(int lcid);
        void SetLibFlags(int uLibFlags); 
        void SaveAllChanges();
    }

    [ComImport] 
    [Guid("00020405-0000-0000-C000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    internal interface ICreateTypeInfo 
    {
         void SetGuid([In, MarshalAs(UnmanagedType.LPStruct)] Guid guid); 
         void SetTypeFlags(int uTypeFlags);
         void SetDocString(string doc);
         void SetHelpContext(int dwHelpContext);
         void SetVersion (short  wMajorVerNum, short wMinorVerNum); 
         void AddRefTypeInfo(ITypeInfo  pTInfo, IntPtr  phRefType);
         void AddFuncDesc(uint index, ref System.Runtime.InteropServices.ComTypes.FUNCDESC pFuncDesc); 
         void AddImplType(uint index, IntPtr hRefType); 
         void SetImplTypeFlags(uint index,int implTypeFlags);
         void SetAlignment(int cbAlignment); 
         void SetSchema(string pStrSchema);
         void AddVarDesc (uint index, ref System.Runtime.InteropServices.ComTypes.VARDESC pVarDesc);
         void SetFuncAndParamNames();
         void SetVarName(); 
         void SetTypeDescAlias();
         void DefineFuncAsDllEntry(); 
         void SetFuncDocString(); 
         void SetVarDocString(ushort index, string szDocString);
         void SetFuncHelpContext(ushort index, uint dwHelpContext); 
         void SetVarHelpContext(ushort index, uint dwHelpContext);
         void SetMops(ushort index,string  bstrMops);
         void SetTypeIdldesc(ref System.Runtime.InteropServices.ComTypes.IDLDESC  pIdlDesc);
         void LayOut(); 
    }
 
    [ComImport] 
    [Guid("0002040E-0000-0000-C000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    internal interface ICreateTypeInfo2
    {
         void SetGuid([In, MarshalAs(UnmanagedType.LPStruct)] Guid guid);
         void SetTypeFlags(int uTypeFlags); 
         void SetDocString(string doc);
         void SetHelpContext(int dwHelpContext); 
         void SetVersion (short  wMajorVerNum, short wMinorVerNum); 
         void AddRefTypeInfo(ITypeInfo  pTInfo, IntPtr  phRefType);
         void AddFuncDesc(uint index, ref System.Runtime.InteropServices.ComTypes.FUNCDESC pFuncDesc); 
         void AddImplType(uint index, IntPtr hRefType);
         void SetImplTypeFlags(uint index,int implTypeFlags);
         void SetAlignment(int cbAlignment);
         void SetSchema(string pStrSchema); 
         void AddVarDesc (uint index, ref System.Runtime.InteropServices.ComTypes.VARDESC pVarDesc);
         void SetFuncAndParamNames(); 
         void SetVarName(); 
         void SetTypeDescAlias();
         void DefineFuncAsDllEntry(); 
         void SetFuncDocString();
         void SetVarDocString(ushort index, string szDocString);
         void SetFuncHelpContext(ushort index, uint dwHelpContext);
         void SetVarHelpContext(ushort index, uint dwHelpContext); 
         void SetMops(ushort index,string  bstrMops);
         void SetTypeIdldesc(ref System.Runtime.InteropServices.ComTypes.IDLDESC  pIdlDesc); 
         void LayOut(); 
         void DeleteFuncDesc(ushort index);
         void DeleteFuncDescByMemId(int memid,  System.Runtime.InteropServices.ComTypes.INVOKEKIND invKind); 
         void DeleteVarDesc(ushort index);
         void DeleteVarDescByMemId(int memid);
         void DeleteImplType();
         void SetCustData(); 
         void SetFuncCustData();
         void SetParamCustData( ); 
         void SetVarCustData( ); 
         void SetImplTypeCustData( );
         void SetHelpStringContext(); 
         void SetFuncHelpStringContext();
         void SetVarHelpStringContext();
         void Invalidate();
         void SetName (string szName); 
    }
 
 
}

// 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