簡體   English   中英

使用AutoHotkey創建沒有默認值的Windows注冊表項(不為空)

[英]Creating a Windows registry key with no default value (NOT blank) using AutoHotkey

我需要使用AutoHotkey創建一個沒有默認值的新注冊表項。

簡單的解決方案應該是:

RegWrite, REG_SZ, HKCU, Environment\testkey

將值名稱和值字段留空。 不幸的是這將創建testkey一個空白的默認值,即空字符串,這是不是我想要的。 我想要一個帶有未定義內容的默認值,即在RegEdit中創建一個新鍵時發生的默認值(抱歉,由於缺乏術語,我有一個意大利語本地化的操作系統,所以我不知道“未設置”值的方式)在英語語言環境中顯示)。

我找到了一種解決方法,該方法是在創建后立即使用以下方法刪除默認值:

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

但是這似乎是一個骯臟的技巧,如果我必須創建許多這樣的密鑰,可能會影響性能。

有沒有更干凈的方法來實現相同的目標(也許是某種方式來強制RegWrite不創建空白默認值,而只是創建密鑰)?

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
}

我正在使用來自http://ahkscript.org的 ahk 1.1.16.05 32bit unicode贏得7 64bit

對於我認為您要嘗試做的事情,這至少在我的測試中起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM