Registry - How to rename key in registry using C++?











up vote
3
down vote

favorite












How to rename key in registry using C++?



I want rename key "MyappVersion1" to "MyappVersion2".



I don't see any function in MSDN about renaming keys in registry.










share|improve this question






















  • What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
    – xian
    Oct 4 '09 at 12:58















up vote
3
down vote

favorite












How to rename key in registry using C++?



I want rename key "MyappVersion1" to "MyappVersion2".



I don't see any function in MSDN about renaming keys in registry.










share|improve this question






















  • What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
    – xian
    Oct 4 '09 at 12:58













up vote
3
down vote

favorite









up vote
3
down vote

favorite











How to rename key in registry using C++?



I want rename key "MyappVersion1" to "MyappVersion2".



I don't see any function in MSDN about renaming keys in registry.










share|improve this question













How to rename key in registry using C++?



I want rename key "MyappVersion1" to "MyappVersion2".



I don't see any function in MSDN about renaming keys in registry.







c++ c winapi registry






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 4 '09 at 12:35









Jasmin25

9217




9217












  • What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
    – xian
    Oct 4 '09 at 12:58


















  • What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
    – xian
    Oct 4 '09 at 12:58
















What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58




What you could do is store the version string in the registry under MyApp. Then, instead of renaming or copying & deleting you would just have to update the value of the version string.
– xian
Oct 4 '09 at 12:58












6 Answers
6






active

oldest

votes

















up vote
4
down vote



accepted










There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.






share|improve this answer





















  • Thank you anders :)
    – Jasmin25
    Oct 4 '09 at 13:05


















up vote
7
down vote













If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().






share|improve this answer





















  • Thanks Ferruccio, I will implement that :)
    – Jasmin25
    Oct 4 '09 at 13:06


















up vote
3
down vote













if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx






share|improve this answer

















  • 1




    RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
    – c00000fd
    Apr 11 '17 at 5:42


















up vote
3
down vote













Some more info on this old entry:



NTRenameKey() is an old API for renaming a registry key.



Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.



[DllImport("advapi32")]
public static extern int RegRenameKey(
SafeRegistryHandle hKey,
[MarshalAs(UnmanagedType.LPWStr)] string oldname,
[MarshalAs(UnmanagedType.LPWStr)] string newname);





share|improve this answer




























    up vote
    2
    down vote













    I know this is an old entry, but in case anyone else comes looking, like me ...



    In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName) available from Vista onwards.






    share|improve this answer




























      up vote
      0
      down vote













      Good day my fellow programmers,



      Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:



          public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));

      [Flags]
      public enum ACCESS_MASK : uint
      {
      DELETE = 0x00010000,
      READ_CONTROL = 0x00020000,
      WRITE_DAC = 0x00040000,
      WRITE_OWNER = 0x00080000,
      SYNCHRONIZE = 0x00100000,
      STANDARD_RIGHTS_REQUIRED = 0x000F0000,
      STANDARD_RIGHTS_READ = 0x00020000,
      STANDARD_RIGHTS_WRITE = 0x00020000,
      STANDARD_RIGHTS_EXECUTE = 0x00020000,
      STANDARD_RIGHTS_ALL = 0x001F0000,
      SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
      ACCESS_SYSTEM_SECURITY = 0x01000000,
      MAXIMUM_ALLOWED = 0x02000000,
      GENERIC_READ = 0x80000000,
      GENERIC_WRITE = 0x40000000,
      GENERIC_EXECUTE = 0x20000000,
      GENERIC_ALL = 0x10000000
      }

      [Flags]
      public enum REGSAM : uint
      {
      DELETE = 0x00010000,
      READ_CONTROL = 0x00020000,
      WRITE_DAC = 0x00040000,
      WRITE_OWNER = 0x00080000,
      KEY_QUERY_VALUE = 0x0001,
      KEY_SET_VALUE = 0x0002,
      KEY_CREATE_SUB_KEY = 0x0004,
      KEY_ENUMERATE_SUB_KEYS = 0x0008,
      KEY_NOTIFY = 0x0010,
      KEY_CREATE_LINK = 0x0020,
      KEY_WOW64_32KEY = 0x0200,
      KEY_WOW64_64KEY = 0x0100,
      KEY_WOW64_RES = 0x0300,
      KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
      KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
      KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
      KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
      }

      [DllImport("advapi32.dll", SetLastError = true)]
      static extern int RegCloseKey(
      IntPtr hKey);

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

      [DllImport("ntdll.dll")]
      static extern int NtRenameKey(
      IntPtr KeyHandle,
      ref UNICODE_STRING NewName);

      [StructLayout(LayoutKind.Sequential)]
      private struct UNICODE_STRING
      {
      public ushort Length;
      public ushort MaximumLength;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string Buffer;

      public UNICODE_STRING(string value)
      {
      int len = value.Length;
      Length = (ushort) (2 * len);
      MaximumLength = (ushort) (2 * (len + 1));
      Buffer = value;
      }
      }

      public static bool RenameHklmKey(string key, string newName)
      {
      IntPtr keyHandle;
      UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
      int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
      if (0 == result)
      {
      result = NtRenameKey(keyHandle, ref unicodeString);
      RegCloseKey(keyHandle);
      }

      return 0 == result;
      }


      enums were taken from Vanara project
      Hope it helps someone






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1516312%2fregistry-how-to-rename-key-in-registry-using-c%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.






        share|improve this answer





















        • Thank you anders :)
          – Jasmin25
          Oct 4 '09 at 13:05















        up vote
        4
        down vote



        accepted










        There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.






        share|improve this answer





















        • Thank you anders :)
          – Jasmin25
          Oct 4 '09 at 13:05













        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.






        share|improve this answer












        There is no function to rename on older versions of windows, you need to copy/delete on your own AFAIK.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 4 '09 at 12:51









        Anders

        68.7k1074127




        68.7k1074127












        • Thank you anders :)
          – Jasmin25
          Oct 4 '09 at 13:05


















        • Thank you anders :)
          – Jasmin25
          Oct 4 '09 at 13:05
















        Thank you anders :)
        – Jasmin25
        Oct 4 '09 at 13:05




        Thank you anders :)
        – Jasmin25
        Oct 4 '09 at 13:05












        up vote
        7
        down vote













        If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().






        share|improve this answer





















        • Thanks Ferruccio, I will implement that :)
          – Jasmin25
          Oct 4 '09 at 13:06















        up vote
        7
        down vote













        If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().






        share|improve this answer





















        • Thanks Ferruccio, I will implement that :)
          – Jasmin25
          Oct 4 '09 at 13:06













        up vote
        7
        down vote










        up vote
        7
        down vote









        If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().






        share|improve this answer












        If your app requires Vista or newer versions of Windows, you can use RegCopyTree() followed by RegDeleteTree().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 4 '09 at 12:49









        Ferruccio

        79.2k34198284




        79.2k34198284












        • Thanks Ferruccio, I will implement that :)
          – Jasmin25
          Oct 4 '09 at 13:06


















        • Thanks Ferruccio, I will implement that :)
          – Jasmin25
          Oct 4 '09 at 13:06
















        Thanks Ferruccio, I will implement that :)
        – Jasmin25
        Oct 4 '09 at 13:06




        Thanks Ferruccio, I will implement that :)
        – Jasmin25
        Oct 4 '09 at 13:06










        up vote
        3
        down vote













        if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx






        share|improve this answer

















        • 1




          RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
          – c00000fd
          Apr 11 '17 at 5:42















        up vote
        3
        down vote













        if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx






        share|improve this answer

















        • 1




          RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
          – c00000fd
          Apr 11 '17 at 5:42













        up vote
        3
        down vote










        up vote
        3
        down vote









        if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx






        share|improve this answer












        if you need to support XP, could use SHCopyKey followed by RegDeleteKeyEx







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 8 '11 at 21:38









        Bevan Collins

        1,1671220




        1,1671220








        • 1




          RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
          – c00000fd
          Apr 11 '17 at 5:42














        • 1




          RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
          – c00000fd
          Apr 11 '17 at 5:42








        1




        1




        RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
        – c00000fd
        Apr 11 '17 at 5:42




        RegDeleteKeyEx is not available on 32-bit XP. In there you have only one option, i.e. to use RegDeleteKey
        – c00000fd
        Apr 11 '17 at 5:42










        up vote
        3
        down vote













        Some more info on this old entry:



        NTRenameKey() is an old API for renaming a registry key.



        Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.



        [DllImport("advapi32")]
        public static extern int RegRenameKey(
        SafeRegistryHandle hKey,
        [MarshalAs(UnmanagedType.LPWStr)] string oldname,
        [MarshalAs(UnmanagedType.LPWStr)] string newname);





        share|improve this answer

























          up vote
          3
          down vote













          Some more info on this old entry:



          NTRenameKey() is an old API for renaming a registry key.



          Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.



          [DllImport("advapi32")]
          public static extern int RegRenameKey(
          SafeRegistryHandle hKey,
          [MarshalAs(UnmanagedType.LPWStr)] string oldname,
          [MarshalAs(UnmanagedType.LPWStr)] string newname);





          share|improve this answer























            up vote
            3
            down vote










            up vote
            3
            down vote









            Some more info on this old entry:



            NTRenameKey() is an old API for renaming a registry key.



            Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.



            [DllImport("advapi32")]
            public static extern int RegRenameKey(
            SafeRegistryHandle hKey,
            [MarshalAs(UnmanagedType.LPWStr)] string oldname,
            [MarshalAs(UnmanagedType.LPWStr)] string newname);





            share|improve this answer












            Some more info on this old entry:



            NTRenameKey() is an old API for renaming a registry key.



            Also, Pavel has more information on the RegRenameKey() call mentioned in randomsock's answer, and (though this is a C++ question) provides this nice PInvoke signature for it.



            [DllImport("advapi32")]
            public static extern int RegRenameKey(
            SafeRegistryHandle hKey,
            [MarshalAs(UnmanagedType.LPWStr)] string oldname,
            [MarshalAs(UnmanagedType.LPWStr)] string newname);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 8 '17 at 17:58









            Aaron

            513




            513






















                up vote
                2
                down vote













                I know this is an old entry, but in case anyone else comes looking, like me ...



                In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName) available from Vista onwards.






                share|improve this answer

























                  up vote
                  2
                  down vote













                  I know this is an old entry, but in case anyone else comes looking, like me ...



                  In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName) available from Vista onwards.






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I know this is an old entry, but in case anyone else comes looking, like me ...



                    In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName) available from Vista onwards.






                    share|improve this answer












                    I know this is an old entry, but in case anyone else comes looking, like me ...



                    In this MSDN page, someone found an undocumented RegRenameKey(hKey, keyName, newKeyName) available from Vista onwards.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 25 '15 at 22:40









                    randomsock

                    42525




                    42525






















                        up vote
                        0
                        down vote













                        Good day my fellow programmers,



                        Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:



                            public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));

                        [Flags]
                        public enum ACCESS_MASK : uint
                        {
                        DELETE = 0x00010000,
                        READ_CONTROL = 0x00020000,
                        WRITE_DAC = 0x00040000,
                        WRITE_OWNER = 0x00080000,
                        SYNCHRONIZE = 0x00100000,
                        STANDARD_RIGHTS_REQUIRED = 0x000F0000,
                        STANDARD_RIGHTS_READ = 0x00020000,
                        STANDARD_RIGHTS_WRITE = 0x00020000,
                        STANDARD_RIGHTS_EXECUTE = 0x00020000,
                        STANDARD_RIGHTS_ALL = 0x001F0000,
                        SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
                        ACCESS_SYSTEM_SECURITY = 0x01000000,
                        MAXIMUM_ALLOWED = 0x02000000,
                        GENERIC_READ = 0x80000000,
                        GENERIC_WRITE = 0x40000000,
                        GENERIC_EXECUTE = 0x20000000,
                        GENERIC_ALL = 0x10000000
                        }

                        [Flags]
                        public enum REGSAM : uint
                        {
                        DELETE = 0x00010000,
                        READ_CONTROL = 0x00020000,
                        WRITE_DAC = 0x00040000,
                        WRITE_OWNER = 0x00080000,
                        KEY_QUERY_VALUE = 0x0001,
                        KEY_SET_VALUE = 0x0002,
                        KEY_CREATE_SUB_KEY = 0x0004,
                        KEY_ENUMERATE_SUB_KEYS = 0x0008,
                        KEY_NOTIFY = 0x0010,
                        KEY_CREATE_LINK = 0x0020,
                        KEY_WOW64_32KEY = 0x0200,
                        KEY_WOW64_64KEY = 0x0100,
                        KEY_WOW64_RES = 0x0300,
                        KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
                        KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
                        KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
                        KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
                        }

                        [DllImport("advapi32.dll", SetLastError = true)]
                        static extern int RegCloseKey(
                        IntPtr hKey);

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

                        [DllImport("ntdll.dll")]
                        static extern int NtRenameKey(
                        IntPtr KeyHandle,
                        ref UNICODE_STRING NewName);

                        [StructLayout(LayoutKind.Sequential)]
                        private struct UNICODE_STRING
                        {
                        public ushort Length;
                        public ushort MaximumLength;
                        [MarshalAs(UnmanagedType.LPWStr)]
                        public string Buffer;

                        public UNICODE_STRING(string value)
                        {
                        int len = value.Length;
                        Length = (ushort) (2 * len);
                        MaximumLength = (ushort) (2 * (len + 1));
                        Buffer = value;
                        }
                        }

                        public static bool RenameHklmKey(string key, string newName)
                        {
                        IntPtr keyHandle;
                        UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
                        int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
                        if (0 == result)
                        {
                        result = NtRenameKey(keyHandle, ref unicodeString);
                        RegCloseKey(keyHandle);
                        }

                        return 0 == result;
                        }


                        enums were taken from Vanara project
                        Hope it helps someone






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          Good day my fellow programmers,



                          Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:



                              public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));

                          [Flags]
                          public enum ACCESS_MASK : uint
                          {
                          DELETE = 0x00010000,
                          READ_CONTROL = 0x00020000,
                          WRITE_DAC = 0x00040000,
                          WRITE_OWNER = 0x00080000,
                          SYNCHRONIZE = 0x00100000,
                          STANDARD_RIGHTS_REQUIRED = 0x000F0000,
                          STANDARD_RIGHTS_READ = 0x00020000,
                          STANDARD_RIGHTS_WRITE = 0x00020000,
                          STANDARD_RIGHTS_EXECUTE = 0x00020000,
                          STANDARD_RIGHTS_ALL = 0x001F0000,
                          SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
                          ACCESS_SYSTEM_SECURITY = 0x01000000,
                          MAXIMUM_ALLOWED = 0x02000000,
                          GENERIC_READ = 0x80000000,
                          GENERIC_WRITE = 0x40000000,
                          GENERIC_EXECUTE = 0x20000000,
                          GENERIC_ALL = 0x10000000
                          }

                          [Flags]
                          public enum REGSAM : uint
                          {
                          DELETE = 0x00010000,
                          READ_CONTROL = 0x00020000,
                          WRITE_DAC = 0x00040000,
                          WRITE_OWNER = 0x00080000,
                          KEY_QUERY_VALUE = 0x0001,
                          KEY_SET_VALUE = 0x0002,
                          KEY_CREATE_SUB_KEY = 0x0004,
                          KEY_ENUMERATE_SUB_KEYS = 0x0008,
                          KEY_NOTIFY = 0x0010,
                          KEY_CREATE_LINK = 0x0020,
                          KEY_WOW64_32KEY = 0x0200,
                          KEY_WOW64_64KEY = 0x0100,
                          KEY_WOW64_RES = 0x0300,
                          KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
                          KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
                          KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
                          KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
                          }

                          [DllImport("advapi32.dll", SetLastError = true)]
                          static extern int RegCloseKey(
                          IntPtr hKey);

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

                          [DllImport("ntdll.dll")]
                          static extern int NtRenameKey(
                          IntPtr KeyHandle,
                          ref UNICODE_STRING NewName);

                          [StructLayout(LayoutKind.Sequential)]
                          private struct UNICODE_STRING
                          {
                          public ushort Length;
                          public ushort MaximumLength;
                          [MarshalAs(UnmanagedType.LPWStr)]
                          public string Buffer;

                          public UNICODE_STRING(string value)
                          {
                          int len = value.Length;
                          Length = (ushort) (2 * len);
                          MaximumLength = (ushort) (2 * (len + 1));
                          Buffer = value;
                          }
                          }

                          public static bool RenameHklmKey(string key, string newName)
                          {
                          IntPtr keyHandle;
                          UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
                          int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
                          if (0 == result)
                          {
                          result = NtRenameKey(keyHandle, ref unicodeString);
                          RegCloseKey(keyHandle);
                          }

                          return 0 == result;
                          }


                          enums were taken from Vanara project
                          Hope it helps someone






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Good day my fellow programmers,



                            Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:



                                public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));

                            [Flags]
                            public enum ACCESS_MASK : uint
                            {
                            DELETE = 0x00010000,
                            READ_CONTROL = 0x00020000,
                            WRITE_DAC = 0x00040000,
                            WRITE_OWNER = 0x00080000,
                            SYNCHRONIZE = 0x00100000,
                            STANDARD_RIGHTS_REQUIRED = 0x000F0000,
                            STANDARD_RIGHTS_READ = 0x00020000,
                            STANDARD_RIGHTS_WRITE = 0x00020000,
                            STANDARD_RIGHTS_EXECUTE = 0x00020000,
                            STANDARD_RIGHTS_ALL = 0x001F0000,
                            SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
                            ACCESS_SYSTEM_SECURITY = 0x01000000,
                            MAXIMUM_ALLOWED = 0x02000000,
                            GENERIC_READ = 0x80000000,
                            GENERIC_WRITE = 0x40000000,
                            GENERIC_EXECUTE = 0x20000000,
                            GENERIC_ALL = 0x10000000
                            }

                            [Flags]
                            public enum REGSAM : uint
                            {
                            DELETE = 0x00010000,
                            READ_CONTROL = 0x00020000,
                            WRITE_DAC = 0x00040000,
                            WRITE_OWNER = 0x00080000,
                            KEY_QUERY_VALUE = 0x0001,
                            KEY_SET_VALUE = 0x0002,
                            KEY_CREATE_SUB_KEY = 0x0004,
                            KEY_ENUMERATE_SUB_KEYS = 0x0008,
                            KEY_NOTIFY = 0x0010,
                            KEY_CREATE_LINK = 0x0020,
                            KEY_WOW64_32KEY = 0x0200,
                            KEY_WOW64_64KEY = 0x0100,
                            KEY_WOW64_RES = 0x0300,
                            KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
                            }

                            [DllImport("advapi32.dll", SetLastError = true)]
                            static extern int RegCloseKey(
                            IntPtr hKey);

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

                            [DllImport("ntdll.dll")]
                            static extern int NtRenameKey(
                            IntPtr KeyHandle,
                            ref UNICODE_STRING NewName);

                            [StructLayout(LayoutKind.Sequential)]
                            private struct UNICODE_STRING
                            {
                            public ushort Length;
                            public ushort MaximumLength;
                            [MarshalAs(UnmanagedType.LPWStr)]
                            public string Buffer;

                            public UNICODE_STRING(string value)
                            {
                            int len = value.Length;
                            Length = (ushort) (2 * len);
                            MaximumLength = (ushort) (2 * (len + 1));
                            Buffer = value;
                            }
                            }

                            public static bool RenameHklmKey(string key, string newName)
                            {
                            IntPtr keyHandle;
                            UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
                            int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
                            if (0 == result)
                            {
                            result = NtRenameKey(keyHandle, ref unicodeString);
                            RegCloseKey(keyHandle);
                            }

                            return 0 == result;
                            }


                            enums were taken from Vanara project
                            Hope it helps someone






                            share|improve this answer














                            Good day my fellow programmers,



                            Just in case you're trying to do it in C# (P/INVOKE), here is how I did it:



                                public static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002));

                            [Flags]
                            public enum ACCESS_MASK : uint
                            {
                            DELETE = 0x00010000,
                            READ_CONTROL = 0x00020000,
                            WRITE_DAC = 0x00040000,
                            WRITE_OWNER = 0x00080000,
                            SYNCHRONIZE = 0x00100000,
                            STANDARD_RIGHTS_REQUIRED = 0x000F0000,
                            STANDARD_RIGHTS_READ = 0x00020000,
                            STANDARD_RIGHTS_WRITE = 0x00020000,
                            STANDARD_RIGHTS_EXECUTE = 0x00020000,
                            STANDARD_RIGHTS_ALL = 0x001F0000,
                            SPECIFIC_RIGHTS_ALL = 0x0000FFFF,
                            ACCESS_SYSTEM_SECURITY = 0x01000000,
                            MAXIMUM_ALLOWED = 0x02000000,
                            GENERIC_READ = 0x80000000,
                            GENERIC_WRITE = 0x40000000,
                            GENERIC_EXECUTE = 0x20000000,
                            GENERIC_ALL = 0x10000000
                            }

                            [Flags]
                            public enum REGSAM : uint
                            {
                            DELETE = 0x00010000,
                            READ_CONTROL = 0x00020000,
                            WRITE_DAC = 0x00040000,
                            WRITE_OWNER = 0x00080000,
                            KEY_QUERY_VALUE = 0x0001,
                            KEY_SET_VALUE = 0x0002,
                            KEY_CREATE_SUB_KEY = 0x0004,
                            KEY_ENUMERATE_SUB_KEYS = 0x0008,
                            KEY_NOTIFY = 0x0010,
                            KEY_CREATE_LINK = 0x0020,
                            KEY_WOW64_32KEY = 0x0200,
                            KEY_WOW64_64KEY = 0x0100,
                            KEY_WOW64_RES = 0x0300,
                            KEY_READ = (ACCESS_MASK.STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_WRITE = (ACCESS_MASK.STANDARD_RIGHTS_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_EXECUTE = (KEY_READ) & (~ACCESS_MASK.SYNCHRONIZE),
                            KEY_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_ALL | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK) & (~ACCESS_MASK.SYNCHRONIZE),
                            }

                            [DllImport("advapi32.dll", SetLastError = true)]
                            static extern int RegCloseKey(
                            IntPtr hKey);

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

                            [DllImport("ntdll.dll")]
                            static extern int NtRenameKey(
                            IntPtr KeyHandle,
                            ref UNICODE_STRING NewName);

                            [StructLayout(LayoutKind.Sequential)]
                            private struct UNICODE_STRING
                            {
                            public ushort Length;
                            public ushort MaximumLength;
                            [MarshalAs(UnmanagedType.LPWStr)]
                            public string Buffer;

                            public UNICODE_STRING(string value)
                            {
                            int len = value.Length;
                            Length = (ushort) (2 * len);
                            MaximumLength = (ushort) (2 * (len + 1));
                            Buffer = value;
                            }
                            }

                            public static bool RenameHklmKey(string key, string newName)
                            {
                            IntPtr keyHandle;
                            UNICODE_STRING unicodeString = new UNICODE_STRING(newName);
                            int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key, 0, REGSAM.KEY_ALL_ACCESS, out keyHandle);
                            if (0 == result)
                            {
                            result = NtRenameKey(keyHandle, ref unicodeString);
                            RegCloseKey(keyHandle);
                            }

                            return 0 == result;
                            }


                            enums were taken from Vanara project
                            Hope it helps someone







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 at 8:29

























                            answered Nov 15 at 11:52









                            Gregory Stein

                            1069




                            1069






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1516312%2fregistry-how-to-rename-key-in-registry-using-c%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                Alcedinidae

                                Can an atomic nucleus contain both particles and antiparticles? [duplicate]