Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

RegOpenKeyEx (advapi32)
 
.
Summary
Opens the specified registry key

C# Signature:

  [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  public static extern int RegOpenKeyEx(
    UIntPtr hKey,
    string subKey,
    int ulOptions,
    int samDesired,
    out UIntPtr hkResult);

VB Signature:

Declare Auto Function RegOpenKeyEx Lib "advapi32.dll"  (
   ByVal hKey As IntPtr,
   ByVal lpSubKey As String,
   ByVal ulOptions As Integer,
   ByVal samDesired As Integer,
   ByRef phkResult As Integer
) As Integer

User-Defined Types:

None.

Change Notes:

- Changed IntPtr to UIntPtr: When invoking with IntPtr for the handles, you will run into an Overflow. UIntPtr is the right choice if you wish this to work correctly on 32 and 64 bit platforms.

Notes:

The current managed versions of OpenSubKey do not support read/writing from the different registry "views" on 64-bit operating systems. So, you're forced to the the APIs to accomplish this feat. See the example below for a "drop in" replacement for OpenSubKey

If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.

uint options: reserved, must be zero.

int sam: access mask that specifies the desired access rights to the key.

values for hKey (C#):

public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
public static UIntPtr HKEY_CURRENT_USER  = new UIntPtr(0x80000001u);

values for sam (VB):

Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_WOW64_32KEY = &H200
Const KEY_WOW64_64KEY = &H100
Const KEY_WOW64_RES = &H300

For compatibility with 16-bit versions of Windows only 'RegOpenKey' is available.

Tips & Tricks:

Please add some!!

Sample Code (VB):

Public Enum RegWow64Options As Integer
   None = 0
   KEY_WOW64_64KEY = &H100
   KEY_WOW64_32KEY = &H200
End Enum

Public Enum RegistryRights As Integer
   ReadKey = 131097
   WriteKey = 131078
End Enum

' overloaded, see below
Public Function OpenSubKey(ByVal Name As String) As RegistryKey
   Return OpenSubKey(Name, False, RegWow64Options.None)
End Function

' overloaded, see below
Public Function OpenSubKey(ByVal Name As String, ByVal Writeable As Boolean) As RegistryKey
   Return OpenSubKey(Name, Writeable, RegWow64Options.None)
End Function

' open a SubKey
Public Function OpenSubKey(ByVal Name As String, ByVal Writeable As Boolean, ByVal Options As RegWow64Options) As RegistryKey
   Dim ret, Rights As Integer
   Dim hSubKey As IntPtr

   ' quick sanity check
   If hKey.Equals(IntPtr.Zero) Then
     Throw New ApplicationException("Cannot access a closed registry key")
   End If

   Rights = RegistryRights.ReadKey
   If Writeable Then
     Rights = RegistryRights.WriteKey
   End If

   ' Ignore the 64-bit flags when running from <= Win2k
   If CDbl(Environment.OSVersion.Version.Major & "." & Environment.OSVersion.Version.Minor) <= 5.0 Then
     Options = RegWow64Options.None
   End If

   ret = RegOpenKeyEx(hKey, Name, 0, Rights Or Options, hSubKey)
   If ret <> 0 Then
     Return Nothing
   End If

   Dim ans As New RegistryKey
   ans.hKey = hSubKey
   ans.RegKeyName = Name
   ans.IsRootHive = False
   Return ans
End Function

Sample Code (VB) to convert an IntPtr to a RegistryKey

Public Shared Function PointerToRegistryKey(ByVal hKey As IntPtr, ByVal writable As Boolean, ByVal ownsHandle As Boolean) As RegistryKey
    ' Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
    Dim privateConstructors = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
    Dim safeRegistryHandleType = GetType(SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle")
    Dim safeRegistryHandleConstructorTypes As System.Type() = {GetType(IntPtr), GetType(Boolean)}
    Dim safeRegistryHandleConstructor = safeRegistryHandleType.GetConstructor(privateConstructors, Nothing, safeRegistryHandleConstructorTypes, Nothing)
    Dim safeHandle = safeRegistryHandleConstructor.Invoke(New Object() {hKey, ownsHandle})

    ' Create a new Registry key using the private constructor using the safeHandle - this should then behave like a .NET natively opened handle and disposed of correctly
    Dim registryKeyType = GetType(RegistryKey)
    Dim registryKeyConstructorTypes As System.Type() = {safeRegistryHandleType, GetType(Boolean)}
    Dim registryKeyConstructor = registryKeyType.GetConstructor(privateConstructors, Nothing, registryKeyConstructorTypes, Nothing)
    Dim result = DirectCast(registryKeyConstructor.Invoke(New Object() {safeHandle, writable}), RegistryKey)
    Return result
End Function

Sample Code (C#) to read a string value

string example = ReadRegKey(HKEY_LOCAL_MACHINE, @"Software\SomeCompany\OurProduct", "InstalledVersion");

private static string ReadRegKey(UIntPtr rootKey, string keyPath, string valueName)
{
   if (RegOpenKeyEx(rootKey, keyPath, 0, KEY_READ, out hKey) == 0)
   {
      uint size = 1024;
      uint type;
      string keyValue = null;
      StringBuilder keyBuffer = new StringBuilder();

      if (RegQueryValueEx(hKey, valueName, IntPtr.Zero, out type, keyBuffer, ref size) == 0)
      keyValue = keyBuffer.ToString();

      RegCloseKey(hKey);

      return(keyValue);
  }

  return(null);  // Return null if the value could not be read

}

Alternative Managed API:

Microsoft.Win32.Registry.BaseKeyName.OpenSubKey("[KeyName]")

Documentation

Working VB.net Code

The code above didn't compile for me - this code compiles and works for my needs:

    ' Basis from http://blogs.msdn.com/cumgranosalis/archive/2005/12/09/Win64RegistryPart1.aspx
    Private Function GetRegistryKeyHandle(ByVal RegisteryKey As Microsoft.Win32.RegistryKey) As System.IntPtr
    Dim Type As System.Type = System.Type.GetType("Microsoft.Win32.RegistryKey")
    Dim Info As System.Reflection.FieldInfo = Type.GetField("hkey", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)

    Dim Handle As System.Runtime.InteropServices.SafeHandle = CType(Info.GetValue(RegisteryKey), System.Runtime.InteropServices.SafeHandle)
    Dim RealHandle As System.IntPtr = Handle.DangerousGetHandle

    Return Handle.DangerousGetHandle
    End Function

    ' Basis from: http://www.pinvoke.net/default.aspx/advapi32/RegOpenKeyEx.html
    Public Enum RegWow64Options As System.Int32
    None = 0
    KEY_WOW64_64KEY = &H100
    KEY_WOW64_32KEY = &H200
    End Enum

    Private Enum RegistryRights As System.Int32
    ReadKey = 131097
    WriteKey = 131078
    End Enum

    Private Declare Auto Function RegOpenKeyEx Lib "advapi32.dll" (ByVal hKey As System.IntPtr, ByVal lpSubKey As System.String, ByVal ulOptions As System.Int32, ByVal samDesired As System.Int32, ByRef phkResult As System.Int32) As System.Int32

    Public Function OpenSubKey(ByVal ParentKey As Microsoft.Win32.RegistryKey, ByVal SubKeyName As String, ByVal Writeable As Boolean, ByVal Options As RegWow64Options) As Microsoft.Win32.RegistryKey
    If ParentKey Is Nothing OrElse GetRegistryKeyHandle(ParentKey).Equals(System.IntPtr.Zero) Then
        Throw New System.Exception("OpenSubKey: Parent key is not open")
    End If

    Dim Rights As System.Int32 = RegistryRights.ReadKey
    If Writeable Then
        Rights = RegistryRights.WriteKey
    End If

    Dim SubKeyHandle As System.Int32
    Dim Result As System.Int32 = RegOpenKeyEx(GetRegistryKeyHandle(ParentKey), SubKeyName, 0, Rights Or Options, SubKeyHandle)
    If Result <> 0 Then
        Dim W32ex As New System.ComponentModel.Win32Exception()
        Throw New System.Exception("OpenSubKey: Exception encountered opening key", W32ex)
    End If

    Return PointerToRegistryKey(CType(SubKeyHandle, System.IntPtr), Writeable, False)
    End Function

    Private Function PointerToRegistryKey(ByVal hKey As System.IntPtr, ByVal writable As Boolean, ByVal ownsHandle As Boolean) As Microsoft.Win32.RegistryKey
    ' Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
    Dim privateConstructors As System.Reflection.BindingFlags = System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic
    Dim safeRegistryHandleType As System.Type = GetType(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle")
    Dim safeRegistryHandleConstructorTypes As System.Type() = {GetType(System.IntPtr), GetType(System.Boolean)}
    Dim safeRegistryHandleConstructor As System.Reflection.ConstructorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, Nothing, safeRegistryHandleConstructorTypes, Nothing)
    Dim safeHandle As System.Object = safeRegistryHandleConstructor.Invoke(New System.Object() {hKey, ownsHandle})

    ' Create a new Registry key using the private constructor using the safeHandle - this should then behave like a .NET natively opened handle and disposed of correctly
    Dim registryKeyType As System.Type = GetType(Microsoft.Win32.RegistryKey)
    Dim registryKeyConstructorTypes As System.Type() = {safeRegistryHandleType, GetType(System.Boolean)}
    Dim registryKeyConstructor As System.Reflection.ConstructorInfo = registryKeyType.GetConstructor(privateConstructors, Nothing, registryKeyConstructorTypes, Nothing)
    Dim result As Microsoft.Win32.RegistryKey = DirectCast(registryKeyConstructor.Invoke(New Object() {safeHandle, writable}), Microsoft.Win32.RegistryKey)
    Return result
    End Function

Sample usage:

       Dim Config As Microsoft.Win32.RegistryKey = Nothing
        If Is64Bit() Then
        Config = OpenSubKey(Microsoft.Win32.Registry.LocalMachine, "SOFTWARE\" & BaseName, False, RegWow64Options.KEY_WOW64_64KEY)
        Else
        Config = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\" & BaseName, False)
        End If

Working C#.NET Code + Usage

This is essentially a C# port of the VB.NET example above.

Code:

    enum RegWow64Options
    {
        None = 0,
        KEY_WOW64_64KEY = 0x0100,
        KEY_WOW64_32KEY = 0x0200
    }

    enum RegistryRights
    {
        ReadKey = 131097,
        WriteKey = 131078
    }

    /// <summary>
    /// Open a registry key using the Wow64 node instead of the default 32-bit node.
    /// </summary>
    /// <param name="parentKey">Parent key to the key to be opened.</param>
    /// <param name="subKeyName">Name of the key to be opened</param>
    /// <param name="writable">Whether or not this key is writable</param>
    /// <param name="options">32-bit node or 64-bit node</param>
    /// <returns></returns>
    RegistryKey _openSubKey(RegistryKey parentKey, string subKeyName, bool writable, RegWow64Options options)
    {
        //Sanity check
        if (parentKey == null || _getRegistryKeyHandle(parentKey) == IntPtr.Zero)
        {
        return null;
        }

        //Set rights
        int rights = (int)RegistryRights.ReadKey;
        if (writable)
        rights = (int)RegistryRights.WriteKey;

        //Call the native function >.<
        int subKeyHandle, result = RegOpenKeyEx(_getRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)options, out subKeyHandle);

        //If we errored, throw an exception
        if (result != 0)
        {
        throw new System.ComponentModel.Win32Exception("Exception encountered opening registry key.");
        }

        //Get the key represented by the pointer returned by RegOpenKeyEx
        RegistryKey subKey = _pointerToRegistryKey((IntPtr)subKeyHandle, writable, false);
        return subKey;
    }

    /// <summary>
    /// Get a pointer to a registry key.
    /// </summary>
    /// <param name="registryKey">Registry key to obtain the pointer of.</param>
    /// <returns>Pointer to the given registry key.</returns>
    IntPtr _getRegistryKeyHandle(RegistryKey registryKey)
    {
        //Get the type of the RegistryKey
        Type registryKeyType = typeof(RegistryKey);
        //Get the FieldInfo of the 'hkey' member of RegistryKey
        System.Reflection.FieldInfo fieldInfo =
        registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        //Get the handle held by hkey
        SafeHandle handle = (SafeHandle)fieldInfo.GetValue(registryKey);
        //Get the unsafe handle
        IntPtr dangerousHandle = handle.DangerousGetHandle();
        return dangerousHandle;
    }

    /// <summary>
    /// Get a registry key from a pointer.
    /// </summary>
    /// <param name="hKey">Pointer to the registry key</param>
    /// <param name="writable">Whether or not the key is writable.</param>
    /// <param name="ownsHandle">Whether or not we own the handle.</param>
    /// <returns>Registry key pointed to by the given pointer.</returns>
    RegistryKey _pointerToRegistryKey(IntPtr hKey, bool writable, bool ownsHandle)
    {
        //Get the BindingFlags for private contructors
        System.Reflection.BindingFlags privateConstructors = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
        //Get the Type for the SafeRegistryHandle
        Type safeRegistryHandleType = typeof(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
        //Get the array of types matching the args of the ctor we want
        Type[] safeRegistryHandleCtorTypes = new Type[] { typeof(IntPtr), typeof(bool) };
        //Get the constructorinfo for our object
        System.Reflection.ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(
        privateConstructors, null, safeRegistryHandleCtorTypes, null);
        //Invoke the constructor, getting us a SafeRegistryHandle
        Object safeHandle = safeRegistryHandleCtorInfo.Invoke(new Object[] { hKey, ownsHandle });

        //Get the type of a RegistryKey
        Type registryKeyType = typeof(RegistryKey);
        //Get the array of types matching the args of the ctor we want
        Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(bool) };
        //Get the constructorinfo for our object
        System.Reflection.ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(
        privateConstructors, null, registryKeyConstructorTypes, null);
        //Invoke the constructor, getting us a RegistryKey
        RegistryKey resultKey = (RegistryKey)registryKeyCtorInfo.Invoke(new Object[] { safeHandle, writable });
        //return the resulting key
        return resultKey;
    }

    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
    public static extern int RegOpenKeyEx(IntPtr hKey, string subKey, int ulOptions, int samDesired, out int phkResult);

Usage:

    string _getWindowsProductId()
    {
        RegistryKey currentVersion =
        Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion");

        string productID = (string)currentVersion.GetValue("ProductId") ?? string.Empty;

        if (productID == string.Empty)
        {
        RegistryKey software = _openSubKey(Registry.LocalMachine, "SOFTWARE", false, RegWow64Options.KEY_WOW64_64KEY);
        RegistryKey microsoft = _openSubKey(software, "Microsoft", false, RegWow64Options.KEY_WOW64_64KEY);
        RegistryKey windowsNT = _openSubKey(microsoft, "Windows NT", false, RegWow64Options.KEY_WOW64_64KEY);
        currentVersion = _openSubKey(windowsNT, "CurrentVersion", false, RegWow64Options.KEY_WOW64_64KEY);
        productID = (string)currentVersion.GetValue("ProductId") ?? string.Empty;
        }
        return productID;
    }

Microsoft .NET Framework 4.0

All code above is for versions of .NET Framework prior to 4.0. In version 4.0, Microsoft has added support for Registry Views. To access 32-bit registry keys from a 64-bit application, here's an example:

VB.NET:

    ' HKLM\SOFTWARE
    Dim regkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry32).OpenSubKey("SOFTWARE")

C#.NET:

    ' HKLM\SOFTWARE
    Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryView.Registry32).OpenSubKey("SOFTWARE")

In both cases, examples would take you to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node due to using RegistryView.Registry32.

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions