@@ -380,7 +380,7 @@ NAN_METHOD(EnumKeys) {
380
380
auto first = reinterpret_cast <HKEY>(Nan::To<int64_t >(info[0 ]).FromJust ());
381
381
382
382
HKEY hCurrentKey = first;
383
- if (argCount == 5 && !info[1 ]->IsNullOrUndefined () && !info[2 ]->IsNullOrUndefined ())
383
+ if (argCount == 5 && !info[1 ]->IsNullOrUndefined () && !info[2 ]->IsNullOrUndefined () && !info[ 3 ]-> IsNullOrUndefined () )
384
384
{
385
385
Nan::Utf8String subkeyArg (Nan::To<v8::String>(info[1 ]).ToLocalChecked ());
386
386
auto subkey = utf8ToWideChar (std::string (*subkeyArg));
@@ -411,65 +411,79 @@ NAN_METHOD(EnumKeys) {
411
411
first,
412
412
subkey,
413
413
0 ,
414
- KEY_READ | KEY_WOW64_64KEY,
414
+ KEY_WRITE | KEY_WOW64_64KEY,
415
415
&hOpenKey);
416
416
417
417
if (openKey == ERROR_FILE_NOT_FOUND)
418
418
{
419
- // the key does not exist,
420
- info.GetReturnValue ().Set (New<v8::Boolean>(false ));
419
+ Nan::ThrowTypeError (" RegOpenKeyEx : cannot find the registrykey, error_code : ERROR_FILE_NOT_FOUND" );
421
420
return ;
422
421
}
423
422
else if (openKey == ERROR_SUCCESS)
424
423
{
425
- long dwordType;
426
424
long setValue = ERROR_INVALID_HANDLE;
427
425
428
- if (valueType == L" REG_SZ" )
426
+ if (wcscmp ( valueType, L" REG_SZ" ) == 0 )
429
427
{
430
- Nan::Utf8String typeArg (Nan::To<v8::String>(info[3 ]).ToLocalChecked ());
428
+ Nan::Utf8String typeArg (Nan::To<v8::String>(info[4 ]).ToLocalChecked ());
431
429
auto valueData = utf8ToWideChar (std::string (*typeArg));
432
- dwordType = REG_SZ;
430
+ if (valueData == nullptr )
431
+ {
432
+ Nan::ThrowTypeError (" A string was expected for the fifth argument, but could not be parsed." );
433
+ return ;
434
+ }
435
+ int datalength = static_cast <int >(wcslen (valueData) * sizeof (valueData[0 ]));
433
436
setValue = RegSetValueEx (
434
437
hOpenKey,
435
438
valueName,
436
439
0 ,
437
- dwordType ,
440
+ REG_SZ ,
438
441
(const BYTE *)valueData,
439
- sizeof (valueData) );
442
+ datalength );
440
443
}
441
- else if (valueType == L" REG_DWORD" )
444
+ else if (wcscmp ( valueType, L" REG_DWORD" ) == 0 )
442
445
{
443
- auto valueData = (const DWORD *)(Nan::To<int64_t >(info[3 ]).FromJust ());
444
- dwordType = REG_DWORD;
446
+ int dwordData = Nan::To<int >(info[4 ]).FromJust ();
447
+ DWORD valueData = static_cast <DWORD>(dwordData);
448
+
445
449
setValue = RegSetValueEx (
446
450
hOpenKey,
447
451
valueName,
448
452
0 ,
449
- dwordType ,
450
- (const BYTE *)valueData,
453
+ REG_DWORD ,
454
+ (const BYTE *)& valueData,
451
455
sizeof (valueData));
452
456
}
457
+ else
458
+ {
459
+ char errorMessage[255 ];
460
+ sprintf_s (errorMessage, " RegSetValueEx Unmanaged type : '%ls'" , valueType);
461
+ Nan::ThrowTypeError (errorMessage);
462
+ return ;
463
+ }
453
464
454
465
if (setValue != ERROR_SUCCESS)
455
466
{
456
467
// FIXME: the key does not exist, just return an empty array for now
457
468
info.GetReturnValue ().Set (New<v8::Boolean>(false ));
458
469
return ;
459
470
}
471
+ info.GetReturnValue ().Set (New<v8::Boolean>(true ));
460
472
RegCloseKey (hOpenKey);
461
473
}
462
474
else
463
475
{
464
476
char errorMessage[46 ]; // 35 for message + 10 for int + 1 for nul
465
477
sprintf_s (errorMessage, " RegOpenKeyEx failed - exit code: '%d'" , openKey);
466
- Nan::ThrowError (errorMessage);
478
+ Nan::ThrowTypeError (errorMessage);
479
+ return ;
467
480
}
468
481
}
469
-
470
- info.GetReturnValue ().Set (New<v8::Boolean>(true ));
471
- if (hCurrentKey != first)
472
- RegCloseKey (hCurrentKey);
482
+ else
483
+ {
484
+ Nan::ThrowTypeError (" An argument has invalid format" );
485
+ return ;
486
+ }
473
487
}
474
488
}
475
489
0 commit comments