• Welcome to Valhalla Legends Archive.
 

Reading Registery keys. [VB]

Started by FuzZ, March 29, 2004, 06:37 PM

Previous topic - Next topic

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)


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


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.

Newby

I would just personally use WshShell.
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

FuzZ

#2
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.


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


Enjoy.

Eric

#3
I created a slightly incomplete Registry API class a while back that may help which can be found here.

Adron

Quote from: FuzZ on March 29, 2004, 06:37 PM
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 :)

You're supposed to set sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)

Eli_1

Quote from: FuzZ on March 29, 2004, 06:37 PM

Public Const REG_SZ = 1                         ' Unicode nul


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:

sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)


FuzZ

#6
Quote from: Eli_1 on March 30, 2004, 06:12 AM
Quote from: FuzZ on March 29, 2004, 06:37 PM

Public Const REG_SZ = 1                         ' Unicode nul


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:

sBufferStr = Left(sBufferStr, InStr(sBufferStr, Chr(0))-1)



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