Valhalla Legends Forums Archive | Visual Basic Programming | Reading Registery keys. [VB]

AuthorMessageTime
FuzZ
Well, I'm having a problem reading registery keys, except for Windows keys. I'm currently logged on under an Administrator account, and am trying to access HKEY_CURRENT_USER\Software\Winamp -> Default key (for obvious reasons)

[code]
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 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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002 ' even tho i dont use it.
Public Const REG_SZ = 1 ' Unicode nul terminated string

Public Function Winamp_GetPath() As String
Dim sBufferKey As Long
Dim sBufferStr As String

sBufferStr = Space$(256)

RegOpenKey HKEY_CURRENT_USER, "SOFTWARE\WINAMP", sBufferKey
RegQueryValueEx sBufferKey, "Default", 0, REG_SZ, sBufferStr, Len(sBufferStr)

Winamp_GetPath = sBufferStr
End Function
[/code]

however, this returns 256 spaces. This implies that it's not reading the key correctly. I've checked with some other examples from PSCode and didn't see anything wrong with my code. If anyone can point my error out please, feel free :)


EDIT-> Forgot to add my problem, lol
EDIT2-> Respaced function (easier on the eyes)
EDIT3-> Changed subject titled, added "[VB]" as stated in the rules.
March 30, 2004, 12:37 AM
Newby
I would just personally use WshShell.
March 30, 2004, 12:44 AM
FuzZ
Thanks, works like a charm.

For anyone else that wants to do the same, read this
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsconmanipulatingsystemregistryprogrammatically.asp
The code is basically there for you, if you know what you're doing.

Edit-> Decided I would be nice enough to post the code for you.

[code]
Public Function Winamp_GetPath() As String
Dim Sh, Path$
Set Sh = CreateObject("WScript.Shell")
Path = Sh.regread("HKEY_CURRENT_USER\Software\Winamp\")
If Right$(Path, 1) <> "\" Then
Path = Path & "\"
End If
Winamp_GetPath = Path
End Function
[/code]

Enjoy.
March 30, 2004, 1:06 AM
LoRd
I created a slightly incomplete Registry API class a while back that may help which can be found here.
March 30, 2004, 6:07 AM
Adron
[quote author=FuzZ link=board=31;threadid=6066;start=0#msg52529 date=1080607056]
however, this returns 256 spaces. This implies that it's not reading the key correctly. I've checked with some other examples from PSCode and didn't see anything wrong with my code. If anyone can point my error out please, feel free :)
[/quote]

You're supposed to set sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)
March 30, 2004, 9:36 AM
Eli_1
[quote author=FuzZ link=board=31;threadid=6066;start=0#msg52529 date=1080607056]
[code]
Public Const REG_SZ = 1 ' Unicode nul [/code]
[/quote]

Shouldn't REG_SZ be &H1 (might not help you, but eh). And like Adron said, the string is gonna be null terminated so you need to use:
[code]
sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)
[/code]
March 30, 2004, 12:12 PM
FuzZ
[quote author=Eli_1 link=board=31;threadid=6066;start=0#msg52624 date=1080648774]
[quote author=FuzZ link=board=31;threadid=6066;start=0#msg52529 date=1080607056]
[code]
Public Const REG_SZ = 1 ' Unicode nul [/code]
[/quote]

Shouldn't REG_SZ be &H1 (might not help you, but eh). And like Adron said, the string is gonna be null terminated so you need to use:
[code]
sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)
[/code]

[/quote]

That constant is directly from Windows API Viewer.

#1 there wasn't any text in it at all
#2 when it did work, it was truncated.

i might've failed to mention before it would work under some registery keys I believe it was HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
Key: RegisteredUser
April 1, 2004, 3:48 PM

Search