-
Notifications
You must be signed in to change notification settings - Fork 12
/
registry.nim
129 lines (104 loc) · 4.45 KB
/
registry.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import winlean, os
type
HKEY* = uint
const
HKEY_CLASSES_ROOT* = HKEY(0x80000000u)
HKEY_CURRENT_USER* = HKEY(0x80000001u)
HKEY_LOCAL_MACHINE* = HKEY(0x80000002u)
HKEY_USERS* = HKEY(0x80000003u)
HKEY_PERFORMANCE_DATA* = HKEY(0x80000004u)
HKEY_CURRENT_CONFIG* = HKEY(0x80000005u)
HKEY_DYN_DATA* = HKEY(0x80000006u)
RRF_RT_ANY = 0x0000ffff
KEY_WOW64_64KEY = 0x0100
KEY_WOW64_32KEY = 0x0200
REG_SZ = 1
REG_BINARY = 3
when false:
const
REG_NONE = 0
REG_EXPAND_SZ = 2
REG_DWORD = 4
REG_DWORD_LITTLE_ENDIAN = 4
REG_DWORD_BIG_ENDIAN = 5
REG_LINK = 6
REG_MULTI_SZ = 7
REG_RESOURCE_LIST = 8
REG_FULL_RESOURCE_DESCRIPTOR = 9
REG_RESOURCE_REQUIREMENTS_LIST = 10
REG_QWORD = 11
REG_QWORD_LITTLE_ENDIAN = 11
proc regConnectRegistry(lpMachineName: WideCString,
hKey: HKEY, phkResult: var HKEY): int32 {.
importc: "RegConnectRegistryW", dynlib: "Advapi32.dll", stdcall.}
proc regCloseKey(hkey: HKEY): int32 {.
importc: "RegCloseKey", dynlib: "Advapi32.dll", stdcall.}
proc regGetValue(key: HKEY, lpSubKey, lpValue: WideCString;
dwFlags: int32 = RRF_RT_ANY, pdwType: ptr int32,
pvData: pointer,
pcbData: ptr int32): int32 {.
importc: "RegGetValueW", dynlib: "Advapi32.dll", stdcall.}
proc regSetValue(hKey: HKEY, lpSubKey: WideCString, dwType: int32,
lpData: pointer, cbData: int32): int32 {.
importc: "RegSetValueW", dynlib: "Advapi32.dll", stdcall.}
proc regSetValueEx(hKey: HKEY, lpValueName: WideCString, reserved: int32,
dwType: int32, lpData: pointer, cbData: int32): int32 {.
importc: "RegSetValueExW", dynlib: "Advapi32.dll", stdcall.}
template call(f) =
let err = f
if err != 0:
raiseOSError(err.OSErrorCode, "")
proc open*(hKey = HKEY_CURRENT_USER): HKEY =
## Opens a connection to the registry. Usually not required.
call regConnectRegistry(nil, hKey, result)
proc close*(handle: HKEY) =
## Closes the connection.
call regCloseKey(handle)
proc getUnicodeValue*(key: string; handle: HKEY = HKEY_CURRENT_USER): string =
let (a, b) = os.splitPath(key)
let hh = newWideCString a
let kk = newWideCString b
var bufsize: int32
# try a couple of different flag settings:
var flags: int32 = RRF_RT_ANY
call regGetValue(handle, hh, kk, flags, nil, nil, addr bufsize)
var res = newWideCString("", bufsize)
call regGetValue(handle, hh, kk, flags, nil, cast[pointer](res),
addr bufsize)
result = res $ bufsize
proc getBlobValue*(key: string; handle: HKEY = HKEY_CURRENT_USER): string =
let hh = newWideCString key
var bufsize: int32
# try a couple of different flag settings:
var flags: int32 = RRF_RT_ANY or 0x00010000
call regGetValue(handle, hh, nil, flags, nil, nil, addr bufsize)
result = newString(bufsize)
call regGetValue(handle, hh, nil, flags, nil, addr result[0],
addr bufsize)
proc setUnicodeValue*(key, value: string; handle: HKEY = HKEY_CURRENT_USER) =
call regSetValue(handle, newWideCString key, REG_SZ,
cast[pointer](newWideCString value), value.len.int32*2)
proc setBlobValue*(key, value: string; handle: HKEY = HKEY_CURRENT_USER) =
call regSetValueEx(handle, newWideCString key, 0, REG_BINARY,
cast[pointer](cstring(value)), value.len.int32)
proc setBlobValue*(key: string, value: pointer; valueLen: Natural;
handle: HKEY = HKEY_CURRENT_USER) =
call regSetValueEx(handle, newWideCString key, 0, REG_BINARY,
value, valueLen.int32)
when isMainModule:
#let x = open()
setUnicodeValue(r"Software\TestAndreas\ProductKey", "abc", HKEY_CURRENT_USER)
echo getUnicodeValue(r"SOFTWARE\Microsoft\Cryptography\MachineGuid",
HKEY_LOCAL_MACHINE)
echo getUnicodeValue(r"Software\TestAndreas\ProductKey") #, HKEY_CURRENT_USER)
#echo getUnicodeValue(r"hardware\acpi\facs\00000000") #, HKEY_CURRENT_USER)
#discard stdin.readline()
#echo getUnicodeValue(r"Software\Microsoft\Windows\CurrentVersion\GameInstaller")
# HKEY_CURRENT_USER)
#var data = [64'u8, 65'u8, 66'u8]
#setBlobValue(r"Software\TestAndreas\ProductID", addr data, 3)
#echo getBlobValue(r"Software\TestAndreas\ProductID")
#echo getUnicodeValue(r"Software\Microsoft\Windows\CurrentVersion\DigitalProductId")
# echo getUnicodeValue(r"SOFTWARE\Wow6432Node\Microsoft\Cryptography\MachineGuid")
#echo getValue(r"Software\Wow6432Node\7-Zip\Path")
#x.close