Results 1 to 5 of 5

Thread: [RESOLVED] Can't Read Timezones From Registry Unless Admin

  1. #1

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

    Resolved [RESOLVED] Can't Read Timezones From Registry Unless Admin

    Hello,

    I have a VB6 program that lists the available timezones and populates a combobox. Similar to the timezone combobox in the "Date and Time Properties" of windows.

    This is working fine in all versions of windows. I came upon a situation where my combobox was not being populated. After some investigation I found that the program didn't have access to this path in the registry:

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

    When I run the program as an admin the combobox gets populated correctly. If I run as the regular user the combobox doesn't get populated. My program writes and reads to its own folder in the regsitry without issue. However unless I am admin on this particular machine I can't read this path

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

    Any ideas anybody? Thanks.
    Last edited by Hassan Basri; Oct 4th, 2012 at 04:05 AM.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Can't Read Timezones From Registry Unless Admin

    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

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

    Re: Can't Read Timezones From Registry Unless Admin

    Thanks for the answer. I am already doing that but for some reason this is not working. RegOpenKeyEx fails on this machine unless I run the application with admin permissions. Here is my full function PopulateTimeZones. Let me know if you see anything that could fix this issue. Thanks.

    Code:
    Private Function PopulateTimezones(Optional blnFindMatch As Boolean, Optional strTimezone As String) As String
    
    Dim sKeyValue As String
    Dim sValue As String
    Dim lResult As Long
    Dim lIndex As Long
    Dim hKeyMain As Long
    Dim hKey As Long
    Dim lType As Long
    Dim sName As String
    Dim lngLengthStd As Long
    Dim lngLengthDisplay As Long
    
    Dim lngResultStd As Long
    Dim lngResultDisplay As Long
    
    Dim arrTimezones() As String
    Dim iCounter As Integer
    
    On Error GoTo ErrorHandler
    
    CurrentValue = ""
    
    If m_lngWindowsVersion = VER_PLATFORM_WIN32_WINDOWS Then
        strSubKey = SKEY_9X
    Else
        strSubKey = SKEY_NT
    End If
    
    ReDim arrTimezones(0)
    
    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey, 0, KEY_ALL_ACCESS, hKeyMain)
    
    If lResult = ERROR_SUCCESS Then
        lIndex = 0
        Do
            lngLengthStd = 32
            sKeyValue = String(lngLengthStd, 0)
            lResult = RegEnumKey(hKeyMain, lIndex, sKeyValue, lngLengthStd)
            
            If lResult = ERROR_SUCCESS Then
            
                ''''''''''''''''''''''''''''''''''''''''
                lngResultDisplay = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey & "\" & sKeyValue, 0, KEY_ALL_ACCESS, hKey)
                lngResultDisplay = RegQueryValueEx(hKey, "Display", 0&, lType, ByVal 0&, lngLengthStd)
                sValue = Space(lngLengthStd - 1)
                lngResultDisplay = RegQueryValueEx(hKey, "Display", 0&, lType, ByVal sValue, lngLengthStd)
                RegCloseKey hKey
                ''''''''''''''''''''''''''''''''''''''''
                lngResultStd = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey & "\" & sKeyValue, 0, KEY_ALL_ACCESS, hKey)
                lngResultStd = RegQueryValueEx(hKey, "Std", 0&, lType, ByVal 0&, lngLengthDisplay)
                sName = Space(lngLengthDisplay - 1)
                lngResultStd = RegQueryValueEx(hKey, "Std", 0&, lType, ByVal sName, lngLengthDisplay)
                RegCloseKey hKey
                ''''''''''''''''''''''''''''''''''''''''
                If (lngResultDisplay = ERROR_SUCCESS) And (lngResultStd = ERROR_SUCCESS) Then
                    If Not blnFindMatch Then
                        ReDim Preserve arrTimezones(lIndex)
                        arrTimezones(lIndex) = Trim$(Left$(sValue, lngLengthStd))
                        
                        If CurrentTimezone = Trim$(Left$(sName, lngLengthDisplay)) Then
                            CurrentValue = Left$(sValue, lngLengthStd)
                        End If
                    ElseIf sValue = strTimezone Then
                        PopulateTimezones = sKeyValue
                        RegCloseKey hKeyMain
                        Exit Function
                    End If
                End If
                
            End If
            lIndex = lIndex + 1
        Loop While lResult = ERROR_SUCCESS
        RegCloseKey hKeyMain
        
        '''''''''''''''''''''''''''''''''''''''''''''''
        Call SortArray(arrTimezones)
        For iCounter = 0 To UBound(arrTimezones)
            If arrTimezones(iCounter) <> "" Then
                cboWindowsTimezone.AddItem arrTimezones(iCounter)
            End If
        Next iCounter
    Else
        MsgBox "Error"
        PopulateTimezones = ""
    End If
    
    Exit Function
    ErrorHandler:
    PopulateTimezones = ""
    
    End Function

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Can't Read Timezones From Registry Unless Admin

    Have you tried asking for less (read only) access?

  5. #5

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

    Re: Can't Read Timezones From Registry Unless Admin

    Quote Originally Posted by dilettante View Post
    Have you tried asking for less (read only) access?
    Thanks dilettante. That did it. I changed KEY_ALL_ACCESS to KEY_READ and it now works.

    Thanks again.

    For those who need KEY_READ it is as follows:

    Code:
    Public Const READ_CONTROL As Long = &H20000
    Public Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))

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