Skip to content

Commit

Permalink
Merge pull request #275 from desktop/support-expand-sz
Browse files Browse the repository at this point in the history
Add support for REG_EXPAND_SZ type
  • Loading branch information
sergiou87 authored Mar 5, 2024
2 parents b91b280 + 5cafcee commit 8183fca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export function setValue(

if (
valueType != RegistryValueType.REG_SZ &&
valueType != RegistryValueType.REG_EXPAND_SZ &&
valueType != RegistryValueType.REG_DWORD
) {
// not implemented yet
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "registry-js",
"version": "1.15.1",
"version": "1.16.0",
"description": "A simple and opinionated library for working with the Windows registry",
"main": "dist/lib/index.js",
"typings": "dist/lib/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ Napi::Value SetValue(const Napi::CallbackInfo& info)
{
long setValue = ERROR_INVALID_HANDLE;

if (wcscmp(valueType, L"REG_SZ") == 0)
if (wcscmp(valueType, L"REG_SZ") == 0 || wcscmp(valueType, L"REG_EXPAND_SZ") == 0)
{
std::string typeArg = info[4].As<Napi::String>();
auto valueData = utf8ToWideChar(typeArg);
Expand All @@ -436,11 +436,12 @@ Napi::Value SetValue(const Napi::CallbackInfo& info)
return env.Undefined();
}
int datalength = static_cast<int>(wcslen(valueData) * sizeof(valueData[0]));
DWORD regType = wcscmp(valueType, L"REG_SZ") == 0 ? REG_SZ : REG_EXPAND_SZ;
setValue = RegSetValueEx(
hOpenKey,
valueName,
0,
REG_SZ,
regType,
(const BYTE *)valueData,
datalength);
}
Expand Down
29 changes: 28 additions & 1 deletion test/registry-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if (process.platform === 'win32') {
HKEY.HKEY_CURRENT_USER,
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
'ValueTest',
RegistryValueType.REG_EXPAND_SZ,
RegistryValueType.REG_MULTI_SZ,
'Value'
)
expect(result).toBeFalsy()
Expand Down Expand Up @@ -130,6 +130,33 @@ if (process.platform === 'win32') {
expect(programFilesDir!.type).toBe('REG_SZ')
expect(programFilesDir!.data).toBe('Value 123 ! [email protected] (456)')
})

it('can set REG_EXPAND_SZ value for a registry key', () => {
let result = false
try {
result = setValue(
HKEY.HKEY_CURRENT_USER,
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion',
'ValueTestExpandSz',
RegistryValueType.REG_EXPAND_SZ,
'Value 123 ! [email protected] (456);%NVM_HOME%;%NVM_SYMLINK%'
)
} catch (e) {
console.log(e)
}
expect(result).toBeTruthy()

const values = enumerateValues(
HKEY.HKEY_CURRENT_USER,
'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
)

const value = values.find(v => v.name == 'ValueTestExpandSz')
expect(value!.type).toBe('REG_EXPAND_SZ')
expect(value!.data).toBe(
'Value 123 ! [email protected] (456);%NVM_HOME%;%NVM_SYMLINK%'
)
})
})

describe('createKey', () => {
Expand Down

0 comments on commit 8183fca

Please sign in to comment.