2
votes

I need to create a new registry key with no default value using AutoHotkey.

The trivial solution should be:

RegWrite, REG_SZ, HKCU, Environment\testkey

leaving the value name and value fields empty. Unfortunately this creates testkey with a blank default value, i.e. an empty string, which is not what I want. I want a default value with undefined content, i.e. the same which happens when I create a new key in RegEdit (sorry for the want of terminology, I have an Italian localized OS, so I don't know how a "not set" value is displayed in English locale).

I found a workaround, which is to delete the default value just after creation with:

RegWrite, REG_SZ, HKCU, Environment\testkey
RegDelete, HKCU, Environment\testkey, AHK_DEFAULT

but it seems a dirty hack, and maybe would impact performance if I had to create many such keys.

Is there a cleaner way to achieve the same goal (maybe some way to force RegWrite not to create the blank default value, but just to create the key)?

1
Maybe I should mention that I need a solution working for WindowsXP and Windows7, although probably this information is not much relevant. - Lorenzo Donati support Ukraine

1 Answers

3
votes

Autohotkeys Build-in regwrite does as you said not seem to support a blank key.

I did a little testing and here is something for you to try out

If you leave ValueName blank the subkey's default value is used, if the sValue is omitted a blank/zero value is used.

#SingleInstance force
RegWrite("REG_SZ","HKCU","Environment\testkey","","")
return

RegWrite(Type, RKey, SKey, ValueName="",sValue="") 
{
    HKCR    := HKEY_CLASSES_ROOT    := 0x80000000   ; http://msdn.microsoft.com/en-us/library/aa393286.aspx 
    HKCU    := HKEY_CURRENT_USER    := 0x80000001 
    HKLM    := HKEY_LOCAL_MACHINE   := 0x80000002 
    HKU     := HKEY_USERS           := 0x80000003 
    HKCC    := HKEY_CURRENT_CONFIG  := 0x80000005 
    HKDD    := HKEY_DYN_DATA        := 0x80000006 
    REG_NONE                := 0                    ; http://msdn.microsoft.com/en-us/library/ms724884.aspx
    REG_SZ                  := 1
    REG_EXPAND_SZ           := 2
    REG_BINARY              := 3
    REG_DW := REG_DWORD     := 4
    REG_DWORD_BIG_ENDIAN    := 5
    REG_LINK                := 6
    REG_MULTI_SZ            := 7
    REG_RESOURCE_LIST       := 8

    if !(RKey := %RKey%)                            ; Dynamicaly assign the RootKey
        Return false                                ; A invalid rootkey was givven
    if !(Type := %Type%)                            ; Dynamicaly assign the DataType
        Return false                                ; A invalid Type was givven
    if DllCall("Advapi32.dll\RegCreateKeyEx","uint", RKey, "Str", SKey, "uint", 0   , "uint", 0 , "uint", 0, "uint",0xF003F, "uint", 0  , "uint *", hKey) { ; create the key
        Return false                                ; Error creating or opening the key 
        }       
    If (Type == REG_SZ or Type == REG_EXPAND_SZ)
                DllCall("Advapi32.dll\RegSetValueEx", "uint", hKey, "str", ValueName, "uint", 0, "uint", Type, "str", sValue, "uint", DataSize := (StrLen(sValue) + 1)*2)   ; write string
   else If (Type == REG_BINARY) {
      size:=StrLen(sValue)//2                       ; set the data to half, one byte=2 hex digits
      VarSetCapacity(Rbin, size,0)                  ; set the capacity
      loop,% size {
         StringLeft, bin, sValue,2                  ; get the left 2digits at the time
         NumPut("0x" Bin,Rbin,A_Index-1,"Char")     ; Store the data
         StringTrimLeft, sValue, sValue, 2          ; remove the to digits
         }
      DllCall("Advapi32.dll\RegSetValueEx","uint",hKey,"str",ValueName,"uint",0,"uint",Type,Uint,&Rbin,"uint",size)   ; write string
    } Else If (Type == REG_DWORD) {
        VarSetCapacity(RDW, 4,0)                    ; setthe capacity to 4 bytes
        NumPut(sValue,RDW,0)                        ; Store the data in itData
        DllCall("Advapi32.dll\RegSetValueEx","uint",hKey,"str",ValueName,"uint",0,"uint",Type,"uint",&RDW,"uint",4)   ; write dword ; a DWORD is a 32-bit (4 byte) number
        ; RDW := "" ; clear the variable
        }
        DllCall("Advapi32.dll\RegCloseKey", "uint", hKey)   ; Release the handle of the key
    return, true
}

I am on win 7 64bit using ahk 1.1.16.05 32bit unicode from http://ahkscript.org

This has worked in my tests atleast for what I think you are trying to do.