Merge tag 'v1.1.10'

This commit is contained in:
Baptiste Augrain 2025-03-04 12:06:21 +01:00
commit f39ea93244
No known key found for this signature in database
GPG Key ID: D0F9263E966FE50B
6 changed files with 54 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@vscodium/policy-watcher",
"version": "1.1.8-2501211616",
"version": "1.1.10-2503041159",
"description": "",
"main": "index.js",
"repository": {

View File

@ -8,9 +8,9 @@
using namespace Napi;
NumberPolicy::NumberPolicy(const std::string name, const std::string &vendorName, const std::string &productName)
: RegistryPolicy(name, vendorName, productName, REG_QWORD) {}
: RegistryPolicy(name, vendorName, productName, {REG_QWORD}) {}
long long NumberPolicy::parseRegistryValue(LPBYTE buffer, DWORD bufferSize) const
long long NumberPolicy::parseRegistryValue(LPBYTE buffer, DWORD bufferSize, DWORD type) const
{
return *reinterpret_cast<long long *>(buffer);
}

View File

@ -18,7 +18,7 @@ public:
NumberPolicy(const std::string name, const std::string &vendorName, const std::string &productName);
protected:
long long parseRegistryValue(LPBYTE buffer, DWORD bufferSize) const;
long long parseRegistryValue(LPBYTE buffer, DWORD bufferSize, DWORD type) const;
Value getJSValue(Env env, long long value) const;
};

View File

@ -9,6 +9,8 @@
#include <napi.h>
#include <windows.h>
#include <optional>
#include <vector>
#include <algorithm>
#include "../Policy.hh"
using namespace Napi;
@ -17,10 +19,10 @@ template <typename T>
class RegistryPolicy : public Policy
{
public:
RegistryPolicy(const std::string name, const std::string &vendorName, const std::string &productName, const DWORD regType)
RegistryPolicy(const std::string name, const std::string &vendorName, const std::string &productName, const std::vector<DWORD>& types)
: Policy(name),
registryKey("Software\\Policies\\" + vendorName + "\\" + productName),
regType(regType) {}
supportedTypes(types) {}
bool refresh()
{
@ -57,12 +59,12 @@ public:
}
protected:
virtual T parseRegistryValue(LPBYTE buffer, DWORD bufferSize) const = 0;
virtual T parseRegistryValue(LPBYTE buffer, DWORD bufferSize, DWORD type) const = 0;
virtual Value getJSValue(Env env, T value) const = 0;
private:
const std::string registryKey;
const DWORD regType;
const std::vector<DWORD> supportedTypes;
std::optional<T> value;
std::optional<T> read(HKEY root)
@ -72,17 +74,32 @@ private:
if (ERROR_SUCCESS != RegOpenKeyEx(root, registryKey.c_str(), 0, KEY_READ, &hKey))
return std::nullopt;
BYTE buffer[1024];
DWORD bufferSize = sizeof(buffer);
DWORD bufferSize = 0;
DWORD type;
auto readResult = RegQueryValueEx(hKey, name.c_str(), 0, &type, buffer, &bufferSize);
// First query to get required buffer size
auto result = RegQueryValueEx(hKey, name.c_str(), 0, &type, nullptr, &bufferSize);
if (ERROR_SUCCESS != result && ERROR_MORE_DATA != result)
{
RegCloseKey(hKey);
return std::nullopt;
}
if (std::find(supportedTypes.begin(), supportedTypes.end(), type) == supportedTypes.end())
{
RegCloseKey(hKey);
return std::nullopt;
}
std::vector<BYTE> buffer(bufferSize);
result = RegQueryValueEx(hKey, name.c_str(), 0, &type, buffer.data(), &bufferSize);
RegCloseKey(hKey);
if (ERROR_SUCCESS != readResult || type != regType)
if (ERROR_SUCCESS != result)
return std::nullopt;
return std::optional<T>{parseRegistryValue(buffer, bufferSize)};
return std::optional<T>{parseRegistryValue(buffer.data(), bufferSize, type)};
}
};

View File

@ -8,10 +8,31 @@
using namespace Napi;
StringPolicy::StringPolicy(const std::string name, const std::string &vendorName, const std::string &productName)
: RegistryPolicy(name, vendorName, productName, REG_SZ) {}
: RegistryPolicy(name, vendorName, productName, {REG_SZ, REG_MULTI_SZ}) {}
std::string StringPolicy::parseRegistryValue(LPBYTE buffer, DWORD bufferSize) const
std::string StringPolicy::parseRegistryValue(LPBYTE buffer, DWORD bufferSize, DWORD type) const
{
if (type == REG_MULTI_SZ)
{
std::string result;
const char *current = reinterpret_cast<char *>(buffer);
const char *end = reinterpret_cast<char *>(buffer) + bufferSize;
while (current < end && *current != '\0')
{
std::string line(current);
if (!result.empty())
{
result += '\n';
}
result += line;
current += line.length() + 1; // Skip past null terminator
}
return result;
}
// REG_SZ handling
return std::string(reinterpret_cast<char *>(buffer), bufferSize - 1);
}

View File

@ -18,7 +18,7 @@ public:
StringPolicy(const std::string name, const std::string &vendorName, const std::string &productName);
protected:
std::string parseRegistryValue(LPBYTE buffer, DWORD bufferSize) const;
std::string parseRegistryValue(LPBYTE buffer, DWORD bufferSize, DWORD type) const;
Value getJSValue(Env env, std::string value) const;
};