I had been looking for a project to write so that I could learn how to use COM objects, and after reading
this post, I searched around and found
this registry API and
this method of forcing it to use either the 32 bit or 64 bit registry. So with this class, you can access both 32 bit and 64 bit registries from either 32 bit or 64 bit AHK EXEs. I just started writing this yesterday and I haven't done much testing besides the sample code below, but everything seems to be working. The only functions from the API that I don't have implemented are GetSecurityDescriptor and SetSecurityDescriptor.
Registry.ahk
Code:
#Include <Registry>
reg := new Registry
reg.mode := 32 ;32 or 64 bit mode (default is 64), has no effect on 32-bit Windows
reg.tree := Registry.trees.HKLM ;tree defaults to HKLM if not set
;checking access permissions
reg.subkey := "Software"
access := reg.CheckAccess(Registry.permissions.KEY_CREATE_SUB_KEY) ;see if we have access to create a new key in "Software"
MsgBox, % access ? "Access granted." : "Access denied."
If (!access) { ;run as admin if we don't have access
Run, *RunAs "%A_ScriptFullPath%"
ExitApp
}
;creating keys and setting values
reg.subkey := "Software\AutoHotkey Registry Test" ;all of the values below will be placed in this subkey
reg.CreateKey() ;create the subkey defined above
reg.SetBinaryValue("BinaryTest", [0xff, 0x0, 0x15]) ;binary values are set as an array of bytes
reg.SetDWORDValue("DWORDTest", 123456)
reg.SetExpandedStringValue("ExpandedStringTest", "%Processor_Architecture%")
reg.SetMultiStringValue("MultiStringTest", ["test1", "test2", "test3"]) ;multi strings are set as an array of strings
reg.SetQWORDValue("QWORDTest", 789456) ;QWORDS require at least Windows Vista
reg.SetStringValue("StringTest", "stringtext")
reg.SetStringValue("", "This is the default.") ;an empty string sets the default value
;you can also specify the subkey in the last parameter in the function
reg.CreateKey(reg.subkey . "\Test Subkey")
reg.SetStringValue("test", "this is inside ""\Test Subkey""", reg.subkey . "\Test Subkey")
;reading values
for i, byte in reg.GetBinaryValue("BinaryTest") ;binary values are returned as an array of bytes
text .= byte . "|"
text .= "`n" . reg.GetDWORDValue("DWORDTest") . "`n"
text .= reg.GetExpandedStringValue("ExpandedStringTest") . "`n"
for i, string in reg.GetMultiStringValue("MultiStringTest") ;multi string values are returned as an array of strings
text .= string . "|"
text .= "`n" . reg.GetQWORDValue("QWORDTest") . "`n"
text .= reg.GetStringValue("StringTest") . "`n"
text .= reg.GetStringValue("") ;reading the default value
MsgBox, % text
;enumerating keys and values
text2 := "SubKeys of " . reg.subkey . ":`n"
for i, subkey in reg.EnumKey()
text2 .= A_Tab . subkey . "`n"
text2 .= "`nDWORD and QWORD values of " . reg.subkey . ":`n"
for name, type in reg.EnumValues() {
if ((type = Registry.types.REG_DWORD) or (type = Registry.types.REG_QWORD))
text2 .= A_Tab . name . "`n"
}
MsgBox, % text2
;Run, regedit
;Pause
;deleting
reg.DeleteValue("StringTest")
for i, key in reg.EnumKey() ;all subkeys must be deleted before you can delete a key
reg.DeleteKey(reg.subkey . "\" . key)
reg.DeleteKey()