Regisry Keys 
Author Message
 Regisry Keys

I need to know how to read the value of a registry key into a string
variable.  I have tried the GetSettings method, but I'm not sure if I'm
using it correctly.  I need to determine the Username of the currently
logged in user.  This is stored in
HKEY_Local_Machine\Network\Logon\Username.  If anyone can help me with this
it would be much appreciated.  Thanks, Dan.



Tue, 17 Oct 2000 03:00:00 GMT  
 Regisry Keys

Here ya go... this will read a RegVal...i understand SOME of it.. maybe one
of the VBgods here can explain it .....and tell if i left out any of the API
stuff...

Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long,
ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32" Alias
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal
lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef
lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As
Long

'------------------------------------------------------------
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As
String, ByRef KeyVal As String) As Boolean
'------------------------------------------------------------
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An
Open Registry Key
    Dim hDepth As Long                                      '
    Dim KeyValType As Long                                  ' Data Type Of A
Registry Key
    Dim tmpVal As String                                    ' Tempory
Storage For A Registry Key Value
    Dim KeyValSize As Long                                  ' Size Of
Registry Key Variable
    '------------------------------------------------------------
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    '------------------------------------------------------------
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open
Registry Key

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle
Error...

    tmpVal = String$(1024, 0)                               ' Allocate
Variable Space
    KeyValSize = 1024                                       ' Mark Variable
Size

    '------------------------------------------------------------
    ' Retrieve Registry Key Value...
    '------------------------------------------------------------
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         KeyValType, tmpVal, KeyValSize)    ' Get/Create Key
Value

    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors

    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then           ' Win95 Adds
Null Terminated String...
        tmpVal = Left(tmpVal, KeyValSize - 1)               ' Null Found,
Extract From String
    Else                                                    ' WinNT Does NOT
Null Terminate String...
        tmpVal = Left(tmpVal, KeyValSize)                   ' Null Not
Found, Extract String Only
    End If
    '------------------------------------------------------------
    ' Determine Key Value Type For Conversion...
    '------------------------------------------------------------
    Select Case KeyValType                                  ' Search Data
Types...
    Case REG_SZ                                             ' String
Registry Key Data Type
        KeyVal = tmpVal                                     ' Copy String
Value
    Case REG_DWORD                                          ' Double Word
Registry Key Data Type
        For i = Len(tmpVal) To 1 Step -1                    ' Convert Each
Bit
            KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value
Char. By Char.
        Next
        KeyVal = Format$("&h" + KeyVal)                     ' Convert Double
Word To String
    End Select

    GetKeyValue = True                                      ' Return Success
    rc = RegCloseKey(hKey)                                  ' Close Registry
Key
    Exit Function                                           ' Exit
'------------------------------------------------------------
GetKeyError:    ' Cleanup After An Error Has Occured...
'------------------------------------------------------------
    KeyVal = ""                                             ' Set Return Val
To Empty String
    GetKeyValue = False                                     ' Return Failure
    rc = RegCloseKey(hKey)                                  ' Close Registry
Key
'------------------------------------------------------------
End Function
'------------------------------------------------------------

Quote:

>I need to know how to read the value of a registry key into a string
>variable.  I have tried the GetSettings method, but I'm not sure if I'm
>using it correctly.  I need to determine the Username of the currently
>logged in user.  This is stored in
>HKEY_Local_Machine\Network\Logon\Username.  If anyone can help me with this
>it would be much appreciated.  Thanks, Dan.



Wed, 18 Oct 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Regisry editing

2. Reading Regisry

3. Bypass Key (Shift Key)

4. Assigning arrow keys as hot keys

5. Changing the AllowbyPass key from shift to some other Key

6. WD 2000 - Enter Key to act like Tab Key

7. Enter key equals Tab key

8. Enter key equals Tab key

9. WD 2000 - Enter Key to act like Tab Key

10. Difference between Keys.Control and Keys.ControlKey

11. Key Down and Key press

12. find the specify Root key and Sub key from the registry

 

 
Powered by phpBB® Forum Software