Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Registry - deeeep 1

Status
Not open for further replies.

vpekulas

Programmer
Jan 8, 2002
154
0
0
CA
Can somebody help me out here ?
I'm trying to read a rather long registry address but with no luck, the address is:

HKEY_CURRENT_USER\Software\Microsoft\Internet\Net2Net\Set

The key value I need is called 'SMTP' and is REG_SZ type

Is there a way to read this deep in registry ?
Thank you in advance ?
 
You need to open the key, read it then close it. Try this:

Code:
'PUT THIS IN THE DECLARATIONS SECTION:

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

Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" ( _
                    ByVal hKey As Long, _
                    ByVal lpValueName As String, _
                    ByVal lpReserved As Long, _
                    lpType As Long, _
                    ByVal lpData As String, _
                    lpcbData As Long _
                    ) As Long                             ' Note that if you declare the lpData parameter as String, you must pass it By Value.

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

Dim msKeyValue as string

Const SubKey_Name As string = "Software\Microsoft\Internet\Net2Net\Set" 
Const SubKey_Value as string = "SMTP"


'NOW 3 SUBROUTINES TO OPEN, READ AND CLOSE THE KEY
Sub OpenKey()

Dim lReturn As Long

lst.Clear
lst.AddItem "Opening the key"
lReturn = RegOpenKeyEx(HKEY_CURRENT_USER, SubKey_Name, 0, KEY_READ, mhKeyHandle)
lst.AddItem "...Return value = " & Str(lReturn)
lst.AddItem "...Key Open"
lst.AddItem "...Key handle = " & Str(mhKeyHandle)

End Sub

Sub ReadKey()
Dim lReturn As Long
Dim lLen As Long
Dim lType As Long

lLen = 200
lType = REG_SZ
msKeyValue = String(lLen, 0)
lReturn = RegQueryValueExString(mhKeyHandle, SubKey_Value, 0&, lType, msKeyValue, lLen)
lst.AddItem "...Return value = " & Str(lReturn)
msKeyValue = Left$(msKeyValue, lLen)
lst.AddItem "...Key value = " & msKeyValue

End Sub

Sub CloseKey()

Dim lReturn As Long
lst.AddItem "Closing the key"
lReturn = RegCloseKey(mhKeyHandle)
lst.AddItem "...Return value = " & Str(lReturn)
lst.AddItem "...Key closed"

End Sub
 
Hi,
Thanks for the code, it seems to be simple, but yet, no matter what address I enter I always get key value full of empty characters, to be exact it's 200 of them.
It doesn't want to read the registry.

I've tried a few address such as:
Address: HKEY_USERS\.DEFAULT\Control Panel\Current
KEY Name: Color Schemes

Yet it doesn't work. Is there something that I should be aware of before opening the registry ? It seems to be 'locked' or something.

Thank You.
 
OK - Let's do some checking.

Use Regedit to check the Mousespeed value under HKEY_CurrentUser\Control Panel\Mouse.

Set the SubKey_Name variable to "Control Panel\Mouse" and the SubKey_Value to "MouseSpeed". Run the original bit of code. Do you get the correct value back?

 
What permissions do you have on this machine? Reading the registry can't be done by Guest users.

Chip H.
 
OK Here it is:

I'm logged in as administrator on Win2K, using VB6.

I tried to get the MouseSpeed value, but with no sucess.
No matter what I do, the key value always gets back empty.
Return Value = 6; Key Handle = 0

No idea what to do :(

 
Now I remember that when accessing the registry through the Win32 API you have to have your return string full of empty characters like spaces. And then the get value procedure just places the value back into the variable you already set and ends it with a null... so make sure that you are making your string variable a good length

If I'm reading the code upstairs correctly it's this part.
Code:
lLen = 200
lType = REG_SZ
msKeyValue = String(lLen, 0)
Code:
lReturn = RegQueryValueExString(mhKeyHandle, SubKey_Value, 0&, lType, msKeyValue, lLen)
lst.AddItem "...Return value = " & Str(lReturn)
msKeyValue = Left$(msKeyValue, lLen)
lst.AddItem "...Key value = " & msKeyValue


It's sometimes overlooked....

Martin If the sky is blue and the Sun is yellow.... Why isn't the air Green?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top