;============================================================ ; iron_registry.hsp — レジストリ操作モジュール ; ; Win32 API (advapi32.dll) を使用。外部 DLL 不要。 ; ; API: ; reg_read hkey, "subkey", "name" → refstr に値, stat (0=成功) ; reg_write hkey, "subkey", "name", "value" → stat (0=成功) ; reg_write_dword hkey, "subkey", "name", value → stat ; reg_delete_value hkey, "subkey", "name" → stat ; reg_delete_key hkey, "subkey" → stat ; reg_exists(hkey, "subkey", "name") → 1/0 ; ; hkey 定数: ; HKEY_CLASSES_ROOT = 0x80000000 ; HKEY_CURRENT_USER = 0x80000001 ; HKEY_LOCAL_MACHINE = 0x80000002 ; HKEY_USERS = 0x80000003 ; HKEY_CURRENT_CONFIG = 0x80000005 ; ; 例: ; #include "iron_registry.hsp" ; reg_read HKEY_CURRENT_USER, "Environment", "TEMP" ; mes "TEMP = " + refstr ;============================================================ #ifndef __iron_registry_hsp__ #define __iron_registry_hsp__ ; 定数 #const global HKEY_CLASSES_ROOT 0x80000000 #const global HKEY_CURRENT_USER 0x80000001 #const global HKEY_LOCAL_MACHINE 0x80000002 #const global HKEY_USERS 0x80000003 #const global HKEY_CURRENT_CONFIG 0x80000005 #module iron_registry ; HKEY は x64 で 8 バイトポインタなので intptr 必須。 ; int で宣言すると 4 バイトに truncate されて access violation になる。 #uselib "advapi32.dll" #cfunc _RegOpenKeyExW "RegOpenKeyExW" intptr, wstr, int, int, var #cfunc _RegCloseKey "RegCloseKey" intptr #cfunc _RegQueryValueExW "RegQueryValueExW" intptr, wstr, int, var, var, var #cfunc _RegSetValueExW "RegSetValueExW" intptr, wstr, int, int, var, int #cfunc _RegDeleteValueW "RegDeleteValueW" intptr, wstr #cfunc _RegDeleteKeyW "RegDeleteKeyW" intptr, wstr #uselib "kernel32.dll" #cfunc _WC2MB_r "WideCharToMultiByte" int, int, var, int, var, int, int, int ; KEY_READ = 0x20019, KEY_WRITE = 0x20006, KEY_ALL_ACCESS = 0xF003F #define _KEY_READ 0x20019 #define _KEY_WRITE 0x20006 #define _REG_SZ 1 #define _REG_DWORD 4 ;------------------------------------------------------------ ; reg_read hkey, subkey, name, var_result → stat (0=success) ; 結果は第4引数 (var) に格納される ; 例: reg_read HKEY_CURRENT_USER, "Environment", "TEMP", result ; if stat == 0 : mes result ;------------------------------------------------------------ #deffunc reg_read int hkey, str subkey, str name, var out_result, \ local hk, local rtype, local buf, local bsz, local ret ; HKEY は x64 で 8 バイトなので int64 で確保 (RegOpenKeyExW が 8 バイト書く) dimtype hk, 8 hk = int64(0) out_result = "" ret = _RegOpenKeyExW(hkey, subkey, 0, _KEY_READ, hk) if ret != 0 : return ret bsz = 32768 sdim buf, bsz rtype = 0 ret = _RegQueryValueExW(hk, name, 0, rtype, buf, bsz) _r = _RegCloseKey(hk) if ret != 0 : return ret ; REG_SZ: UTF-16 → UTF-8 変換 sdim _ubuf_r, 32768 _r = _WC2MB_r(65001, 0, buf, -1, _ubuf_r, 32768, 0, 0) out_result = _ubuf_r return 0 ;------------------------------------------------------------ ; reg_write hkey, subkey, name, value (文字列) ;------------------------------------------------------------ #deffunc reg_write int hkey, str subkey, str name, str value, \ local hk, local ret, local vbuf, local vlen dimtype hk, 8 hk = int64(0) ret = _RegOpenKeyExW(hkey, subkey, 0, _KEY_WRITE, hk) if ret != 0 : return ret vbuf = value vlen = strlen(vbuf) + 1 ret = _RegSetValueExW(hk, name, 0, _REG_SZ, vbuf, vlen) _r = _RegCloseKey(hk) return ret ;------------------------------------------------------------ ; reg_write_dword hkey, subkey, name, value (整数) ;------------------------------------------------------------ #deffunc reg_write_dword int hkey, str subkey, str name, int value, \ local hk, local ret, local dw dimtype hk, 8 hk = int64(0) ret = _RegOpenKeyExW(hkey, subkey, 0, _KEY_WRITE, hk) if ret != 0 : return ret dw = value ret = _RegSetValueExW(hk, name, 0, _REG_DWORD, dw, 4) _r = _RegCloseKey(hk) return ret ;------------------------------------------------------------ ; reg_delete_value ;------------------------------------------------------------ #deffunc reg_delete_value int hkey, str subkey, str name, \ local hk, local ret dimtype hk, 8 hk = int64(0) ret = _RegOpenKeyExW(hkey, subkey, 0, _KEY_WRITE, hk) if ret != 0 : return ret ret = _RegDeleteValueW(hk, name) _r = _RegCloseKey(hk) return ret ;------------------------------------------------------------ ; reg_delete_key ;------------------------------------------------------------ #deffunc reg_delete_key int hkey, str subkey return _RegDeleteKeyW(hkey, subkey) ;------------------------------------------------------------ ; reg_exists(hkey, subkey, name) → 1/0 ;------------------------------------------------------------ #defcfunc reg_exists int hkey, str subkey, str name, \ local hk, local ret, local bsz, local _rtype2, local _dummy2 dimtype hk, 8 hk = int64(0) ret = _RegOpenKeyExW(hkey, subkey, 0, _KEY_READ, hk) if ret != 0 : return 0 ; 実データは読まず存在確認のみ: _dummy2 に十分な実サイズを確保し、 ; bsz もその実サイズに合わせる (旧コードは 4B バッファに 32KB 書かせており ; スタック破壊で customstack_delete 時に AV していた) bsz = 16 _rtype2 = 0 sdim _dummy2, 16 ret = _RegQueryValueExW(hk, name, 0, _rtype2, _dummy2, bsz) _r = _RegCloseKey(hk) ; ret == 0 (成功) or ret == 234 (ERROR_MORE_DATA = バッファ不足だが値は存在) if ret == 0 : return 1 if ret == 234 : return 1 return 0 #global #endif