Results 1 to 11 of 11

Thread: Getting DWORD does not work on one machine

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Getting DWORD does not work on one machine

    Hello, I am retreiving the DWORD from the registry using this code

    VB Code:
    1. Function GetSettingLong(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, Optional Default As Long) As Long
    2.  
    3. Dim lResult As Long
    4. Dim lValueType As Long
    5. Dim lBuf As Long
    6. Dim lDataBufSize As Long
    7. Dim r As Long
    8. Dim keyhand As Long
    9.  
    10. r = RegOpenKey(Hkey, strPath, keyhand)
    11.  
    12. lDataBufSize = 4
    13.    
    14. lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
    15.  
    16. If lResult = ERROR_SUCCESS Then
    17.     If lValueType = REG_DWORD Then
    18.         GetSettingLong = lBuf
    19.     End If
    20. End If
    21.  
    22. r = RegCloseKey(keyhand)
    23.    
    24. End Function

    This works perfectly on most machines that runs this function, however on an Windows XP machine this always returns 0. Even though on other XP machines this works perfectly on this XP machine it does not work even though lResult has the value ERROR_SUCCESS.

    I have looked directly in the registry and the values are there, I have sucessfully retreived REG_SZ values from that XP machine but for some reason it does not reterive the DWORD for this machine.

    The only difference I see is that it is a French version of windows. Any ideas anybody?

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Getting DWORD does not work on one machine

    Not sure, but you may need to get/set permissions to read the value: (KEY_ALL_ACCESS)
    VB Code:
    1. Option Explicit
    2.  
    3. 'Module level code.
    4.  
    5. 'Registry APIs.
    6. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    7.  
    8. Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    9.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    10.    ByVal samDesired As Long, phkResult As Long) As Long
    11.  
    12. Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    13.    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    14.    lpType As Long, lpData As Any, lpcbData As Long) As Long
    15.  
    16. 'Registry manipulation
    17. Public Const REG_OPTION_NON_VOLATILE = 0
    18. Public Const KEY_QUERY_VALUE = &H1
    19. Public Const KEY_SET_VALUE = &H2
    20. Public Const KEY_CREATE_SUB_KEY = &H4
    21. Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    22. Public Const KEY_NOTIFY = &H10
    23. Public Const KEY_CREATE_LINK = &H20
    24. Public Const STANDARD_RIGHTS_READ = &H20000
    25. Public Const STANDARD_RIGHTS_ALL = &H1F0000
    26. Public Const SYNCHRONIZE = &H100000
    27. Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or _
    28.    KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) _
    29.    And (Not SYNCHRONIZE))
    30. Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    31.  
    32. Public Function modReadDWORD(lngHKey As Long, strSubKey As String, _
    33.    strValueName As String) As Long
    34. 'Read the DWORD value.
    35.    Dim lngData As Long
    36.    Dim lngKeyHandle As Long
    37.    Dim lngDataType   As Long
    38.    Dim lngDataSize As Long
    39.  
    40. 'Open the key.
    41.    If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle) <> _
    42.       ERROR_SUCCESS Then GoTo READ_DWORD_ERROR
    43. 'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
    44.    If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) <> _
    45.       ERROR_MORE_DATA Then GoTo READ_DWORD_ERROR
    46. 'Test for DWORD value.
    47.    If lngDataType = REG_DWORD Then
    48. 'Get the key`s content.
    49.       If RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, lngData, lngDataSize) <> _
    50.          ERROR_SUCCESS Then
    51.          GoTo READ_DWORD_ERROR
    52.       Else
    53. 'Return it...
    54.          modReadDWORD = lngData
    55.       End If
    56.    End If
    57. 'Close the key.
    58.    RegCloseKey lngKeyHandle
    59. 'Exit.
    60.    Exit Function
    61. READ_DWORD_ERROR:
    62. 'Close the key.
    63.    Call RegCloseKey(lngKeyHandle)
    64. 'Time for any error handling.
    65.    MsgBox "Error in modReadDWORD"
    66. End Function

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Getting DWORD does not work on one machine

    Thank you schoolbusdriver however it didn't work. It is very strange why it wouldn't work, I am even logged on as an administrator.

    Anybody have any other ideas?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Getting DWORD does not work on one machine

    Are you sure you are looking in the right place? There are a lot of the same keys in HKLM as there are in HKCU.

  5. #5

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Getting DWORD does not work on one machine

    Yes positif, it works perfectly on lots of different machines, win95, win200, xp etc... However on this one machine (with a french version of windows xp) it does not.

    This is what I am looking for (replace COUNTRY_NAME by actual country)

    HKEY_LOCAL_MACHINE

    SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\COUNTRY_NAME

    Index

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Getting DWORD does not work on one machine

    try this

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Getting DWORD does not work on one machine

    This is also working for me.
    VB Code:
    1. Private Sub Command1_Click()
    2.     On Error Resume Next
    3.     Dim WshShell As Object, regInfo As String
    4.     Set WshShell = CreateObject("WScript.Shell")
    5.     regInfo = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\India Standard Time\Dlt")
    6.     If Len(regInfo) Then
    7.         Debug.Print regInfo
    8.     Else
    9.         If Err Then
    10.             Debug.Print "Reg Path Error"
    11.         Else
    12.             Debug.Print "Key Value Empty"
    13.         End If
    14.     End If
    15. End Sub

  8. #8

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Getting DWORD does not work on one machine

    Thank you danasegarane however I would prefer to use the API calls for maximum portability.

    I really have no idea why the API calls would not work on one machine.

  9. #9
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Getting DWORD does not work on one machine

    If the routine I posted above didn't give any errors, the only reason I can think of is that somehow the permissions for that key have changed. In regedit, if you right-click on "Time Zones" (and/or the subkeys) and pick "Permissions" you'll see a whole range of settings. As Admin, you should have full control.

  10. #10

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Getting DWORD does not work on one machine

    Thanks again schoolbusdriver, I checked I have full access, but for some reason I cannot retreive this value on this machine even though the code works perfectly on all other machines.

  11. #11
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Getting DWORD does not work on one machine

    I know this is an old thread, but found some interesting info that may go some way to explaining why the value was not retrieved, and why RegQueryValueEx can lie to you. An explanation and alternative: SHQueryValueEx

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