Results 1 to 2 of 2

Thread: Cannot Read or Create a registry entry in HKEY_LOCAL_MACHINE using advapi32.dll

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2011
    Posts
    61

    Cannot Read or Create a registry entry in HKEY_LOCAL_MACHINE using advapi32.dll

    Running on Windows 10 SP1
    Using VB6

    I have a Class called Registry.cls that handles a number of Registrty related functions. It used to work. By that I mean, I think the old Windows 7 x86 version of the app accessed the registry and located my marker thus apps ran fine. When I copied the DLL's across to x64 they still ran fine.

    However I have now cleaned my Registry of entries related to this, and also recompiled all apps on Windows 7 x64. It all works except for accessing the Registry entry.

    The entries are not being found.

    I have spent a good deal of time experimenting with a simple test app, and what I have found is that if I use the Registry functions to access HKEY_CURRENT_USER = &H80000001 it all works fine.

    However if I set my destination as HKEY_LOCAL_MACHINE = &H80000002 it does not create any registry entries, nor Read any that are manually created.

    Why would this be ?

    I have also tried compiling the test exe and running as Administrator but with the same issue.

    Odd thing is if I run a simple script using SetupFactory, it creates the registry entry fine. Just in HKEY_LOCAL_MACHINE, not in Wow6432node, however I still cannot read it using recognized VB6 code that has been working for years.

    The following is the section that fails, specifically as far as I can tell, the RegQueryValueEx

    Code:
        'Attempt to open the key
        If Len(sKeyPath) Then
            lResult = RegOpenKeyEx(lhkey, sKeyPath, 0, KEY_READ, lHandle)
            If lResult = ERROR_SUCCESS Then
                lValueLength = 1
                ReDim yBuffer(0 To lValueLength - 1)
                
                'Query the value
                lResult = RegQueryValueEx(lHandle, sValueName, ByVal 0&, lValueType, yBuffer(0), lValueLength)
                If lResult = ERROR_MORE_DATA Or lResult = ERROR_SUCCESS Then
                    'We need to check if our data buffer was large enough
                    If lResult = ERROR_MORE_DATA Then
                        ReDim yBuffer(0 To lValueLength - 1)
                        
                        'The data buffer was not large enough so we resized it and
                        'need to query the value again
                        lResult = RegQueryValueEx(lHandle, sValueName, ByVal 0&, lValueType, yBuffer(0), lValueLength)
                        If Not lResult = ERROR_SUCCESS Then
                            If Not lResult = ERROR_NO_MORE_ITEMS Then

    Here are the API Declaration in case.

    Code:
    'Registry API Declarations
    Private Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
    Private Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
    Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
    Private Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long

    In the above ;

    'Attempt to open the key ;
    Seems to work fine ? IResult = 0 and sKeyPath is correct. Ihkey = -2147483646 which I don't understand. ?
    I say "works" because the lResult = 0

    Where is fails ;
    'Query the value ;
    IHandle = 856, sValueName is correct, lValueType = 0, yBuffer(0) = 0, lValueLength = 1, lResult = 2


    I just don't get it.

    Any help would be greatly appreciated.

    Thanks

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Cannot Read or Create a registry entry in HKEY_LOCAL_MACHINE using advapi32.dll

    You might want to read about registry virtualization. I suspect that may be causing your problems. You can read about it here:-
    https://learn.microsoft.com/en-us/wi...virtualization
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width