Ask a questionAsk a question
 

AnswerReading from Registry in Win7 not compatible.

  • Friday, November 06, 2009 6:33 AMmrrcomp7 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    HI

    I have VBA code that reads and writes to the registry key. The code works fine on all versions of Windows prior to Win 7. In Win 7 the write is working fine but the read can't open the key

      ret = RegOpenKeyExA(MainKey, SubKey, 0&, KEY_READ, lpHKey)
       

    Any suggestions? HELP


Answers

  • Monday, November 09, 2009 9:50 AMRong-Chun ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello,

    When the code does not work, what error message you got?

    I perform a test on Windows 7 x86 version and it worked well on my box. Please try it and let me know whether it works for you.

    <code>
    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 Const HKEY_CURRENT_USER = &H80000001

    Private Const READ_CONTROL = &H20000
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const KEY_NOTIFY = &H10
    Private Const SYNCHRONIZE = &H100000
    Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))

    Sub test()
    Dim KeyName As String, handle As Long
    KeyName = "Software\Microsoft"
    r = RegOpenKeyEx(HKEY_CURRENT_USER, KeyName, 0, KEY_READ, handle)
    MsgBox r
    End Sub
    </code>

    Since you are reading registry keys, you might also check the key that you read to make sure the current user have the permission.

    Thanks,
    Rong-Chun Zhang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Monday, November 09, 2009 9:50 AMRong-Chun ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello,

    When the code does not work, what error message you got?

    I perform a test on Windows 7 x86 version and it worked well on my box. Please try it and let me know whether it works for you.

    <code>
    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 Const HKEY_CURRENT_USER = &H80000001

    Private Const READ_CONTROL = &H20000
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const KEY_NOTIFY = &H10
    Private Const SYNCHRONIZE = &H100000
    Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))

    Sub test()
    Dim KeyName As String, handle As Long
    KeyName = "Software\Microsoft"
    r = RegOpenKeyEx(HKEY_CURRENT_USER, KeyName, 0, KEY_READ, handle)
    MsgBox r
    End Sub
    </code>

    Since you are reading registry keys, you might also check the key that you read to make sure the current user have the permission.

    Thanks,
    Rong-Chun Zhang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Wednesday, November 11, 2009 12:08 PMRong-Chun ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    Have you got any progress on this issue? If there is anything else we can help, welcome to post here.

    Thanks,
    Rong-Chun Zhang

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.