Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for REG_EXPAND_SZ type #275

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading