Using RegLoadAppKey from c# getting a failure return code of 3 after calling after the first time RRS feed

  • Question

  • Hello, I am trying to open private registry files. The first time I call the RegLoadAppKey to load the first registry file it works fine but then successive calls gives me error code 3. This only happens when I run the exe, created from my code, directly, outside of Visual studio....when I run the project inside vs 2017 it works fine (at least on my main dev machine).

    Native method wrapper:

    internal static class NativeMethods
            internal enum RegSAM
                AllAccess = 0x000f003f,
                Read = 0x20019,
                Write = 0x20006,
            [DllImport("Microsoft.VisualStudio.Setup.Configuration.Native.dll", ExactSpelling = true, PreserveSig = true)]
            internal static extern int GetSetupConfiguration([MarshalAs(UnmanagedType.Interface), Out] out ISetupConfiguration configuration, 
                                                             IntPtr reserved);
            [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern int RegLoadAppKey(String hiveFile, out int hKey, RegSAM samDesired, int options, int reserved);
            //Can't get this one to work
            //[DllImport("advapi32.dll", SetLastError = true/*, CharSet = CharSet.Unicode, EntryPoint = "RegCloseKey"/*, ExactSpelling = true*/)]
            //internal static extern int RegCloseKey(IntPtr hKey);

    Call to method:

    NativeMethods.RegLoadAppKey(regFileName, out result, NativeMethods.RegSAM.Read, 0, 0);

    I could never get this to work but I was guessing that closing the previous handle before opening a new one would of fixed it:

    NativeMethods.RegCloseKey(new IntPtr(handle));

    Any ideas?

    • Edited by TS46235 Tuesday, June 13, 2017 9:11 PM
    Tuesday, June 13, 2017 9:05 PM

All replies

  • According to the docs:

    "Any hive loaded using RegLoadAppKey is automatically unloaded when all handles to the keys inside the hive are closed using RegCloseKey."

    From the docs, error 3 seems to be:

    3 (0x3)

    The system cannot find the path specified.

    My portal and blog about VSX: http://www.visualstudioextensibility.com; Twitter: https://twitter.com/VSExtensibility; MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Wednesday, June 14, 2017 5:00 AM
  • not sure what happened but it worked on my local computer this morning. The error when I run on another machine is 183, which is the same error I was getting the last time I ran into this before I had refactored.

    183 (0xB7)

    Cannot create a file when that file already exists.

    I'm not sure exactly what its trying to create but i figured it was something to do with an open handle since I couldn't get the RegCloseKey to work....when I call it with the same handle that I received from RegLoadAppKey (when in VS debug mode) I get: 

    System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'

    Note that I receive an Int32 from RegLoadAppKey and when I call RegCloseKey it expects a pointer so I convert handle, which is an Int32, during the call:

    NativeMethods.RegCloseKey(new IntPtr(handle));

    The RegCloseKey doesnt mention that it works with RegLoadAppKey like it does from RegLoadAppKey docs:

    The handle must have been opened by the RegCreateKeyEx,RegCreateKeyTransactedRegOpenKeyExRegOpenKeyTransacted, or RegConnectRegistry function.

    Update: When I ran the standalone exe on local machine I didnt get an exception but got this instead:
    6 (0x6)

    The handle is invalid.

    ...so this must be the reason why RegCloseKey fails....it doesnt like the handle I'm passing in...must be doing it wrong.


    My code is already calling RegCloseKey via SafeRegistryHandle's dispose, so I dont even need to call RegCloseKey....there is some other problem

    using (var safeRegistryHandle = new SafeRegistryHandle(new IntPtr(registryHandle), true))
        using (var appKey = RegistryKey.FromHandle(safeRegistryHandle))
        using (var parentKey = appKey.OpenSubKey(parentKeyName, true))
            if (parentKey == null)
                throw new ApplicationException("Could not open " + parentKeyName);

    • Edited by TS46235 Wednesday, June 14, 2017 3:24 PM
    Wednesday, June 14, 2017 1:58 PM
  • Hi TS46235,

    please switch run RegUnLoadKey method, and then use RegLoadKey again, and check if it works for you.

    Best regards,

    Cole Wu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 4, 2017 7:41 AM