Reading Registry Keys

BlitzPlus Forums/BlitzPlus Programming/Reading Registry Keys

I got some decls from COde Archives.. but no one worked with me..

The problem is.. i'm trying to read a subkey from my registry with Blitz. How can i do that?

By the way.. the key is: HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\mscansian@...

I need to read the value from that key and put it on a string :)


thanks for helping!

(This was far longer than I intended, but hopefully it covers everything you'll need :))

I don't have Messenger Plus installed, so I can't check, but is LogsDirectory a key or a field? I'm guessing it's a field, in which case the following should work. I'm also going to guess that the field is of type REG_EXPAND_SZ. What that means is the key will contain something like "%USERPROFILE%" as well as the path. I'm pretty sure the code in the archives doesn't handle that key type. I've included a function to sort out the paths too :)

Anyway, hopefully the code below will help out:

AdvApi32.decls
.lib "advapi32.dll"

AdvApi32_RegOpenKey%(hKey%, subKey$, result*):"RegOpenKeyA"
AdvApi32_RegCloseKey%(hKey%):"RegCloseKey"
AdvApi32_RegQueryValueEx%(hKey%, lpValueName$, lpReserved%, lpType*, lpData*, lpcbData*):"RegQueryValueExA"


Helper functions:
Const HKEY_CURRENT_USER		= $80000001
Const HKEY_LOCAL_MACHINE	= $80000002
Const HKEY_USERS		= $80000003
Const HKEY_CURRENT_CONFIG	= $80000005

Const REG_SZ			= 1
Const REG_EXPAND_SZ		= 2

Const REGISTRY_MAX_KEY_SIZE	= 255

Function AA32_GetRegistryKey$(hKey%, keyName$, fieldName$, defaultValue$ = "")
	
	; Open the key - returns empty if not found
	Local keyHandle = AA32_OpenRegistryKey(hKey, keyName)
	If keyHandle = 0 Then Return ""
	
	; Get the key contents
	Local keyType		= CreateBank(4)
	Local keyData		= CreateBank(REGISTRY_MAX_KEY_SIZE)
	Local keyDataSize	= CreateBank(4) 	: PokeInt(keyDataSize, 0, BankSize(keyData))
	Local keyValue$		= defaultValue
	
	Local result = AdvApi32_RegQueryValueEx(keyHandle, fieldName, 0, keyType, keyData, keyDataSize)
	If result = 0 Then
		
		; Only read registry strings
		If PeekInt(keyType, 0) = REG_SZ Or PeekInt(keyType, 0) = REG_EXPAND_SZ Then
			
			For i = 0 To PeekInt(keyDataSize, 0) - 2
				keyValue = keyValue + Chr(PeekByte(keyData, i))
			Next
			
		EndIf
	EndIf
	
	Return keyValue$
	
End Function


Function AA32_OpenRegistryKey(hKey%, keyName$)
	
	Local keyHandle	= CreateBank(4)
	Local result	= AdvApi32_RegOpenKey(hKey, keyName, keyHandle)
	
	If result = 0 Then
		Return PeekInt(keyHandle, 0)
	EndIf
	
	Return 0
	
End Function


And finally, an example or two :)
; Include your helper functions here

Print AA32_GetRegistryKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Themes", "InstallTheme")
Print AA32_GetRegistryKey(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\Desktop\General", "Wallpaper")


If you get a result that contains "%USERPROFILE%" or similar, the code below can be used to get the actual value:

Kernel32.decls
.lib "kernel32.dll"

Kernel32_ExpandEnvironmentStrings%(inputString$, outputBank*, maxSize%):"ExpandEnvironmentStringsA"


Helper function and examples:
; Example
print K32_ExpandEnvironmentStrings("%USERPROFILE%")

; Helper
Function K32_ExpandEnvironmentStrings$(inputString$)
	
	Local maxLength%	= CreateBank(4) : PokeInt(maxLength, 0, 255)
	Local returnBuffer	= CreateBank(PeekInt(maxLength, 0))
	Local returnSize	= 0
	Local returnString$	= ""
	
	returnSize = Kernel32_ExpandEnvironmentStrings(inputString, returnBuffer%, maxLength)
	If returnSize = 0 Then Return ""
	
	For i = 0 To returnSize - 1
		returnString = returnString + Chr(PeekByte(returnBuffer, i))
	Next
	
	Return returnString
	
End Function


Hope that helps!

Yep.. that worked :D

thks!