Re: problem with RegQueryValueEx (long)
- From: "Kjell" <Kjell@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 2 Jan 2006 13:22:02 -0800
Thanks Mike :-)
hmmmm, not sure I want you to examine any of my code......
(anyway, that example was not my code.)
Kjell
"MikeD" wrote:
>
> "Kjell" <Kjell@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:35C10C4B-D866-447F-BEDE-264C962BB9A1@xxxxxxxxxxxxxxxx
> > Below is a function that reads a value from Registry, my problem is that I
> > now need to read a REG_DWORD type of value, I think this is the same as
> > Hex
> > value.
> >
> > And it doesn't work, I think it has something to do that the below
> > function
> > can only read and return string values, which works fine.. (thanks to
> > Randy
> > Birch for provinding this as part of a sample from vbnet.com)
> >
> > If I remove the "And dwType = REG_SZ " part and some other stuff it still
> > doesn't work, so what do I have to do to read hex values??
> >
> > Function RegGetString(hInKey As Long, ByVal sSubKey As String, ByVal
> > sValName As String) As String
> > Dim RetVal, hSubKey As Long, dwType As Long, SZ As Long, r As Long, v$
> > Const REG_SZ = 1
> > Const KEY_QUERY_VALUE = &H1
> > Const ERROR_SUCCESS As Long = 0
> > RetVal = ""
> > r = RegOpenKeyEx(hInKey, sSubKey, 0, KEY_QUERY_VALUE, hSubKey)
> > If r <> ERROR_SUCCESS Then GoTo Quit_Now
> > SZ = 256: v$ = String$(SZ, 0)
> > r = RegQueryValueEx(hSubKey, sValName, 0, dwType, ByVal v$, SZ)
> > If r = ERROR_SUCCESS And dwType = REG_SZ Then
> > RetVal = Left(v$, SZ - 1)
> > Else
> > RetVal = ""
> > End If
> > If hSubKey <> 0 Then r = RegCloseKey(hSubKey)
> > Quit_Now:
> > RegGetString = RetVal
> > End Function
>
> No, that code above is NOT going to work for getting a DWORD value.
>
> First, dwType needs to be REG_DWORD. Second, the output buffer (you used v$
> for this) needs to be Long and the buffer size (you used SZ for this) needs
> to be 4 (because a Long is 4 bytes). Third, you should have posted the
> declaration you were using for RegQueryValueEx. You may not have that
> declared that correctly for a DWORD. The buffer, being a Long that gets
> filled by the function, needs to be passed ByRef. Fourth, I would recommend
> opening the key using KEY_READ rather than just KEY_QUERY_VALUE (which might
> be too restrictive). Fifth, get rid of that GoTo Quit_Now! Yucky, yucky,
> poo, poo. Any programmers under my direction that did that would be taking a
> class in VB 101 (and you can bet your bottom dollar I'd be conducting more
> code reviews on that programmer). <g>
>
> Anyway.....here's my Registry class module. It's got methods for
> reading/writing string values, DWORD values, Binary values, enumerating
> keys, deleting keys, and more. It's completely self-contained. It has no
> dependencies on anything. Therefore, you can either make a DLL out of it you
> want or just include it as a module in any project. Feel free to use or
> modify it however you wish.
>
> Just copy between the BEGIN CODE and END CODE and paste into Notepad. When
> you save it, be SURE it has a .cls extension (Notepad defaults new files to
> ..txt unless you include the entire file name in quotation marks).
>
> There *are* some minor inconsistencies because I wrote it over a period of
> time (about 3 years, on an as-needed basis for additional functionality.
> Also note that I never wrote the code for the DeleteKeyByEnumeration
> function (but never removed the method either) because, well, what's the
> point now? It's only necessary for NT4, and even then only if IE4 or greater
> is not installed (read the comments in the code). Giving it a quick
> look-see, I see I also never wrote the WriteBinaryValue method. I guess I've
> never needed to do that and forgot about it. Oh well. That can be an
> exercise for you. <g>
>
> ----BEGIN CODE
> VERSION 1.0 CLASS
> BEGIN
> MultiUse = -1 'True
> Persistable = 0 'NotPersistable
> DataBindingBehavior = 0 'vbNone
> DataSourceBehavior = 0 'vbNone
> MTSTransactionMode = 0 'NotAnMTSObject
> END
> Attribute VB_Name = "Registry"
> Attribute VB_GlobalNameSpace = False
> Attribute VB_Creatable = False
> Attribute VB_PredeclaredId = False
> Attribute VB_Exposed = False
> Option Explicit
>
> Private m_lError As Long
>
> 'Required for RegEnumKeyEx and RegQueryInfoKey
> Private Type FILETIME
> lLowDateTime As Long
> lHighDateTime As Long
> End Type
>
> Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias
> "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpszSubKey As String, dwOptions
> As Long, ByVal samDesired As Long, lpHKey As Long) As Long
> Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
> (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
> Private Declare Function RegCreateKey Lib "advapi32" Alias "RegCreateKeyA"
> (ByVal hKey As Long, ByVal lpszSubKey As String, phkResult 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 Long, phkResult As Long,
> lpdwDisposition As Long) As Long
> Private Declare Function RegDeleteKey Lib "advapi32" Alias "RegDeleteKeyA"
> (ByVal hKey As Long, ByVal lpszSubKey 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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
> As Long
> Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
> "RegQueryValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal
> lpdwRes As Long, lpdwType As Long, lpDataBuff As Any, nSize As Long) As Long
> Private Declare Function RegQueryValue Lib "advapi32.dll" Alias
> "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal
> lpValue As String, lpcbValue As Long) As Long
> Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias
> "RegSetValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal
> dwRes As Long, ByVal dwType As Long, lpDataBuff As Any, ByVal nSize As Long)
> As Long
> Private Declare Function RegConnectRegistry Lib "advapi32.dll" (ByVal
> lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long
> Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long)
> 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 RegEnumValue Lib "advapi32.dll" Alias
> "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As
> String, lpcbName As Long, ByVal lpReserved As Long, lpdwType As Long,
> lpValue As Any, lpcbValue As Long) As Long
> Private Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias
> "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass
> As Long, ByVal lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As
> Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As
> Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long,
> lpftLastWriteTime As FILETIME) As Long
>
> 'The following functions require Win98/2000 OR Win95/NT with IE4 or greater.
> 'Since these are relatively new API functions, they are not listed in the
> 'WIN32API.TXT file included with VB5/6.
> Private Declare Function SHDeleteKey Lib "shlwapi.dll" Alias "SHDeleteKeyA"
> (ByVal hKey As Long, ByVal pszSubKey As String) As Long
>
>
> 'Error codes returned from Registry functions.
> Const ERROR_SUCCESS As Long = 0&
> Const ERROR_BADDB As Long = 1009&
> Const ERROR_BADKEY As Long = 1010&
> Const ERROR_CANTOPEN As Long = 1011&
> Const ERROR_CANTREAD As Long = 1012&
> Const ERROR_CANTWRITE As Long = 1013&
> Const ERROR_OUTOFMEMORY As Long = 14&
> Const ERROR_INVALID_PARAMETER As Long = 87&
> Const ERROR_ACCESS_DENIED As Long = 5&
> Const ERROR_NO_MORE_ITEMS As Long = 259&
> Const ERROR_MORE_DATA As Long = 234&
>
> 'These are application-defined error codes
> Const ERROR_INVALID_HKEY_FORMAT As Long = vbObjectError + 1
> Const ERROR_INVALID_HKEY_HANDLE As Long = vbObjectError + 2
>
> 'Registry value types
> Const REG_NONE As Long = 0& ' No value type
> Const REG_SZ As Long = 1& ' Unicode nul
> terminated string
> Const REG_EXPAND_SZ As Long = 2& ' Unicode nul
> terminated string
> ' (with environment
> variable references)
> Const REG_BINARY As Long = 3& ' Free form binary
> Const REG_DWORD As Long = 4& ' 32-bit number
> Const REG_DWORD_LITTLE_ENDIAN As Long = 4& ' 32-bit number
> (same as REG_DWORD)
> Const REG_DWORD_BIG_ENDIAN As Long = 5& ' 32-bit number
> Const REG_LINK As Long = 6& ' Symbolic Link
> (unicode)
> Const REG_MULTI_SZ As Long = 7& ' Multiple Unicode
> strings
> Const REG_RESOURCE_LIST As Long = 8& ' Resource list in
> the resource map
> Const REG_FULL_RESOURCE_DESCRIPTOR As Long = 9& ' Resource list in
> the hardware description
> Const REG_RESOURCE_REQUIREMENTS_LIST As Long = 10&
>
> 'Registry Read/Write permissions:
> Const KEY_QUERY_VALUE As Long = &H1&
> Const KEY_SET_VALUE As Long = &H2&
> Const KEY_CREATE_SUB_KEY As Long = &H4&
> Const KEY_ENUMERATE_SUB_KEYS As Long = &H8&
> Const KEY_NOTIFY As Long = &H10&
> Const KEY_CREATE_LINK As Long = &H20&
> Const READ_CONTROL As Long = &H20000
> Const WRITE_DAC As Long = &H40000
> Const WRITE_OWNER As Long = &H80000
> Const SYNCHRONIZE As Long = &H100000
> Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
> Const STANDARD_RIGHTS_READ As Long = READ_CONTROL
> 'Const STANDARD_RIGHTS_WRITE As Long = READ_CONTROL
> Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
> Const STANDARD_RIGHTS_EXECUTE As Long = READ_CONTROL
> Const KEY_READ As Long = STANDARD_RIGHTS_READ Or
> KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
> Const KEY_WRITE As Long = STANDARD_RIGHTS_WRITE Or
> KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
> Const KEY_EXECUTE As Long = KEY_READ
>
> Private Const VER_PLATFORM_WIN32_NT As Long = 2
>
> Private Type OsVersionInfo
> dwVersionInfoSize As Long
> dwMajorVersion As Long
> dwMinorVersion As Long
> dwBuildNumber As Long
> dwPlatform As Long
> szCSDVersion As String * 128
> End Type
>
> Private Declare Function GetVersionEx Lib "Kernel32.dll" Alias
> "GetVersionExA" (lpStruct As OsVersionInfo) As Long
>
> Private m_bIsWinNT As Boolean
>
> Private Type DLLVERSIONINFO
> cbSize As Long
> dwMajor As Long
> dwMinor As Long
> dwBuildNumber As Long
> dwPlatformID As Long
> End Type
>
> Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA"
> (ByVal lpLibFileName As String) As Long
> Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As
> Long) As Long
> Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As
> Long, ByVal lpProcName As String) As Long
> Private Declare Function DllGetVersion Lib "Shell32" (pdvi As
> DLLVERSIONINFO) As Long
>
>
> Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
> (Destination As Any, Source As Any, ByVal Length As Long)
>
> Private Function DeleteKey9x(KeyName As String, SubKeyToDelete As String) As
> Boolean
>
> Dim hKey As Long
> Dim lRet As Long
> Dim hMainKey As Long
>
> ResetError
>
> hMainKey = GetHKEYHandle(KeyName)
>
> If hMainKey Then
> 'Open the key
> lRet = RegOpenKeyEx(hMainKey, KeyName, 0&, KEY_WRITE, hKey)
> If lRet = ERROR_SUCCESS Then
> 'Key was opened successfully; delete specified subkey
> lRet = RegDeleteKey(hKey, SubKeyToDelete)
>
> If lRet = ERROR_SUCCESS Then
> DeleteKey9x = True
> Else
> DeleteKey9x = False
> m_lError = lRet
> End If
>
> lRet = RegCloseKey(hKey)
> Else
> DeleteKey9x = False
> m_lError = lRet
> End If
> End If
>
> End Function
>
> Public Function DoesKeyExist(ByVal KeyName As String) As Boolean
>
> Dim hMainKey As Long
> Dim lRet As Long
> Dim hKey As Long
>
> ResetError
>
> hMainKey = GetHKEYHandle(KeyName)
>
> If hMainKey Then
> 'Open the key
> lRet = RegOpenKeyEx(hMainKey, KeyName, 0&, KEY_READ, hKey)
> If lRet = ERROR_SUCCESS Then
> DoesKeyExist = True
> lRet = RegCloseKey(hKey)
> Else
.
- References:
- Re: problem with RegQueryValueEx (long)
- From: MikeD
- Re: problem with RegQueryValueEx (long)
- Prev by Date: Re: Identify folder location
- Next by Date: Re: Problem with Doevents
- Previous by thread: Re: problem with RegQueryValueEx (long)
- Next by thread: Re: mscomm, can I force read only 1 byte to buffer?
- Index(es):
Relevant Pages
|