vivo vivo
vivo vivo

Reputation: 25

RegOpenKeyEx returns error 161

i'm having difficulty to rename one key of my registry. I dont know but all the time change the name so i was trying to use a program to do automatically. The code is the follow:

using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Program
    {
        private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(-2147483646);
        [DllImport("advapi32")]
        public static extern int RegRenameKey(SafeRegistryHandle hKey, [MarshalAs(UnmanagedType.LPWStr)] string oldname,
        [MarshalAs(UnmanagedType.LPWStr)] string newname);
        [DllImport("Advapi32.dll", EntryPoint = "RegOpenKeyExW", CharSet = CharSet.Unicode)]
        public static extern int RegOpenKeyEx(IntPtr hKey, [In] string lpSubKey, int ulOptions, int samDesired, out IntPtr phkResult);

        static void Main(string[] args)
        { //mhmmm si, todo esta bien , bueno ire a ver lo que iba oka  ver
            ///Estas intentando renombrar una clave de registro una la crpeta que la contiene...
            IntPtr result;

            SafeRegistryHandle hKey = null;//no es necesario, esta funcuonando, si no, no me hubiere retorando un int
            hKey = new SafeRegistryHandle(HKEY_LOCAL_MACHINE,true);
            int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "\\SOFTWARE\\Company", 0,0,out result);
            Console.WriteLine(resul);
            int rosul = RegRenameKey(hKey, "SOFTWARE\\Company\\", "SOFTWARE\\Editado\\");
            Console.WriteLine(rosul);
            Console.ReadLine(); //Ok a ver dejamever unos ejemplos de advapi, los tienes ahi? mierdaaa no tiees ideas de los peos que se acaba de tirar mi perro

        }

    }
}

The problem is when i open the key:

int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "\\SOFTWARE\\Company", 0,0,out result);

For some reason i'm not opening well the key. The error's return me:

errors

My regedit rule is not renamed:

capture of my key

Article that i took like reference:

http://blogs.microsoft.co.il/pavely/2015/09/29/regrenamekey-hidden-registry-api/

I hope someone helps... It's for my computer only.

Upvotes: 1

Views: 1779

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18310

Error 87 is ERROR_BAD_PARAMETER which indicates that one or more of the parameters you pass to the function is incorrect.

Error 161 is ERROR_BAD_PATHNAME which indicates that you've passed an incorrect path to the function.

I think both errors are caused because you're using either leading or trailing slashes in your paths. So instead of for example \\SOFTWARE\\Company you would have SOFTWARE\\Company. Try the below changes and see if it works.

Removed leading slashes:

int resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company", 0,0,out result);

Removed trailing slashes:

int rosul = RegRenameKey(hKey, "SOFTWARE\\Company", "SOFTWARE\\Editado");

References

Upvotes: 3

Related Questions