home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / unity / d56 / FNDUTL.ZIP / System / cRegistry.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-10-29  |  13.2 KB  |  353 lines

  1. {$INCLUDE ..\cDefines.inc}
  2. unit cRegistry;
  3.  
  4. interface
  5.  
  6. uses
  7.   // Delphi
  8.   Windows,
  9.  
  10.   // Fundamentals
  11.   cUtils;
  12.  
  13.  
  14.  
  15. {                                                                              }
  16. {                      Windows Registry functions v3.01                        }
  17. {                                                                              }
  18. {         This unit is copyright ⌐ 2002 by David Butler (david@e.co.za)        }
  19. {                                                                              }
  20. {                  This unit is part of Delphi Fundamentals.                   }
  21. {                    Its original file name is cWindows.pas                    }
  22. {       The latest version is available from the Fundamentals home page        }
  23. {                     http://fundementals.sourceforge.net/                     }
  24. {                                                                              }
  25. {                I invite you to use this unit, free of charge.                }
  26. {        I invite you to distibute this unit, but it must be for free.         }
  27. {             I also invite you to contribute to its development,              }
  28. {             but do not distribute a modified copy of this file.              }
  29. {                                                                              }
  30. {          A forum is available on SourceForge for general discussion          }
  31. {             http://sourceforge.net/forum/forum.php?forum_id=2117             }
  32. {                                                                              }
  33. { Description:                                                                 }
  34. {   Windows Registry functions.                                                }
  35. {                                                                              }
  36. { Revision history:                                                            }
  37. {   2002/09/22  v3.01  Created cRegistry unit from cWindows.                   }
  38. {                                                                              }
  39.  
  40.  
  41.  
  42. {                                                                              }
  43. { Registry                                                                     }
  44. {                                                                              }
  45. Procedure SplitRegName (const Name : String; var Key, ValueName : String);
  46.  
  47. Function  RegKeyExists (const RootKey : HKEY; const Key : String) : Boolean;
  48. Function  RegValueExists (const RootKey : HKEY; const Key, Name : String) : Boolean;
  49.  
  50. Function  RegSetValue (const RootKey : HKEY; const Key, Name : String;
  51.           const ValueType : Cardinal; const Value : Pointer;
  52.           const ValueSize : Integer) : Boolean; overload;
  53. Function  RegSetValue (const RootKey : HKEY; const Name : String;
  54.           const ValueType : Cardinal; const Value : Pointer;
  55.           const ValueSize : Integer) : Boolean; overload;
  56.  
  57. Function  SetRegistryString (const RootKey : HKEY; const Key : String;
  58.           const Name : String; const Value : String) : Boolean; overload;
  59. Function  SetRegistryString (const RootKey : HKEY; const Name : String;
  60.           const Value : String) : Boolean; overload;
  61.  
  62. Function  SetRegistryDWord (const RootKey : HKEY; const Name : String;
  63.           const Value : LongWord) : Boolean;
  64.  
  65. Function  SetRegistryBinary (const RootKey : HKEY; const Name: String;
  66.           const Value; const ValueSize : Integer) : Boolean;
  67.  
  68. Function  RegGetValue (const RootKey : HKEY; const Key, Name : String;
  69.           const ValueType : Cardinal; var RegValueType : Cardinal;
  70.           var ValueBuf : Pointer; var ValueSize : Integer) : Boolean; overload;
  71. Function  RegGetValue (const RootKey : HKEY; const Name : String;
  72.           const ValueType : Cardinal; var RegValueType : Cardinal;
  73.           var ValueBuf : Pointer; var ValueSize : Integer) : Boolean; overload;
  74.  
  75. Function  GetRegistryString (const RootKey : HKEY; const Key, Name : String) : String; overload;
  76. Function  GetRegistryString (const RootKey : HKEY; const Name : String) : String; overload;
  77.  
  78. Function  GetRegistryDWord (const RootKey : HKEY; const Key, Name : String) : LongWord;
  79.  
  80. Function  DeleteRegistryValue (const RootKey : HKEY; const Key, Name : String) : Boolean;
  81. Function  DeleteRegistryKey (const RootKey : HKEY; const Key : String) : Boolean;
  82.  
  83. Function  ConnectRegistry (const MachineName : String; const RootKey : HKEY;
  84.           var RemoteKey : HKEY) : Boolean;
  85. Function  DisconnectRegistry (const RemoteKey : HKEY) : Boolean;
  86.  
  87. Function  EnumRegistryValues (const RootKey : HKEY; const Name : String;
  88.           var ValueList : StringArray) : Boolean;
  89. Function  EnumRegistryKeys (const RootKey : HKEY; const Name : String;
  90.           var KeyList : StringArray) : Boolean;
  91.  
  92.  
  93.  
  94. implementation
  95.  
  96. uses
  97.   // Delphi
  98.   SysUtils,
  99.  
  100.   // Fundamentals
  101.   cStrings;
  102.  
  103.  
  104.  
  105. {                                                                              }
  106. { Registry                                                                     }
  107. {                                                                              }
  108. Procedure SplitRegName (const Name : String; var Key, ValueName : String);
  109. var S : String;
  110.     I : Integer;
  111.   Begin
  112.     S := WithoutSuffix (WithoutPrefix (Name, '\'), '\');
  113.     I := Pos ('\', S, [foReverse]);
  114.     if I <= 0 then
  115.       begin
  116.         Key := S;
  117.         ValueName := '';
  118.         exit;
  119.       end;
  120.     Key := CopyLeft (S, I - 1);
  121.     ValueName := CopyFrom (S, I + 1);
  122.   End;
  123.  
  124. { Exists                                                                       }
  125. Function RegKeyExists (const RootKey : HKEY; const Key : String) : Boolean;
  126. var Handle : HKEY;
  127.   Begin
  128.     if RegOpenKeyEx (RootKey, PChar (Key), 0, KEY_READ, Handle) = ERROR_SUCCESS then
  129.       begin
  130.         Result := True;
  131.         RegCloseKey (Handle);
  132.       end else
  133.       Result := False;
  134.   End;
  135.  
  136. Function RegValueExists (const RootKey : HKEY; const Key, Name : String) : Boolean;
  137. var Handle : HKEY;
  138.   Begin
  139.     if RegOpenKeyEx (RootKey, PChar (Key), 0, KEY_READ, Handle) = ERROR_SUCCESS then
  140.       begin
  141.         Result := RegQueryValueEx (Handle, Pointer (Name), nil, nil, nil, nil) = ERROR_SUCCESS;
  142.         RegCloseKey (Handle);
  143.       end else
  144.       Result := False;
  145.   End;
  146.  
  147. { Set                                                                          }
  148. Function RegSetValue (const RootKey : HKEY; const Key, Name : String;
  149.          const ValueType : Cardinal; const Value : Pointer;
  150.          const ValueSize : Integer) : Boolean;
  151. var D : DWORD;
  152.     Handle : HKEY;
  153.   Begin
  154.     Result := False;
  155.     if ValueSize < 0 then
  156.       exit;
  157.     if RegCreateKeyEx (RootKey, PChar (Key), 0, nil, REG_OPTION_NON_VOLATILE,
  158.         KEY_WRITE, nil, Handle, @D) <> ERROR_SUCCESS then
  159.       exit;
  160.     Result := RegSetValueEx (Handle, Pointer (Name), 0, ValueType, Value, ValueSize) = ERROR_SUCCESS;
  161.     RegCloseKey (Handle);
  162.   End;
  163.  
  164. Function RegSetValue (const RootKey : HKEY; const Name : String;
  165.          const ValueType : Cardinal; const Value : Pointer;
  166.          const ValueSize : Integer) : Boolean;
  167. var K, N : String;
  168.   Begin
  169.     SplitRegName (Name, K, N);
  170.     Result := RegSetValue (RootKey, K, N, ValueType, Value, ValueSize);
  171.   End;
  172.  
  173. Function SetRegistryString (const RootKey : HKEY; const Key : String; const Name : String; const Value : String) : Boolean;
  174.   Begin
  175.     Result := RegSetValue (RootKey, Key, Name, REG_SZ, PChar (Value), Length (Value) + 1);
  176.   End;
  177.  
  178. Function SetRegistryString (const RootKey : HKEY; const Name : String; const Value : String) : Boolean;
  179.   Begin
  180.     Result := RegSetValue (RootKey, Name, REG_SZ, PChar (Value), Length (Value) + 1);
  181.   End;
  182.  
  183. Function SetRegistryDWord (const RootKey : HKEY; const Name : String; const Value : LongWord) : Boolean;
  184.   Begin
  185.     Result := RegSetValue (RootKey, Name, REG_DWORD, @Value, Sizeof (LongWord));
  186.   End;
  187.  
  188. Function SetRegistryBinary (const RootKey : HKEY; const Name: String; const Value; const ValueSize : Integer) : Boolean;
  189.   Begin
  190.     Result := RegSetValue (RootKey, Name, REG_BINARY, @Value, ValueSize);
  191.   End;
  192.  
  193. { Get                                                                          }
  194. Function RegGetValue (const RootKey : HKEY; const Key, Name : String;
  195.          const ValueType : Cardinal; var RegValueType : Cardinal;
  196.          var ValueBuf : Pointer; var ValueSize : Integer) : Boolean;
  197. var Handle  : HKEY;
  198.     Buf     : Pointer;
  199.     BufSize : Cardinal;
  200.   Begin
  201.     Result := False;
  202.     ValueSize := 0;
  203.     ValueBuf := nil;
  204.     if RegOpenKeyEx (RootKey, PChar (Key), 0, KEY_READ, Handle) <> ERROR_SUCCESS then
  205.       exit;
  206.     BufSize := 0;
  207.     RegQueryValueEx (Handle, Pointer (Name), nil, @RegValueType, nil, @BufSize);
  208.     if BufSize <= 0 then
  209.       exit;
  210.     GetMem (Buf, BufSize);
  211.     if RegQueryValueEx (Handle, Pointer (Name), nil, @RegValueType, Buf, @BufSize) = ERROR_SUCCESS then
  212.       begin
  213.         ValueBuf := Buf;
  214.         ValueSize := Integer (BufSize);
  215.         Result := True;
  216.       end;
  217.     if not Result then
  218.       FreeMem (Buf);
  219.     RegCloseKey (Handle);
  220.   End;
  221.  
  222. Function RegGetValue (const RootKey : HKEY; const Name : String;
  223.          const ValueType : Cardinal; var RegValueType : Cardinal;
  224.          var ValueBuf : Pointer; var ValueSize : Integer) : Boolean;
  225. var K, N : String;
  226.   Begin
  227.     SplitRegName (Name, K, N);
  228.     Result := RegGetValue (RootKey, K, N, ValueType, RegValueType, ValueBuf, ValueSize);
  229.   End;
  230.  
  231. Function GetRegistryString (const RootKey : HKEY; const Key, Name : String) : String;
  232. var Buf   : Pointer;
  233.     Size  : Integer;
  234.     VType : Cardinal;
  235.   Begin
  236.     Result := '';
  237.     if not RegGetValue (RootKey, Key, Name, REG_SZ, VType, Buf, Size) then
  238.       exit;
  239.     if (VType = REG_DWORD) and (Size >= Sizeof (LongWord)) then
  240.       Result := IntToStr (PLongWord (Buf)^) else
  241.       begin
  242.         SetLength (Result, Size);
  243.         MoveMem (Buf^, Pointer (Result)^, Size);
  244.       end;
  245.     FreeMem (Buf);
  246.   End;
  247.  
  248. Function GetRegistryString (const RootKey : HKEY; const Name : String) : String;
  249. var K, N : String;
  250.   Begin
  251.     SplitRegName (Name, K, N);
  252.     Result := GetRegistryString (RootKey, K, N);
  253.   End;
  254.  
  255. Function GetRegistryDWord (const RootKey : HKEY; const Key, Name : String) : LongWord;
  256. var Buf   : Pointer;
  257.     Size  : Integer;
  258.     VType : Cardinal;
  259.   Begin
  260.     Result := 0;
  261.     if not RegGetValue (RootKey, Key, Name, REG_DWORD, VType, Buf, Size) then
  262.       exit;
  263.     if (VType = REG_DWORD) and (Size >= Sizeof (LongWord)) then
  264.       Result := PLongWord (Buf)^;
  265.     FreeMem (Buf);
  266.   End;
  267.  
  268. { Delete                                                                       }
  269. Function DeleteRegistryValue (const RootKey : HKEY; const Key, Name : String) : Boolean;
  270. var Handle : HKEY;
  271.   Begin
  272.     if RegOpenKeyEx (RootKey, PChar (Key), 0, KEY_WRITE, Handle) = ERROR_SUCCESS then
  273.       begin
  274.         Result := RegDeleteValue (Handle, Pointer (Name)) = ERROR_SUCCESS;
  275.         RegCloseKey (Handle);
  276.       end else
  277.       Result := False;
  278.   End;
  279.  
  280. Function DeleteRegistryKey (const RootKey : HKEY; const Key : String) : Boolean;
  281. var Handle : HKEY;
  282.     K, N   : String;
  283.   Begin
  284.     SplitRegName (Key, K, N);
  285.     if RegOpenKeyEx (RootKey, PChar (K), 0, KEY_WRITE, Handle) = ERROR_SUCCESS then
  286.       begin
  287.         Result := RegDeleteKey (Handle, Pointer (N)) = ERROR_SUCCESS;
  288.         RegCloseKey (Handle);
  289.       end else
  290.       Result := False;
  291.   End;
  292.  
  293. { Remote Registries                                                            }
  294. Function ConnectRegistry (const MachineName : String; const RootKey : HKEY;
  295.          var RemoteKey : HKEY) : Boolean;
  296.   Begin
  297.     Result := RegConnectRegistry (PChar (MachineName), RootKey, RemoteKey) = ERROR_SUCCESS;
  298.   End;
  299.  
  300. Function DisconnectRegistry (const RemoteKey : HKEY) : Boolean;
  301.   Begin
  302.     Result := RegCloseKey (RemoteKey) = ERROR_SUCCESS;
  303.   End;
  304.  
  305. { Enumerate                                                                    }
  306. Function RegEnum (const RootKey : HKEY; const Name : String;
  307.          var ResultList : StringArray; const DoKeys : Boolean) : Boolean;
  308. var Buf     : Array [0..2047] of Char;
  309.     BufSize : Cardinal;
  310.     I       : Integer;
  311.     Res     : Integer;
  312.     S       : String;
  313.     Handle  : HKEY;
  314.   Begin
  315.     ResultList := nil;
  316.     Result := RegOpenKeyEx (RootKey, PChar(Name), 0, KEY_READ, Handle) = ERROR_SUCCESS;
  317.     if not Result then
  318.       exit;
  319.     I := 0;
  320.     Repeat
  321.       BufSize := Sizeof (Buf);
  322.       if DoKeys then
  323.         Res := RegEnumKeyEx (Handle, I, @Buf [0], BufSize, nil, nil, nil, nil)
  324.       else
  325.         Res := RegEnumValue (Handle, I, @Buf [0], BufSize, nil, nil, nil, nil);
  326.       if Res = ERROR_SUCCESS then
  327.         begin
  328.           SetLength (S, BufSize);
  329.           if BufSize > 0 then
  330.             MoveMem (Buf [0], Pointer (S)^, BufSize);
  331.           Append (ResultList, S);
  332.           Inc (I);
  333.         end;
  334.     Until Res <> ERROR_SUCCESS;
  335.     RegCloseKey (Handle);
  336.   End;
  337.  
  338. Function EnumRegistryValues (const RootKey : HKEY; const Name : String; var ValueList : StringArray) : Boolean;
  339.   Begin
  340.     Result := RegEnum (RootKey, Name, ValueList, False);
  341.   End;
  342.  
  343. Function EnumRegistryKeys (const RootKey : HKEY; const Name : String; var KeyList : StringArray) : Boolean;
  344.   Begin
  345.     Result := RegEnum (RootKey, Name, KeyList, True);
  346.   End;
  347.  
  348.  
  349.  
  350.  
  351. end.
  352.  
  353.