windows - 使用 AutoHotkey 创建没有默认值(非空白)的 Windows 注册表项

标签 windows registry autohotkey

我需要使用 AutoHotkey 创建一个默认值的新注册表项。

简单的解决方案应该是:

RegWrite, REG_SZ, HKCU, Environment\testkey

将值名称和值字段留空。不幸的是,这会创建带有空白默认值的testkey,即空字符串,这不是我想要的。 我想要一个带有未定义内容的默认值,即当我在 RegEdit 中创建新 key 时发生的情况相同(抱歉缺少术语,我有一个意大利本地化操作系统,所以我不知道如何在英语语言环境中显示“未设置”值)。

我找到了一个解决方法,即在创建后删除默认值:

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

但这似乎是一个肮脏的黑客行为,如果我必须创建许多这样的 key ,也许会影响性能。

是否有更简洁的方法来实现相同的目标(也许有某种方法可以强制 RegWrite 不创建空白默认值,而只是创建 key )?

最佳答案

Autohotkeys 内置 regwrite 正如您所说,似乎不支持空白键。

我做了一些测试,这里有一些东西供您尝试

如果将 ValueName 留空,则使用子项的默认值;如果省略 sValue,则使用空白/零值。

#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
}

我在 win 7 64 位上使用来自 http://ahkscript.org 的 ahk 1.1.16.05 32 位 unicode

这在我的测试中至少对我认为你想做的事情有效。

关于windows - 使用 AutoHotkey 创建没有默认值(非空白)的 Windows 注册表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26956332/

相关文章:

python - 使用 Anaconda (Python) 在 Windows 上安装 Pyomo

windows - 在批处理脚本中生成带有时间戳的唯一文件名

windows - 如何隐藏windows程序并发送点击和填写表格?

autohotkey - 如何查找 AutoHotKey 中打开的某个程序的窗口数量?

python - 在Visual Studio的Windows上安装MPIR时遇到问题吗?

.net - 处理数据库模式中的更改

Java RMI InitialContext : Equivalent of LocateRegistry. createRegistry(int)?

c# - 注册表更改权限删除其他用户权限

powershell - 区分注册表项和值路径

linux - 更改 xterm 为组合键生成的转义序列