Unusual results writing longs to Registry (please help) 
Author Message
 Unusual results writing longs to Registry (please help)

Using Win API functions, I made some procedures for reading and writing to
the Registry. Writing strings works, but when I try to write a long integer,
the results are weird.

1 is written as 1,694,550,287
10 is written as 73,729,193
100 is written as 90,832,943
and numbers below 0 aren't allowed for some reason (error 87,
errRegInvalidParameters is returned)

I'm TOTALLY baffled. Much of the code comes from a KB article (Q145679), but
I've reshaped it in several places. I haven't made the example exactly as it
is, but I can't see much difference between its and my code...

Here's the relevant code:

Public Const Success = 0
Public Const RegString = 1
Public Const RegLong = 4

Public Sub RegWriteValue(sPath As String, _
                         sValueName As String, _
                         vData As Variant, _
                         lType As Long, _
                         Optional lRoot As Long)
On Error GoTo Exceptions
    If (lRoot = 0) Then lRoot = hKeyLocalMachine
    Dim hKey As Long
    hKey = GetKey(lRoot, sPath)
    Dim lReturn As Long

    Select Case lType
    Case RegString
        Dim sData As String
        sData = Format$(vData) & Chr$(0)
        lReturn = RegSetValueExString(hKey, sValueName, 0&, _
                                      RegString, sData, _
                                      Len(sData))
    Case RegLong
        Dim lData As Long
        lData = CLng(vData)
        lReturn = RegSetValueExLong(hKey, sValueName, 0&, _
                                    RegLong, lData, RegLong)
    Case Else
        Err.Raise 5 'invalid call
    End Select

    If (lReturn <> Success) Then Err.Raise lReturn
Cleanup:
    On Error Resume Next
    RegCloseKey hKey
    Exit Sub

Exceptions:
    If (hKey <> 0) Then RegCloseKey hKey
    Err.Raise (Err.Number)
End Sub

Private Function GetKey(lRoot As Long, _
                        sPath As String) As Long
On Error GoTo Exceptions
    Dim lAPIReturn As Long
    Dim hKey As Long
    Dim lDisposition As Long
    Dim sa As SECURITY_ATTRIBUTES
    lAPIReturn = RegCreateKeyEx(lRoot, sPath, hKey, vbNullString, _
                                regOptionVolatile, KEY_ALL_ACCESS, _
                                sa, hKey, lDisposition)
    If (lAPIReturn = Success) Then
        GetKey = hKey
    Else
        Err.Raise errKeyNotFound
    End If
    Exit Function
Exceptions:
    Err.Raise (Err.Number)
End Function



Sat, 15 Jul 2000 03:00:00 GMT  
 Unusual results writing longs to Registry (please help)

You didn't include the declares in your code so I've put my own in,  the
following code works as expected on a Win95 machine.

1 is written as 1 hex (1 Decimal)
10 is written as a hex (10 decimal)
100 is written as 64 hex (100 Decimal)
-1 is written as ffffffff hex (4294967295 Decimal)
-10 is written as fffffff6 hex (4294967286 Decimal)
-100 is written as ffffff9c hex (4294967196 Decimal)

Don't forget that this is an unsigned long that is being written so the
negative numbers "wrap over".

'**********************   Code Start  *************************
Type SECURITY_ATTRIBUTES
  nLength As Long
  lpSecurityDescriptor As Long
  bInheritHandle As Long
End Type

Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_SET_VALUE = &H2
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 Success = 0
Public Const RegString = 1
Public Const RegLong = 4

Public Const REG_OPTION_VOLATILE = 1
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_USERS = &H80000003

Declare Function RegSetValueExLong Lib "advapi32.dll" _
  Alias "RegSetValueExA" (ByVal hKey As Long, _
  ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long,
_
  lpData As Long, ByVal cbData As Long) As Long

Declare Function RegSetValueExString Lib "advapi32.dll" _
  Alias "RegSetValueExA" (ByVal hKey As Long, _
  ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long,
_
  ByVal lpData As String, ByVal cbData As Long) As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

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 SECURITY_ATTRIBUTES, phkResult As Long,
lpdwDisposition As Long) As Long

Public Sub RegWriteValue(sPath As String, _
             sValueName As String, _
             vData As Variant, _
             lType As Long, _
             Optional lRoot As Long)
On Error GoTo Exceptions
  If (lRoot = 0) Then lRoot = HKEY_LOCAL_MACHINE
  Dim hKey As Long
  hKey = GetKey(lRoot, sPath)
  Dim lReturn As Long
  Select Case lType
  Case RegString
    Dim sData As String
    sData = Format$(vData) & Chr$(0)
    lReturn = RegSetValueExString(hKey, sValueName, 0&, _
                    RegString, sData, _
                    Len(sData))
  Case RegLong
    Dim lData As Long
    lData = CLng(vData)
    lReturn = RegSetValueExLong(hKey, sValueName, 0&, _
                  RegLong, lData, RegLong)
  Case Else
    Err.Raise 5 'invalid call
  End Select
  If (lReturn <> Success) Then Err.Raise lReturn
Cleanup:
  On Error Resume Next
  RegCloseKey hKey
  Exit Sub
Exceptions:
  If (hKey <> 0) Then RegCloseKey hKey
  Err.Raise (Err.Number)
End Sub

Private Function GetKey(lRoot As Long, _
            sPath As String) As Long
On Error GoTo Exceptions
  Dim lAPIReturn As Long
  Dim hKey As Long
  Dim lDisposition As Long
  Dim sa As SECURITY_ATTRIBUTES
  lAPIReturn = RegCreateKeyEx(lRoot, sPath, hKey, vbNullString, _
                REG_OPTION_VOLATILE, KEY_ALL_ACCESS, _
                sa, hKey, lDisposition)
  If (lAPIReturn = Success) Then
    GetKey = hKey
  Else
    Err.Raise 1000 'errKeyNotFound
  End If
  Exit Function
Exceptions:
  Err.Raise (Err.Number)
End Function
'**********************   Code End  ***************************

Quote:

>Using Win API functions, I made some procedures for reading and writing to
>the Registry. Writing strings works, but when I try to write a long
integer,
>the results are weird.

>1 is written as 1,694,550,287
>10 is written as 73,729,193
>100 is written as 90,832,943
>and numbers below 0 aren't allowed for some reason (error 87,
>errRegInvalidParameters is returned)

>I'm TOTALLY baffled. Much of the code comes from a KB article (Q145679),
but
>I've reshaped it in several places. I haven't made the example exactly as
it
>is, but I can't see much difference between its and my code...

>Here's the relevant code:

.... Cut


Sun, 16 Jul 2000 03:00:00 GMT  
 Unusual results writing longs to Registry (please help)

Thanks, Terry. There was a problem with one of my Declares. I had a ByVal on
the lpData argument in RegSetValueExLong. I must've copied it from the
RegSetValueExString declare, which uses ByVal on that argument...

Adam



Sun, 16 Jul 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Unusual results writing longs to Registry (please help)

2. PLEASE HELP, PLEASE HELP, PLEASE HELP, PLEASE HELP, PLEASE HELP, PLEASE HELP, PLEASE HELP,

3. Text boxes - unusual editing facilities (please help!)

4. Unusual VB Error - Help Please

5. Registry Problems Help Please! - registry.bas (0/1)

6. Registry Problems Help Please! - registry.bas (1/1)

7. Cancelling file writing process during long writes

8. Reading Registry value, help me please,please

9. Writing a VBS file which reads and writes to the registry

10. Can anyone HELP me PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE PLEASE

11. Help: read or write to the registry

12. writing to registry..HELP

 

 
Powered by phpBB® Forum Software