Quick.Registry.pas 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. { ***************************************************************************
  2. Copyright (c) 2016-2020 Kike Pérez
  3. Unit : Quick.Registry
  4. Description : Util registry info
  5. Author : Kike Pérez
  6. Version : 2.0
  7. Created : 22/01/2021
  8. Modified : 25/01/2021
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Registry;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. Winapi.Windows,
  27. System.Win.Registry;
  28. type
  29. TRegRootKey = (rootCU, rootLM);
  30. TRegistryUtils = class
  31. public
  32. class function GetNewReg(aRootKey : TRegRootKey; aReadOnly : Boolean = False) : TRegistry;
  33. class function GetUniqueMachineId: TGUID; static;
  34. class function IsDarkMode : Boolean;
  35. end;
  36. implementation
  37. class function TRegistryUtils.GetNewReg(aRootKey : TRegRootKey; aReadOnly : Boolean = False) : TRegistry;
  38. begin
  39. if aReadOnly then Result := TRegistry.Create(KEY_READ)
  40. else Result := TRegistry.Create(KEY_ALL_ACCESS);
  41. if aRootKey = TRegRootKey.rootCU then Result.RootKey := HKEY_CURRENT_USER
  42. else Result.RootKey := HKEY_LOCAL_MACHINE;
  43. end;
  44. class function TRegistryUtils.IsDarkMode : Boolean;
  45. var
  46. reg : TRegistry;
  47. begin
  48. reg := GetNewReg(TRegRootKey.rootCU,True);
  49. try
  50. reg.RootKey := HKEY_CURRENT_USER;
  51. reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize');
  52. Result := not reg.ReadBool('AppsUseLightTheme');
  53. finally
  54. reg.Free;
  55. end;
  56. end;
  57. class function TRegistryUtils.GetUniqueMachineId : TGUID;
  58. var
  59. reg : TRegistry;
  60. begin
  61. reg := GetNewReg(TRegRootKey.rootLM,True);
  62. try
  63. reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Cryptography');
  64. Result := StringToGUID(reg.ReadString('MachineGuid'));
  65. finally
  66. reg.Free;
  67. end;
  68. end;
  69. end.