/source/utils/RegistryUtils.pas

http://sales-purchases.googlecode.com/ · Pascal · 154 lines · 126 code · 28 blank · 0 comment · 3 complexity · f43a09b0e4cccff0246d49bf35dbbbfa MD5 · raw file

  1. unit RegistryUtils;
  2. interface
  3. uses
  4. Classes, Registry;
  5. const
  6. ERROR_SUCCESS = 0;
  7. ERROR_REGISTRY_NOTASSIGNED = ERROR_SUCCESS - 1;
  8. ERROR_REGISTRY_OPENKEY = ERROR_SUCCESS - 2;
  9. ERROR_REGISTRY_READWRITE = ERROR_SUCCESS - 3;
  10. function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant;
  11. function RegistryReadSections (Strings : TStrings; Key : string) : Boolean;
  12. function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean;
  13. function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer;
  14. function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String;
  15. function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double;
  16. function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime;
  17. procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType);
  18. procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean);
  19. procedure RegistryWriteInteger(Section, Key : String; Value : Integer);
  20. procedure RegistryWriteString(Section, Key : String; Value : String);
  21. procedure RegistryWriteFloat(Section, Key : String; Value : Double);
  22. procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime);
  23. var
  24. TheRegistry : TRegIniFile;
  25. implementation
  26. uses
  27. Windows, RegistryKeys, Variants, SysUtils;
  28. function RegistryReadBoolean(Section, Key : String; DefaultValue : Boolean = false) : Boolean;
  29. begin
  30. result := RegistryReadValue(Section, Key, DefaultValue, varBoolean);
  31. end;
  32. function RegistryReadInteger(Section, Key : String; DefaultValue : Integer = 0) : Integer;
  33. begin
  34. result := RegistryReadValue(Section, Key, DefaultValue, varInteger);
  35. end;
  36. function RegistryReadDateTime(Section, Key : String; DefaultValue : TDateTime) : TDateTime;
  37. begin
  38. result := RegistryReadValue(Section, Key, DefaultValue, varDate);
  39. end;
  40. function RegistryReadSections (Strings : TStrings; Key : string) : Boolean;
  41. var
  42. s : string;
  43. begin
  44. with TheRegistry do
  45. try
  46. s := Filename;
  47. if s = '' then
  48. s := REGISTRY_KEY_PRODUCT;
  49. Result := OpenKey (Key, False);
  50. if Result then
  51. begin
  52. ReadSections (Strings);
  53. CloseKey;
  54. OpenKey (s, False)
  55. end
  56. except
  57. Result := False
  58. end;
  59. end;
  60. function RegistryReadString(Section, Key : String; DefaultValue : String = '') : String;
  61. begin
  62. result := RegistryReadValue(Section, Key, DefaultValue, varUString);
  63. end;
  64. function RegistryReadFloat(Section, Key : String; DefaultValue : Double = 0.0) : Double;
  65. begin
  66. result := RegistryReadValue(Section, Key, DefaultValue, varDouble);
  67. end;
  68. function RegistryReadValue(Section, Key : String; DefaultValue : Variant; AType : TVarType) : Variant;
  69. begin
  70. case AType of
  71. varBoolean :
  72. result := TheRegistry.ReadBool(Section, Key, DefaultValue);
  73. varInteger :
  74. result := TheRegistry.ReadInteger(Section, Key, DefaultValue);
  75. varUString :
  76. result := TheRegistry.ReadString(Section, Key, DefaultValue);
  77. varSingle, varDouble :
  78. result := StrToFloat(TheRegistry.ReadString(Section, Key, FloatToStr(DefaultValue)));
  79. varDate :
  80. result := StrToDate(TheRegistry.ReadString(Section, Key, DateToStr(DefaultValue)));
  81. end;
  82. end;
  83. procedure RegistryWriteBoolean(Section, Key : String; Value : Boolean);
  84. begin
  85. RegistryWriteValue(Section, Key, Value, varBoolean);
  86. end;
  87. procedure RegistryWriteInteger(Section, Key : String; Value : Integer);
  88. begin
  89. RegistryWriteValue(Section, Key, Value, varInteger);
  90. end;
  91. procedure RegistryWriteString(Section, Key : String; Value : String);
  92. begin
  93. RegistryWriteValue(Section, Key, Value, varUString);
  94. end;
  95. procedure RegistryWriteFloat(Section, Key : String; Value : Double);
  96. begin
  97. RegistryWriteValue(Section, Key, Value, varDouble);
  98. end;
  99. procedure RegistryWriteDateTime(Section, Key : String; Value : TDateTime);
  100. begin
  101. RegistryWriteValue(Section, Key, Value, varDate);
  102. end;
  103. procedure RegistryWriteValue(Section, Key : String; Value : Variant; AType : TVarType);
  104. begin
  105. case AType of
  106. varBoolean :
  107. TheRegistry.WriteBool(Section, Key, Value);
  108. varInteger :
  109. TheRegistry.WriteInteger(Section, Key, Value);
  110. varUString :
  111. TheRegistry.WriteString(Section, Key, Value);
  112. varSingle, varDouble :
  113. TheRegistry.WriteString(Section, Key, FloatToStr(Value));
  114. varDate :
  115. TheRegistry.WriteString(Section, Key, DateToStr(Value));
  116. end;
  117. end;
  118. initialization
  119. TheRegistry := TRegIniFile.Create;
  120. TheRegistry.OpenKey(REGISTRY_KEY_PRODUCT, true);
  121. finalization
  122. if Assigned (TheRegistry) then begin
  123. TheRegistry.CloseKey;
  124. TheRegistry.Free
  125. end;
  126. end.