regKeyExists function

bool regKeyExists(
  1. int hkey,
  2. String subKey
)

Tests if a registry key exists.

hkey is typically HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE

See the following link for additional values: https://docs.microsoft.com/en-us/windows/win32/sysinfo/predefined-keys

subKey is name of the registry key you want to open. This is typically something like 'Environment'.

A WindowsException is thrown the call falls.

Implementation

bool regKeyExists(
  int hkey,
  String subKey,
) {
  var exists = false;
  final pOpenKey = calloc<IntPtr>();
  final pSubKey = TEXT(subKey);

  try {
    final result = RegOpenKeyEx(hkey, pSubKey, 0, KEY_QUERY_VALUE, pOpenKey);
    if (result == ERROR_SUCCESS) {
      exists = true;
      RegCloseKey(pOpenKey.value);
    }
  } finally {
    calloc
      ..free(pOpenKey)
      ..free(pSubKey);
  }
  return exists;
}