winreg.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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='') or (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:=RegCreateKeyExA(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:=RegDeleteKeyA(GetBaseKey(RelativeKey(Key)),P)=ERROR_SUCCESS;
  48. end;
  49. function TRegistry.DeleteValue(const Name: String): Boolean;
  50. begin
  51. Result := RegDeleteValueA(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 RegQueryValueExA(fCurrentKey,P,Nil,
  61. @RD,Buffer,lpdword(@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:=RegQueryValueExA(fCurrentKey,P,Nil,lpdword(@RegData),Nil,lpdword(@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. var
  93. S : string;
  94. Rel : Boolean;
  95. begin
  96. Result:=0;
  97. S:=Key;
  98. Rel:=RelativeKey(S);
  99. if not(Rel) then
  100. Delete(S,1,1);
  101. {$ifdef WinCE}
  102. RegOpenKeyEx(GetBaseKey(Rel),PWideChar(WideString(S)),0,FAccess,Result);
  103. {$else WinCE}
  104. RegOpenKeyEx(GetBaseKey(Rel),PChar(S),0,FAccess,Result);
  105. {$endif WinCE}
  106. end;
  107. function TRegistry.GetKeyInfo(var Value: TRegKeyInfo): Boolean;
  108. var
  109. winFileTime: Windows.FILETIME;
  110. sysTime: TSystemTime;
  111. begin
  112. FillChar(Value, SizeOf(Value), 0);
  113. With Value do
  114. Result:=RegQueryInfoKeyA(CurrentKey,nil,nil,nil,lpdword(@NumSubKeys),
  115. lpdword(@MaxSubKeyLen),nil,lpdword(@NumValues),lpdword(@MaxValueLen),
  116. lpdword(@MaxDataLen),nil,@winFileTime)=ERROR_SUCCESS;
  117. if Result then
  118. begin
  119. FileTimeToSystemTime(@winFileTime, @sysTime);
  120. Value.FileTime := SystemTimeToDateTime(sysTime);
  121. end;
  122. end;
  123. function TRegistry.KeyExists(const Key: string): Boolean;
  124. var
  125. KeyHandle : HKEY;
  126. OldAccess : LONG;
  127. begin
  128. Result:=false;
  129. OldAccess:=FAccess;
  130. try
  131. FAccess:=KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS or STANDARD_RIGHTS_READ;
  132. KeyHandle:=GetKey(Key);
  133. if KeyHandle<>0 then
  134. begin
  135. RegCloseKey(KeyHandle);
  136. Result:=true;
  137. end;
  138. finally
  139. FAccess:=OldAccess;
  140. end;
  141. end;
  142. function TRegistry.LoadKey(const Key, FileName: string): Boolean;
  143. begin
  144. Result := False;
  145. end;
  146. function TRegistry.OpenKey(const Key: string; CanCreate: Boolean): Boolean;
  147. Var
  148. P: PChar;
  149. Handle: HKEY;
  150. Disposition: Integer;
  151. SecurityAttributes: Pointer; //LPSECURITY_ATTRIBUTES;
  152. begin
  153. SecurityAttributes := Nil;
  154. P:=PrepKey(Key);
  155. If CanCreate then
  156. begin
  157. Handle:=0;
  158. Result:=RegCreateKeyExA(GetBaseKey(RelativeKey(Key)),P,0,'',
  159. REG_OPTION_NON_VOLATILE,
  160. fAccess,SecurityAttributes,Handle,
  161. pdword(@Disposition))=ERROR_SUCCESS
  162. end
  163. else
  164. Result:=RegOpenKeyExA(GetBaseKey(RelativeKey(Key)),
  165. P,0,fAccess,Handle)=ERROR_SUCCESS;
  166. If Result then
  167. fCurrentKey:=Handle;
  168. end;
  169. function TRegistry.OpenKeyReadOnly(const Key: string): Boolean;
  170. Var
  171. P: PChar;
  172. Handle: HKEY;
  173. begin
  174. P:=PrepKey(Key);
  175. Result := RegOpenKeyExA(GetBaseKey(RelativeKey(Key)),P,0,KEY_READ,Handle) = 0;
  176. If Result Then
  177. fCurrentKey := Handle;
  178. end;
  179. function TRegistry.RegistryConnect(const UNCName: string): Boolean;
  180. begin
  181. Result := False;
  182. end;
  183. function TRegistry.ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
  184. begin
  185. Result := False;
  186. end;
  187. function TRegistry.RestoreKey(const Key, FileName: string): Boolean;
  188. begin
  189. Result := False;
  190. end;
  191. function TRegistry.SaveKey(const Key, FileName: string): Boolean;
  192. begin
  193. Result := False;
  194. end;
  195. function TRegistry.UnLoadKey(const Key: string): Boolean;
  196. begin
  197. Result := false;
  198. end;
  199. function TRegistry.ValueExists(const Name: string): Boolean;
  200. var
  201. Info : TRegDataInfo;
  202. begin
  203. Result:=GetDataInfo(Name,Info);
  204. end;
  205. procedure TRegistry.CloseKey;
  206. begin
  207. If (CurrentKey<>0) then
  208. begin
  209. if LazyWrite then
  210. RegCloseKey(CurrentKey)
  211. else
  212. RegFlushKey(CurrentKey);
  213. fCurrentKey:=0;
  214. end
  215. end;
  216. procedure TRegistry.CloseKey(key:HKEY);
  217. begin
  218. RegCloseKey(CurrentKey)
  219. end;
  220. procedure TRegistry.ChangeKey(Value: HKey; const Path: String);
  221. begin
  222. CloseKey;
  223. FCurrentKey:=Value;
  224. FCurrentPath:=Path;
  225. end;
  226. procedure TRegistry.GetKeyNames(Strings: TStrings);
  227. Var
  228. L : Cardinal;
  229. I: Integer;
  230. Info: TRegKeyInfo;
  231. P : PChar;
  232. begin
  233. Strings.Clear;
  234. if GetKeyInfo(Info) then
  235. begin
  236. L:=Info.MaxSubKeyLen+1;
  237. GetMem(P,L);
  238. Try
  239. for I:=0 to Info.NumSubKeys-1 do
  240. begin
  241. L:=Info.MaxSubKeyLen+1;
  242. RegEnumKeyExA(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
  243. Strings.Add(StrPas(P));
  244. end;
  245. Finally
  246. FreeMem(P);
  247. end;
  248. end;
  249. end;
  250. procedure TRegistry.GetValueNames(Strings: TStrings);
  251. Var
  252. L : Cardinal;
  253. I: Integer;
  254. Info: TRegKeyInfo;
  255. P : PChar;
  256. begin
  257. Strings.Clear;
  258. if GetKeyInfo(Info) then
  259. begin
  260. L:=Info.MaxValueLen+1;
  261. GetMem(P,L);
  262. Try
  263. for I:=0 to Info.NumValues-1 do
  264. begin
  265. L:=Info.MaxValueLen+1;
  266. RegEnumValueA(CurrentKey,I,P,L,Nil,Nil,Nil,Nil);
  267. Strings.Add(StrPas(P));
  268. end;
  269. Finally
  270. FreeMem(P);
  271. end;
  272. end;
  273. end;
  274. Function TRegistry.SysPutData(const Name: string; Buffer: Pointer;
  275. BufSize: Integer; RegData: TRegDataType) : Boolean;
  276. Var
  277. P: PChar;
  278. RegDataType: DWORD;
  279. begin
  280. Case RegData of
  281. rdUnknown : RegDataType:=REG_NONE;
  282. rdString : RegDataType:=REG_SZ;
  283. rdExpandString : RegDataType:=REG_EXPAND_SZ;
  284. rdInteger : RegDataType:=REG_DWORD;
  285. rdBinary : RegDataType:=REG_BINARY;
  286. end;
  287. P:=@Name[1];
  288. Result:=RegSetValueExA(fCurrentKey,P,0,RegDataType,Buffer,BufSize)=ERROR_SUCCESS;
  289. end;
  290. procedure TRegistry.RenameValue(const OldName, NewName: string);
  291. var
  292. L: Integer;
  293. InfoO,InfoN : TRegDataInfo;
  294. D : TRegDataType;
  295. P: PChar;
  296. begin
  297. If GetDataInfo(OldName,InfoO) and Not GetDataInfo(NewName,InfoN) then
  298. begin
  299. L:=InfoO.DataSize;
  300. if L>0 then
  301. begin
  302. GetMem(P,L);
  303. try
  304. L:=GetData(OldName,P,L,D);
  305. If SysPutData(NewName,P,L,D) then
  306. DeleteValue(OldName);
  307. finally
  308. FreeMem(P);
  309. end;
  310. end;
  311. end;
  312. end;
  313. procedure TRegistry.SetCurrentKey(Value: HKEY);
  314. begin
  315. fCurrentKey := Value;
  316. end;
  317. procedure TRegistry.SetRootKey(Value: HKEY);
  318. begin
  319. fRootKey := Value;
  320. end;