Skip to content

Commit 8183fca

Browse files
authored
Merge pull request #275 from desktop/support-expand-sz
Add support for REG_EXPAND_SZ type
2 parents b91b280 + 5cafcee commit 8183fca

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

lib/registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export function setValue(
173173

174174
if (
175175
valueType != RegistryValueType.REG_SZ &&
176+
valueType != RegistryValueType.REG_EXPAND_SZ &&
176177
valueType != RegistryValueType.REG_DWORD
177178
) {
178179
// not implemented yet

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "registry-js",
3-
"version": "1.15.1",
3+
"version": "1.16.0",
44
"description": "A simple and opinionated library for working with the Windows registry",
55
"main": "dist/lib/index.js",
66
"typings": "dist/lib/index.d.ts",

src/main.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ Napi::Value SetValue(const Napi::CallbackInfo& info)
426426
{
427427
long setValue = ERROR_INVALID_HANDLE;
428428

429-
if (wcscmp(valueType, L"REG_SZ") == 0)
429+
if (wcscmp(valueType, L"REG_SZ") == 0 || wcscmp(valueType, L"REG_EXPAND_SZ") == 0)
430430
{
431431
std::string typeArg = info[4].As<Napi::String>();
432432
auto valueData = utf8ToWideChar(typeArg);
@@ -436,11 +436,12 @@ Napi::Value SetValue(const Napi::CallbackInfo& info)
436436
return env.Undefined();
437437
}
438438
int datalength = static_cast<int>(wcslen(valueData) * sizeof(valueData[0]));
439+
DWORD regType = wcscmp(valueType, L"REG_SZ") == 0 ? REG_SZ : REG_EXPAND_SZ;
439440
setValue = RegSetValueEx(
440441
hOpenKey,
441442
valueName,
442443
0,
443-
REG_SZ,
444+
regType,
444445
(const BYTE *)valueData,
445446
datalength);
446447
}

test/registry-test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ if (process.platform === 'win32') {
7575
HKEY.HKEY_CURRENT_USER,
7676
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
7777
'ValueTest',
78-
RegistryValueType.REG_EXPAND_SZ,
78+
RegistryValueType.REG_MULTI_SZ,
7979
'Value'
8080
)
8181
expect(result).toBeFalsy()
@@ -130,6 +130,33 @@ if (process.platform === 'win32') {
130130
expect(programFilesDir!.type).toBe('REG_SZ')
131131
expect(programFilesDir!.data).toBe('Value 123 ! [email protected] (456)')
132132
})
133+
134+
it('can set REG_EXPAND_SZ value for a registry key', () => {
135+
let result = false
136+
try {
137+
result = setValue(
138+
HKEY.HKEY_CURRENT_USER,
139+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
140+
'ValueTestExpandSz',
141+
RegistryValueType.REG_EXPAND_SZ,
142+
'Value 123 ! [email protected] (456);%NVM_HOME%;%NVM_SYMLINK%'
143+
)
144+
} catch (e) {
145+
console.log(e)
146+
}
147+
expect(result).toBeTruthy()
148+
149+
const values = enumerateValues(
150+
HKEY.HKEY_CURRENT_USER,
151+
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
152+
)
153+
154+
const value = values.find(v => v.name == 'ValueTestExpandSz')
155+
expect(value!.type).toBe('REG_EXPAND_SZ')
156+
expect(value!.data).toBe(
157+
'Value 123 ! [email protected] (456);%NVM_HOME%;%NVM_SYMLINK%'
158+
)
159+
})
133160
})
134161

135162
describe('createKey', () => {

0 commit comments

Comments
 (0)