|
-
Oct 27th, 2006, 06:05 AM
#1
Thread Starter
Hyperactive Member
Getting DWORD does not work on one machine
Hello, I am retreiving the DWORD from the registry using this code
VB Code:
Function GetSettingLong(ByVal Hkey As Long, ByVal strPath As String, ByVal strValueName As String, Optional Default As Long) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(Hkey, strPath, keyhand)
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetSettingLong = lBuf
End If
End If
r = RegCloseKey(keyhand)
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?
-
Oct 27th, 2006, 06:55 AM
#2
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:
Option Explicit
'Module level code.
'Registry APIs.
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public 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
Public 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
'Registry manipulation
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const STANDARD_RIGHTS_READ = &H20000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const SYNCHRONIZE = &H100000
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or _
KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) _
And (Not SYNCHRONIZE))
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Function modReadDWORD(lngHKey As Long, strSubKey As String, _
strValueName As String) As Long
'Read the DWORD value.
Dim lngData As Long
Dim lngKeyHandle As Long
Dim lngDataType As Long
Dim lngDataSize As Long
'Open the key.
If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_ALL_ACCESS, lngKeyHandle) <> _
ERROR_SUCCESS Then GoTo READ_DWORD_ERROR
'Get the key`s data length. It should ALWAYS return ERROR_MORE_DATA.
If RegQueryValueEx(lngKeyHandle, strValueName, 0&, lngDataType, 0&, lngDataSize) <> _
ERROR_MORE_DATA Then GoTo READ_DWORD_ERROR
'Test for DWORD value.
If lngDataType = REG_DWORD Then
'Get the key`s content.
If RegQueryValueEx(lngKeyHandle, strValueName, 0&, 0&, lngData, lngDataSize) <> _
ERROR_SUCCESS Then
GoTo READ_DWORD_ERROR
Else
'Return it...
modReadDWORD = lngData
End If
End If
'Close the key.
RegCloseKey lngKeyHandle
'Exit.
Exit Function
READ_DWORD_ERROR:
'Close the key.
Call RegCloseKey(lngKeyHandle)
'Time for any error handling.
MsgBox "Error in modReadDWORD"
End Function
-
Oct 27th, 2006, 10:13 PM
#3
Thread Starter
Hyperactive Member
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?
-
Oct 27th, 2006, 10:31 PM
#4
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.
-
Oct 27th, 2006, 10:51 PM
#5
Thread Starter
Hyperactive Member
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
-
Oct 27th, 2006, 11:20 PM
#6
Re: Getting DWORD does not work on one machine
-
Oct 27th, 2006, 11:39 PM
#7
Re: Getting DWORD does not work on one machine
This is also working for me.
VB Code:
Private Sub Command1_Click()
On Error Resume Next
Dim WshShell As Object, regInfo As String
Set WshShell = CreateObject("WScript.Shell")
regInfo = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\India Standard Time\Dlt")
If Len(regInfo) Then
Debug.Print regInfo
Else
If Err Then
Debug.Print "Reg Path Error"
Else
Debug.Print "Key Value Empty"
End If
End If
End Sub
-
Oct 28th, 2006, 06:48 AM
#8
Thread Starter
Hyperactive Member
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.
-
Oct 28th, 2006, 01:24 PM
#9
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.
-
Nov 3rd, 2006, 12:37 PM
#10
Thread Starter
Hyperactive Member
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.
-
Nov 16th, 2006, 04:18 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|