WinAPI help

Archives Forums/Win32 Discussion/WinAPI help

em22(Posted 2009) [#1]
This may be more suited here than in the Beginners area!

Can anyone help with the way these should be used :

I found a web site ages ago that helped me understand what is required by the API but I cannot for the life figure how to use these!

I know the obvious, how to add them into Blitz+ but my source doesnt really do anything...

.lib "Advapi32.dll"

RegOpenKey%(hKeyParent%,SubKey$,phkResult*):"RegOpenKeyA"
RegCloseKey%(hKey%):"RegCloseKey"
RegFlushKey%(hKey%):"RegFlushKey"
RegCreateKey%(hKeyParent%,SubKey$,phkResult*):"RegCreateKeyA"
RegDeleteKey%(hKeyParent%,SubKey$):"RegDeleteKeyA"
RegSetValueEx%(hKey%,ValueName$,Reserved%,nType%,Bytes*,size%):"RegSetValueExA"
RegDeleteValue%(hKey%,ValueName$):"RegDeleteValueA"
RegEnumKey%(hKey%,idx%,Key*,size%):"RegEnumKeyA"
RegEnumValue%(hKey%,idx%,ValueName*,NameSize*,Reserved%,nType*,ValueBytes*,ValueSize*):"RegEnumValueA"
RegQueryValueEx%(hKey%,ValueName$,Reserved%,nType*,ValueBytes*,ValueSize*):"RegQueryValueExA"


_PJ_(Posted 2009) [#2]
I use the following decls file for advapi32.dll regisrty key stuff:

AdvApi32.decls
.lib "advapi32.dll"

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









EXAMPLE:
;REGISTRY
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
		; Only read registry strings
		If PeekInt(keyType, 0) = REG_SZ Or PeekInt(keyType, 0) = REG_EXPAND_SZ 
			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
		Return PeekInt(keyHandle, 0)
	EndIf	
	Return 0
End Function
; 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




;Example:
DeflatedProfile$="%USERPROFILE%"
keys$=K32_ExpandEnvironmentStrings(DeflatedProfile$)
Loc$=AA32_GetRegistryKey$(HKEY_USERS,".Default\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Personal")
	Docs$=(Trim$(Keys$)+Trim$(Docs$+Trim$(Replace$(Loc$,DeflatedProfile$,""))+"\"+"My Blitz Documents"))
	If (FileType(Docs$)<>2)
		CreateDir Docs$
	End If



em22(Posted 2009) [#3]
That's great thanks!