winreg.inc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. {******************************************************************************
  2. TRegistry
  3. ******************************************************************************}
  4. Procedure TRegistry.SysRegCreate;
  5. begin
  6. FStringSizeIncludesNull:=True;
  7. end;
  8. Procedure TRegistry.SysRegfree;
  9. begin
  10. end;
  11. Function PrepKey(Const S : String) : pChar;
  12. begin
  13. If (S[1]<>'\') then
  14. Result:=@S[1]
  15. else
  16. Result:=@S[2];
  17. end;
  18. Function RelativeKey(Const S : String) : Boolean;
  19. begin
  20. Result:=(S[1]<>'\')
  21. end;
  22. function TRegistry.sysCreateKey(const Key: String): Boolean;
  23. Var
  24. P: PChar;
  25. Disposition: Dword;
  26. Handle: HKEY;
  27. SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
  28. begin
  29. SecurityAttributes := Nil;
  30. P:=PrepKey(Key);
  31. Result:=RegCreateKeyEx(GetBaseKey(RelativeKey(Key)),
  32. P,
  33. 0,
  34. '',
  35. REG_OPTION_NON_VOLATILE,
  36. KEY_ALL_ACCESS,
  37. SecurityAttributes,
  38. Handle,
  39. @Disposition) = ERROR_SUCCESS;
  40. RegCloseKey(Handle);
  41. end;
  42. function TRegistry.DeleteKey(const Key: String): Boolean;
  43. Var
  44. P: PChar;
  45. begin
  46. P:=PRepKey(Key);
  47. Result:=RegDeleteKey(GetBaseKey(RelativeKey(Key)),P)=ERROR_SUCCESS;
  48. end;
  49. function TRegistry.DeleteValue(const Name: String): Boolean;
  50. begin
  51. Result := RegDeleteValue(fCurrentKey, @Name[1]) = ERROR_SUCCESS;
  52. end;
  53. function TRegistry.SysGetData(const Name: String; Buffer: Pointer;
  54. BufSize: Integer; var RegData: TRegDataType): Integer;
  55. Var
  56. P: PChar;
  57. RD : DWord;
  58. begin
  59. P := @Name[1];
  60. If RegQueryValueEx(fCurrentKey,P,Nil,
  61. @RD,Buffer,@BufSize)<>ERROR_SUCCESS Then
  62. Result:=-1
  63. else
  64. begin
  65. If (RD=REG_SZ) then
  66. RegData:=rdString
  67. else if (RD=REG_EXPAND_SZ) then
  68. Regdata:=rdExpandString
  69. else if (RD=REG_DWORD) then
  70. RegData:=rdInteger
  71. else if (RD=REG_BINARY) then
  72. RegData:=rdBinary
  73. else
  74. RegData:=rdUnknown;
  75. Result:=BufSize;
  76. end;
  77. end;
  78. function TRegistry.GetDataInfo(const ValueName: String; var Value: TRegDataInfo): Boolean;
  79. Var
  80. P: PChar;
  81. begin
  82. P:=@ValueName[1];
  83. With Value do
  84. Result:=RegQueryValueEx(fCurrentKey,P,Nil,@RegData,Nil,@DataSize)=ERROR_SUCCESS;
  85. If Not Result Then
  86. begin
  87. Value.RegData := rdUnknown;
  88. Value.DataSize := 0
  89. end
  90. end;
  91. function TRegistry.GetKey(const Key: String): HKEY;
  92. begin
  93. Result := FCurrentKey;
  94. end;
  95. function TRegistry.GetKeyInfo(var Value: TRegKeyInfo): Boolean;
  96. begin
  97. FillChar(Value, SizeOf(Value), 0);
  98. With Value do
  99. Result:=RegQueryInfoKey(CurrentKey,nil,nil,nil,@NumSubKeys,
  100. @MaxSubKeyLen,nil,@NumValues,@MaxValueLen,
  101. @MaxDataLen,nil,@FileTime)=ERROR_SUCCESS;
  102. end;
  103. function TRegistry.KeyExists(const Key: string): Boolean;
  104. Var
  105. Value : TRegKeyInfo;
  106. begin
  107. Result :=GetKeyInfo(Value);
  108. end;
  109. function TRegistry.LoadKey(const Key, FileName: string): Boolean;
  110. begin
  111. Result := False;
  112. end;
  113. function TRegistry.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
  114. Var
  115. P: PChar;
  116. Handle: HKEY;
  117. Disposition: Integer;
  118. SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
  119. begin
  120. SecurityAttributes := Nil;
  121. P:=PrepKey(Key);
  122. If CanCreate then
  123. begin
  124. Handle:=0;
  125. Result:=RegCreateKeyEx(GetBaseKey(RelativeKey(Key)),P,0,'',
  126. REG_OPTION_NON_VOLATILE,
  127. fAccess,SecurityAttributes,Handle,
  128. @Disposition)=ERROR_SUCCESS
  129. end
  130. else
  131. Result:=RegOpenKeyEx(GetBaseKey(RelativeKey(Key)),
  132. P,0,fAccess,Handle)=ERROR_SUCCESS;
  133. If Result then
  134. fCurrentKey:=Handle;
  135. end;
  136. function TRegistry.OpenKeyReadOnly(const Key: string): Boolean;
  137. Var
  138. P: PChar;
  139. Handle: HKEY;
  140. begin
  141. P:=PrepKey(Key);
  142. Result := RegOpenKeyEx(GetBaseKey(RelativeKey(Key)),P,0,KEY_READ,Handle) = 0;
  143. If Result Then
  144. fCurrentKey := Handle;
  145. end;
  146. function TRegistry.RegistryConnect(const UNCName: string): Boolean;
  147. begin
  148. Result := False;
  149. end;
  150. function TRegistry.ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
  151. begin
  152. Result := False;
  153. end;
  154. function TRegistry.RestoreKey(const Key, FileName: string): Boolean;
  155. begin
  156. Result := False;
  157. end;
  158. function TRegistry.SaveKey(const Key, FileName: string): Boolean;
  159. begin
  160. Result := False;
  161. end;
  162. function TRegistry.UnLoadKey(const Key: string): Boolean;
  163. begin
  164. Result := false;
  165. end;
  166. function TRegistry.ValueExists(const Name: string): Boolean;
  167. var
  168. Info : TRegDataInfo;
  169. begin
  170. Result:=GetDataInfo(Name,Info);
  171. end;
  172. procedure TRegistry.CloseKey;
  173. begin
  174. If (CurrentKey<>0) then
  175. begin
  176. if LazyWrite then
  177. RegCloseKey(CurrentKey)
  178. else
  179. RegFlushKey(CurrentKey);
  180. fCurrentKey:=0;
  181. end
  182. end;
  183. procedure TRegistry.ChangeKey(Value: HKey; const Path: String);
  184. begin
  185. CloseKey;
  186. FCurrentKey:=Value;
  187. FCurrentPath:=Path;
  188. end;
  189. procedure TRegistry.GetKeyNames(Strings: TStrings);
  190. Var
  191. L : Cardinal;
  192. I: Integer;
  193. Info: TRegKeyInfo;
  194. P : PChar;
  195. begin
  196. Strings.Clear;
  197. if GetKeyInfo(Info) then
  198. begin
  199. L:=Info.MaxSubKeyLen+1;
  200. GetMem(P,L);
  201. Try
  202. for I:=0 to Info.NumSubKeys-1 do
  203. begin
  204. L:=Info.MaxSubKeyLen+1;
  205. RegEnumKeyEx(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
  206. Strings.Add(StrPas(P));
  207. end;
  208. Finally
  209. FreeMem(P);
  210. end;
  211. end;
  212. end;
  213. procedure TRegistry.GetValueNames(Strings: TStrings);
  214. Var
  215. L : Cardinal;
  216. I: Integer;
  217. Info: TRegKeyInfo;
  218. P : PChar;
  219. begin
  220. Strings.Clear;
  221. if GetKeyInfo(Info) then
  222. begin
  223. L:=Info.MaxValueLen+1;
  224. GetMem(P,L);
  225. Try
  226. for I:=0 to Info.NumValues-1 do
  227. begin
  228. L:=Info.MaxValueLen+1;
  229. RegEnumValue(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
  230. Strings.Add(StrPas(P));
  231. end;
  232. Finally
  233. FreeMem(P);
  234. end;
  235. end;
  236. end;
  237. Function TRegistry.SysPutData(const Name: string; Buffer: Pointer;
  238. BufSize: Integer; RegData: TRegDataType) : Boolean;
  239. Var
  240. P: PChar;
  241. RegDataType: DWORD;
  242. begin
  243. Case RegData of
  244. rdUnknown : RegDataType:=REG_NONE;
  245. rdString : RegDataType:=REG_SZ;
  246. rdExpandString : RegDataType:=REG_EXPAND_SZ;
  247. rdInteger : RegDataType:=REG_DWORD;
  248. rdBinary : RegDataType:=REG_BINARY;
  249. end;
  250. P:=@Name[1];
  251. Result:=RegSetValueEx(fCurrentKey,P,0,RegDataType,Buffer,BufSize)=ERROR_SUCCESS;
  252. end;
  253. procedure TRegistry.RenameValue(const OldName, NewName: string);
  254. var
  255. L: Integer;
  256. InfoO,InfoN : TRegDataInfo;
  257. D : TRegDataType;
  258. P: PChar;
  259. begin
  260. If GetDataInfo(OldName,InfoO) and Not GetDataInfo(NewName,InfoN) then
  261. begin
  262. L:=InfoO.DataSize;
  263. if L>0 then
  264. begin
  265. GetMem(P,L);
  266. try
  267. L:=GetData(OldName,P,L,D);
  268. If SysPutData(NewName,P,L,D) then
  269. DeleteValue(OldName);
  270. finally
  271. FreeMem(P);
  272. end;
  273. end;
  274. end;
  275. end;
  276. procedure TRegistry.SetCurrentKey(Value: HKEY);
  277. begin
  278. fCurrentKey := Value;
  279. end;
  280. procedure TRegistry.SetRootKey(Value: HKEY);
  281. begin
  282. fRootKey := Value;
  283. end;