AutoHotkey Community

It is currently May 27th, 2012, 6:57 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: October 7th, 2011, 5:00 am 
Offline

Joined: January 20th, 2007, 1:29 pm
Posts: 96
Location: Melbourne
Not sure if this is good for anyone else but i use it all the time now for using registry instead of a ini file.

The function i keep in My Documents/Autohotkey/Lib:
Code:
if A_Scriptname=reg.ahk
exitapp
Reg_add(group,prog,val,regtype="REG_SZ")
{
   valofvar:=%val%
   RegWrite, %regtype%, HKEY_CURRENT_USER,Software\%group%\%prog%,%val%,%valofvar%   
}

reg_del(group,prog,val)
{
   global
   if val
   {
   Regdelete, HKEY_CURRENT_USER,Software\%group%\%prog%,%val%
   return
   }
   else
   return
}

reg_kill(group,prog)
{
   global
   reglist_%prog%=
   RegDelete, HKEY_CURRENT_USER,Software\%group%\%prog%

}

reg_read(group,prog,val="")
{
   global
   reglist_%prog%=
   if val
   {
   Regread,value, HKEY_CURRENT_USER,Software\%group%\%prog%,%val%
   return value
   }
   Loop, HKEY_CURRENT_USER,Software\%group%\%prog%,1,1
   {
      if A_LoopRegSubKey contains backup
      continue
      if a_LoopRegType not in REG_SZ,REG_EXPAND_SZ,REG_MULTI_SZ,REG_DWORD,REG_BINARY
          continue
       else
       {
        RegRead, value
           if ErrorLevel
               value = *error*
       }
       %A_LoopRegName% := value
       reglist_%prog%.= A_LoopRegName A_Tab A_Tab value "`n"
   }
}

Reg_write(group,prog,val="",regtype="REG_SZ")
{
   global
   reglist_%prog%=
   if val
   {
   valofvar:=%val%
   RegWrite, %regtype%, HKEY_CURRENT_USER,Software\%group%\%prog%,%val%,%valofvar%
   return
   }

   Loop, HKEY_CURRENT_USER,Software\%group%\%prog%,1,1
   {
      if A_LoopRegSubKey contains backup
      continue
      if a_LoopRegType not in REG_SZ,REG_EXPAND_SZ,REG_MULTI_SZ,REG_DWORD,REG_BINARY
          continue
       else
       {
       key:=%A_LoopRegName%
        RegWrite, %Key%
       reglist_%prog%.= A_LoopRegName A_Tab A_Tab key "`n"
       }
    }
   
}
reg_display(group,prog,val="")
{
   dlist=
   Loop, HKEY_CURRENT_USER,Software\%group%\%prog%,1,1
   {
      if A_LoopRegSubKey contains backup
      continue
      if a_LoopRegType not in REG_SZ,REG_EXPAND_SZ,REG_MULTI_SZ,REG_DWORD,REG_BINARY
          continue
       else
       {
        RegRead, value
           if ErrorLevel
               value = *error*
       }
       %A_LoopRegName% := value
       dlist.= A_LoopRegName A_Tab A_Tab value "`n"
   }
return dlist   
}

and the sample i put together to show it working.
Code:
;templatesource    C:\Windows\ShellNew\Template.ahk
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance , Force
/*
Reg sample syntax
 
Reg_add(rgroup,rprog,valname)

reg_read(rgroup,rprog)                 ;reads all varnames and their values
reg_read(rgroup,rprog,valname)           ;reads just valname and its value

reg_write(rgroup,rprog)               ;writes all varnames and their values
reg_write(rgroup,rprog,valname)           ;writes just valname and its value

Reg_del(rgroup,rprog,valname)           ;deletes just valname and its value

Reg_kill(rgroup,rprog)               ;deletes rprog and all varnames and their values

Reg_Display(rgroup,rprog)            ;returns all rprog's varnames and their values

reglist_%rprog%                         ;Global  holds all variables used in registry
*/

rgroup=Trubbleguy 
rprog=regsample                     ;default used here is "HKEY_CURRENT_USER,Software\%rgroup\%rprog%"

reglist_%rprog%=
(
sample1
sample2
regcode
spozx
spozy
author
8888
)


loop,parse,reglist_%rprog%,`n
{
   %A_LoopField%:=A_Index                  ;give each regname a variable to store for this demo
      reg_add(rgroup,rprog,A_LoopField)       ;add registry items with their values
}

reglist_%rprog%=                        ;clear reglist variables to show Reg_read works
msgbox,,reglist_%rprog%, % reglist_regsample   ;show it cleared

reg_read(rgroup,rprog)
                                    ;read in registry not knowing the variables
msgbox,,reglist_%rprog%, % reglist_regsample   ;display what was found after the read in Global variable


Msgbox % Reg_display(rgroup,rprog)             ;display all reg items for "rprog" after a quick read

regcode=85889976534                        ;change regcode value

reg_write(rgroup,rprog,"regcode")            ;write changed value to reg
Msgbox % Reg_display(rgroup,rprog)             ;display all reg items for "rprog" showing the change

msgbox % reg_read(rgroup,rprog,"regcode")       ;read single value from registry and show it
 
reg_del(rgroup,rprog,"8888")               ;delete value "8888" from registry
Msgbox % Reg_display(rgroup,rprog)             ;display all reg items for "rprog" with missing 8888

reg_kill(rgroup,rprog)
Msgbox,,%rprog% Deleted, % Reg_display(rgroup,rprog) ;display all reg items for "rprog"

exitapp

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 9th, 2011, 4:37 am 
Offline

Joined: January 20th, 2007, 1:29 pm
Posts: 96
Location: Melbourne
Oops, had one line too many, left reglist_%prog%= where it shouldnt have been, fixed now.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 9th, 2011, 3:43 pm 
Offline

Joined: May 26th, 2011, 7:53 am
Posts: 237
Location: uk
Thanks for this - will come in very handy :D


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 16 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group