Results 1 to 5 of 5

Thread: GetSettingLong problems....

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I'm trying to retrieve the following key

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1

    I'm trying to use the GetSettingLong, but I get the error 'Argument not optional', when I use the following code:
    Code:
    NetWAdapter.Caption = GetSettingLong (HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1")
    Module code:

    Code:
    Option Explicit
    
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_USERS = &H80000003
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_DYN_DATA = &H80000006
    Public Const REG_SZ = 1
    Public Const REG_BINARY = 3
    Public Const REG_DWORD = 4
    Public Const ERROR_SUCCESS = 0&
    
    Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    '--------------------------------------------------
    Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData 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
    Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    
    Public Sub CreateKey(hKey As Long, strPath As String)
    Dim hCurKey As Long
    Dim lRegResult As Long
    
    lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    
    If lRegResult <> ERROR_SUCCESS Then
      ' there is a problem
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Sub
    
    Public Sub DeleteKey(ByVal hKey As Long, ByVal strPath As String)
    Dim lRegResult As Long
    
    lRegResult = RegDeleteKey(hKey, strPath)
    
    End Sub
    
    Public Sub DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
    Dim hCurKey As Long
    Dim lRegResult As Long
    
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    
    lRegResult = RegDeleteValue(hCurKey, strValue)
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Sub
    
    Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    Dim hCurKey As Long
    Dim lValueType As Long
    Dim strBuffer As String
    Dim lDataBufferSize As Long
    Dim intZeroPos As Integer
    Dim lRegResult As Long
    
    ' Set standard værdi
    If Not IsEmpty(Default) Then
      GetSettingString = Default
    Else
      GetSettingString = ""
    End If
    
    ' Åbn nøgle og få længden af strengen
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    
    If lRegResult = ERROR_SUCCESS Then
    
      If lValueType = REG_SZ Then
        ' init. string buffer og modtag streng
        strBuffer = String(lDataBufferSize, " ")
        lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
        
        ' formater streng
        intZeroPos = InStr(strBuffer, Chr$(0))
        If intZeroPos > 0 Then
          GetSettingString = Left$(strBuffer, intZeroPos - 1)
        Else
          GetSettingString = strBuffer
        End If
    
      End If
    
    Else
      ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    End Function
    
    Public Sub SaveSettingString(hKey As Long, strPath As String, strValue As String, strData As String)
    Dim hCurKey As Long
    Dim lRegResult As Long
    
    lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    
    lRegResult = RegSetValueEx(hCurKey, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    
    If lRegResult <> ERROR_SUCCESS Then
       ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    End Sub
    
    Public Function GetSettingLong(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, Optional Default As Long) As Long
    
    Dim lRegResult As Long
    Dim lValueType As Long
    Dim lBuffer As Long
    Dim lDataBufferSize As Long
    Dim hCurKey As Long
    
    ' Set standard værdi
    If Not IsEmpty(Default) Then
      GetSettingLong = Default
    Else
      GetSettingLong = 0
    End If
    
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    lDataBufferSize = 4       ' 4 bytes = 32 bits = long
    
    lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, lBuffer, lDataBufferSize)
    
    If lRegResult = ERROR_SUCCESS Then
    
      If lValueType = REG_DWORD Then
        GetSettingLong = lBuffer
      End If
    
    Else
        ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Function
    
    Public Sub SaveSettingLong(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, ByVal lData As Long)
    Dim hCurKey As Long
    Dim lRegResult As Long
    
    lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    
    lRegResult = RegSetValueEx(hCurKey, strValue, 0&, REG_DWORD, lData, 4)
    
    If lRegResult <> ERROR_SUCCESS Then
        ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    End Sub
    
    Public Function GetSettingByte(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, Optional Default As Variant) As Variant
    Dim lValueType As Long
    Dim byBuffer() As Byte
    Dim lDataBufferSize As Long
    Dim lRegResult As Long
    Dim hCurKey As Long
    
    ' set standard værdi
    If Not IsEmpty(Default) Then
      If VarType(Default) = vbArray + vbByte Then
        GetSettingByte = Default
      Else
        GetSettingByte = 0
      End If
    
    Else
      GetSettingByte = 0
    End If
    
    ' Åbn nøgle og få antallet af bytes
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, lValueType, ByVal 0&, lDataBufferSize)
    
    If lRegResult = ERROR_SUCCESS Then
    
      If lValueType = REG_BINARY Then
      
        ' Init buffere og modtag streng
        ReDim byBuffer(lDataBufferSize - 1) As Byte
        lRegResult = RegQueryValueEx(hCurKey, strValueName, 0&, lValueType, byBuffer(0), lDataBufferSize)
        
        GetSettingByte = byBuffer
    
      End If
    
    Else
        ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Function
    
    Public Sub SaveSettingByte(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, byData() As Byte)
    
    
    Dim lRegResult As Long
    Dim hCurKey As Long
    
    lRegResult = RegCreateKey(hKey, strPath, hCurKey)
    
    lRegResult = RegSetValueEx(hCurKey, strValueName, 0&, REG_BINARY, byData(0), UBound(byData()) + 1)
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Sub
    
    Public Function GetAllKeys(hKey As Long, strPath As String) As Variant
    
    Dim lRegResult As Long
    Dim lCounter As Long
    Dim hCurKey As Long
    Dim strBuffer As String
    Dim lDataBufferSize As Long
    Dim strNames() As String
    Dim intZeroPos As Integer
    
    lCounter = 0
    
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    
    Do
    
      lDataBufferSize = 255
      strBuffer = String(lDataBufferSize, " ")
      lRegResult = RegEnumKey(hCurKey, lCounter, strBuffer, lDataBufferSize)
    
      If lRegResult = ERROR_SUCCESS Then
      
        ReDim Preserve strNames(lCounter) As String
        
        intZeroPos = InStr(strBuffer, Chr$(0))
        If intZeroPos > 0 Then
          strNames(UBound(strNames)) = Left$(strBuffer, intZeroPos - 1)
        Else
          strNames(UBound(strNames)) = strBuffer
        End If
    
        lCounter = lCounter + 1
    
      Else
        Exit Do
      End If
    Loop
    
    GetAllKeys = strNames
    End Function
    
    Public Function GetAllValues(hKey As Long, strPath As String) As Variant
    
    Dim lRegResult As Long
    Dim hCurKey As Long
    Dim lValueNameSize As Long
    Dim strValueName As String
    Dim lCounter As Long
    Dim byDataBuffer(4000) As Byte
    Dim lDataBufferSize As Long
    Dim lValueType As Long
    Dim strNames() As String
    Dim lTypes() As Long
    Dim intZeroPos As Integer
    
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    
    Do
      ' Initialise bufffers
      lValueNameSize = 255
      strValueName = String$(lValueNameSize, " ")
      lDataBufferSize = 4000
      
      lRegResult = RegEnumValue(hCurKey, lCounter, strValueName, lValueNameSize, 0&, lValueType, byDataBuffer(0), lDataBufferSize)
      
      If lRegResult = ERROR_SUCCESS Then
        
        ReDim Preserve strNames(lCounter) As String
        ReDim Preserve lTypes(lCounter) As Long
        lTypes(UBound(lTypes)) = lValueType
        
        intZeroPos = InStr(strValueName, Chr$(0))
        If intZeroPos > 0 Then
          strNames(UBound(strNames)) = Left$(strValueName, intZeroPos - 1)
        Else
          strNames(UBound(strNames)) = strValueName
        End If
    
        lCounter = lCounter + 1
    
      Else
        Exit Do
      End If
    Loop
    
    Dim Finisheddata() As Variant
    ReDim Finisheddata(UBound(strNames), 0 To 1) As Variant
    
    For lCounter = 0 To UBound(strNames)
      Finisheddata(lCounter, 0) = strNames(lCounter)
      Finisheddata(lCounter, 1) = lTypes(lCounter)
    Next
    
    GetAllValues = Finisheddata
    
    End Function
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hey

    Review your code and the call:

    Code:
    Public Function GetSettingLong(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, Optional Default As Long) As Long
    It needs 3 variables and one is optional.

    You call the function like this:

    Code:
    NetWAdapter.Caption = GetSettingLong (HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1")
    You only specified 2 variables.
    So you're missing one



    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Hi again!
    What do you mean by a missing variable????
    Where do I insert the code you gave me???
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hehe

    I didn't gave you code, it are just parts of your code.

    This is the function you're calling (it's in the module, I did not wrote it myself.)
    Code:
    Public Function GetSettingLong(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, Optional Default As Long) As Long
    
    Dim lRegResult As Long
    Dim lValueType As Long
    Dim lBuffer As Long
    Dim lDataBufferSize As Long
    Dim hCurKey As Long
    
    ' Set standard værdi
    If Not IsEmpty(Default) Then
      GetSettingLong = Default
    Else
      GetSettingLong = 0
    End If
    
    lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    lDataBufferSize = 4       ' 4 bytes = 32 bits = long
    
    lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, lBuffer, lDataBufferSize)
    
    If lRegResult = ERROR_SUCCESS Then
    
      If lValueType = REG_DWORD Then
        GetSettingLong = lBuffer
      End If
    
    Else
        ' Houston, we have a problem...
    End If
    
    lRegResult = RegCloseKey(hCurKey)
    
    End Function
    Ok, now zoom in at the problem part

    Code:
    Public Function GetSettingLong(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String, Optional Default As Long) As Long
    Now you see 3 bold parts, that are the required variables:
    hKey, strPath & strValue.
    You see an italic part but it's not needed now, it's as it already says, Optional.

    Ok.


    Now let's take a look how you call the function:
    Code:
    You call this function, to output it in a label:
    NetWAdapter.Caption = GetSettingLong(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1")

    GetSettingLong = the function you're calling (it's in the module)


    HKEY_LOCAL_MACHINE = the first parameter (hKey) you pass,
    it's the 'ROOT' of the path in the registry you're looking
    for. (HKEY_LOCAL_MACHINE is a constant declared in the
    module, it has a hex value of &H80000002, but that's
    of no importance here just to ya


    "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1" = the second
    parameter (strPath) you pass, it's the path to the regkey.


    Now we see, that you're only passing 2 of the 3 required variables (hKey & strPath, so you forgot to pass strValue)


    That was all I was telling you


    Anyway, if you can't manage to work it out, download the registry class ( http://www.geocities.com/kedasu/registry.txt ) from Kedaman's homepage ( http://www.kedaman.com ) , add the module and this code and you have a working example


    Code:
    NetWAdapter.Caption = RegVal("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1"])
    Wow I like this kinda post
    I could have skipped everything execpt the last line, but I think I've helped you better with this explanation too

    People, beware of my new kind of posting!

    Ilikeit!



    [Edited by Jop on 11-29-2000 at 04:37 PM]
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I tried this, and I didn't get an error, but it returns 0 even if the string isn't empty....

    NetWAdapter.Caption = GetSettingLong(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1", "Description")
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

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