/RegTools.pas

http://bderecreator.googlecode.com/ · Pascal · 325 lines · 315 code · 10 blank · 0 comment · 1 complexity · fbc8091c3639331419213b5ef3831479 MD5 · raw file

  1. unit RegTools;
  2. interface
  3. uses
  4. Windows, Classes, Registry;
  5. type
  6. TWhenStartupProgram = (wspWin9XStartup, wspAnyLogsOn, wspUsrLogsOn);
  7. function AddRegistryString( RootKey: HKey; const Key, Name, Value: string; CanCreateKey:boolean=true ): boolean;
  8. function GetRegistryString( RootKey: HKey; const Key, Name: string ): string;
  9. function DelRegistryString( RootKey: HKey; const Key, Name: string ): boolean;
  10. function StrToRootKey( s: string ): HKey;
  11. function StrToKey( s: string; var Root: HKey; var Key: string ): boolean;
  12. function ImportRegistryFile( const fn: string; params: TStrings ): boolean;
  13. procedure DeleteRegistryKey( const key: string );
  14. function RegisterServiceWin9X( const ServiceName, Command: string ): boolean;
  15. function RegisterStartupProgram( const Name, Command: string; When: TWhenStartupProgram;
  16. OnlyOnce: boolean= false ): boolean;
  17. function UnregisterServiceWin9X( const ServiceName: string ): boolean;
  18. function UnregisterStartupProgram( const Name: string; When: TWhenStartupProgram;
  19. OnlyOnce: boolean= false ): boolean;
  20. function GetStartupProgramCommand( const Name: string; When: TWhenStartupProgram; OnlyOnce: boolean= false ): string;
  21. function GetStartupProgramList( When: TWhenStartupProgram; List: TStrings; OnlyOnce: boolean= false ): boolean;
  22. implementation
  23. uses
  24. SysUtils, StrTools;
  25. const
  26. KeyCurrVer = '\SOFTWARE\Microsoft\Windows\CurrentVersion\';
  27. resourcestring
  28. rsErrorNoWin9X = 'This function must be used only in Windows 9X';
  29. function StrToRootKey( s: string ): HKey;
  30. begin
  31. s := UpperCase(Trim(s));
  32. if s='HKEY_CLASSES_ROOT' then result := HKEY_CLASSES_ROOT
  33. else if s='HKEY_CURRENT_USER' then result := HKEY_CURRENT_USER
  34. else if s='HKEY_LOCAL_MACHINE' then result := HKEY_LOCAL_MACHINE
  35. else if s='HKEY_USERS' then result := HKEY_USERS
  36. else if s='HKEY_CURRENT_CONFIG' then result := HKEY_CURRENT_CONFIG
  37. else if s='HKEY_DYN_DATA' then result := HKEY_DYN_DATA
  38. else result := 0;
  39. end;
  40. function StrToKey( s: string; var Root: HKey; var Key: string ): boolean;
  41. begin
  42. Root := StrToRootKey(GetParam('\',s));
  43. Key := '\'+s;
  44. result := Root<>0;
  45. end;
  46. function ImportRegistryFile( const fn: string; params: TStrings ): boolean;
  47. var
  48. t : TextFile;
  49. l,n : string;
  50. r : TRegistry;
  51. modo : (modoClave,modoValor);
  52. fin : boolean;
  53. function SacarComillas( s: string ): string;
  54. begin
  55. s := Trim(s);
  56. if (s>'') and (s[1]='"') and (s[length(s)]='"') then
  57. result := copy(s,2,length(s)-2)
  58. else
  59. result := s;
  60. end;
  61. function SacarCorchetes( s: string ): string;
  62. begin
  63. s := Trim(s);
  64. if (s>'') and (s[1]='[') and (s[length(s)]=']') then
  65. result := copy(s,2,length(s)-2)
  66. else
  67. result := s;
  68. end;
  69. function ReadLine( var l: string ): boolean;
  70. begin
  71. repeat
  72. result := not EOF(t);
  73. if not result then exit;
  74. readln(t,l);
  75. l := Trim(l);
  76. until l>'';
  77. l := ReplaceParams(l,params);
  78. end;
  79. begin
  80. result := false;
  81. try
  82. r := TRegistry.Create;
  83. assign(t,fn);
  84. Reset(t);
  85. if not ReadLine(l) or (UpperCase(l)<>'REGEDIT4') then exit;
  86. modo := modoClave;
  87. fin := not ReadLine(l);
  88. result := true;
  89. while not fin and result do
  90. begin
  91. case modo of
  92. modoClave:
  93. if l[1]='[' then
  94. begin
  95. l := SacarCorchetes(l);
  96. r.CloseKey;
  97. r.RootKey := StrToRootKey(GetParam('\',l));
  98. result := r.OpenKey('\'+l,true);
  99. fin := not ReadLine(l);
  100. modo := modoValor;
  101. end
  102. else result := false;
  103. modoValor:
  104. if l[1]='"' then
  105. begin
  106. n := SacarComillas(GetParam('=',l));
  107. // if n='@' then n := '';
  108. r.WriteString(n,SacarComillas(l));
  109. fin := not ReadLine(l);
  110. end
  111. else if l[1]='@' then
  112. begin
  113. GetParam('=',l);
  114. r.WriteString('',SacarComillas(l));
  115. fin := not ReadLine(l);
  116. end
  117. else if l[1]='[' then
  118. modo := modoClave;
  119. else result := false;
  120. end;
  121. end;
  122. Close(t);
  123. r.CloseKey;
  124. except
  125. r.Free;
  126. end;
  127. end;
  128. procedure DeleteRegistryKey( const key: string );
  129. var
  130. r : TRegistry;
  131. k : string;
  132. procedure DeleteKey( k: string );
  133. var
  134. kn : TStringList;
  135. i : integer;
  136. begin
  137. if not r.OpenKey(k,false) then exit;
  138. if r.HasSubKeys then
  139. begin
  140. kn := TStringList.Create;
  141. r.GetKeyNames(kn);
  142. r.CloseKey;
  143. for i := 0 to kn.Count-1 do
  144. DeleteKey(k+'\'+kn[i]);
  145. kn.Free;
  146. end
  147. else r.CloseKey;
  148. r.DeleteKey(k);
  149. end;
  150. begin
  151. k := Key;
  152. r := TRegistry.Create;
  153. r.RootKey := StrToRootKey(GetParam('\',k));
  154. DeleteKey(k);
  155. r.Free;
  156. end;
  157. function AddRegistryString( RootKey: HKey; const Key, Name, Value: string; CanCreateKey:boolean=true ): boolean;
  158. var
  159. r : TRegistry;
  160. begin
  161. result := false;
  162. r := TRegistry.Create;
  163. r.RootKey := RootKey;
  164. if r.OpenKey(Key,CanCreateKey) then
  165. begin
  166. r.WriteString(Name,Value);
  167. result := true;
  168. end;
  169. r.Free;
  170. end;
  171. function GetRegistryString( RootKey: HKey; const Key, Name: string ): string;
  172. var
  173. r : TRegistry;
  174. begin
  175. result := '';
  176. r := TRegistry.Create;
  177. r.RootKey := RootKey;
  178. if r.OpenKey(Key,false) then
  179. result := r.ReadString(Name);
  180. r.Free;
  181. end;
  182. function DelRegistryString( RootKey: HKey; const Key, Name: string ): boolean;
  183. var
  184. r : TRegistry;
  185. begin
  186. result := false;
  187. r := TRegistry.Create;
  188. r.RootKey := RootKey;
  189. if r.OpenKey(Key,false) then
  190. result := r.DeleteValue(Name);
  191. r.Free;
  192. end;
  193. function GetRegistryNames( RootKey: HKey; const Key: string; Names: TStrings ): boolean;
  194. var
  195. r : TRegistry;
  196. begin
  197. result := false;
  198. r := TRegistry.Create;
  199. r.RootKey := RootKey;
  200. if r.OpenKey(Key,false) then
  201. begin
  202. r.GetValueNames(Names);
  203. result := true;
  204. end;
  205. r.Free;
  206. end;
  207. function GetRegistryValues( RootKey: HKey; const Key: string; Values: TStrings ): boolean;
  208. var
  209. r : TRegistry;
  210. n : TStringList;
  211. i : integer;
  212. begin
  213. result := false;
  214. r := TRegistry.Create;
  215. r.RootKey := RootKey;
  216. if r.OpenKey(Key,false) then
  217. begin
  218. n := TStringList.Create;
  219. r.GetValueNames(n);
  220. for i := 0 to n.Count-1 do
  221. Values.Values[n[i]] := r.ReadString(n[i]);
  222. n.Free;
  223. result := true;
  224. end;
  225. r.Free;
  226. end;
  227. function RegisterServiceWin9X( const ServiceName, Command: string ): boolean;
  228. begin
  229. if Win32Platform<>VER_PLATFORM_WIN32_WINDOWS then raise Exception.Create(rsErrorNoWin9X);
  230. result := AddRegistryString(HKEY_LOCAL_MACHINE,KeyCurrVer+'RunServices',ServiceName,Command);
  231. end;
  232. function UnregisterServiceWin9X( const ServiceName: string ): boolean;
  233. begin
  234. if Win32Platform<>VER_PLATFORM_WIN32_WINDOWS then raise Exception.Create(rsErrorNoWin9X);
  235. result := DelRegistryString(HKEY_LOCAL_MACHINE,KeyCurrVer+'RunServices',ServiceName);
  236. end;
  237. procedure GetStartupProgramKey( When: TWhenStartupProgram; OnlyOnce: boolean;
  238. var RootKey: HKEY; var Key: string );
  239. begin
  240. case When of
  241. wspWin9XStartup:
  242. begin
  243. if Win32Platform<>VER_PLATFORM_WIN32_WINDOWS then raise Exception.Create(rsErrorNoWin9X);
  244. RootKey := HKEY_LOCAL_MACHINE;
  245. if OnlyOnce then Key := 'RunServicesOnce'
  246. else Key := 'RunServices';
  247. end;
  248. wspAnyLogsOn:
  249. begin
  250. RootKey := HKEY_LOCAL_MACHINE;
  251. if OnlyOnce then Key := 'RunOnce'
  252. else Key := 'Run';
  253. end;
  254. wspUsrLogsOn:
  255. begin
  256. RootKey := HKEY_CURRENT_USER;
  257. if OnlyOnce and (Win32Platform<>VER_PLATFORM_WIN32_WINDOWS) then raise Exception.Create(rsErrorNoWin9X);
  258. if OnlyOnce then Key := 'RunOnce'
  259. else Key := 'Run';
  260. end;
  261. end;
  262. end;
  263. function RegisterStartupProgram( const Name, Command: string; When: TWhenStartupProgram;
  264. OnlyOnce: boolean= false ): boolean;
  265. var
  266. rk : HKEY;
  267. k : string;
  268. begin
  269. GetStartupProgramKey(When,OnlyOnce,rk,k);
  270. result := AddRegistryString(rk,KeyCurrVer+k,Name,Command);
  271. end;
  272. function UnregisterStartupProgram( const Name: string; When: TWhenStartupProgram;
  273. OnlyOnce: boolean= false ): boolean;
  274. var
  275. rk : HKEY;
  276. k : string;
  277. begin
  278. GetStartupProgramKey(When,OnlyOnce,rk,k);
  279. result := DelRegistryString(rk,KeyCurrVer+k,Name);
  280. end;
  281. function GetStartupProgramCommand( const Name: string; When: TWhenStartupProgram; OnlyOnce: boolean= false ): string;
  282. var
  283. rk : HKEY;
  284. k : string;
  285. begin
  286. GetStartupProgramKey(When,OnlyOnce,rk,k);
  287. result := GetRegistryString(rk,KeyCurrVer+k,Name);
  288. end;
  289. function GetStartupProgramList( When: TWhenStartupProgram; List: TStrings; OnlyOnce: boolean= false ): boolean;
  290. var
  291. rk : HKEY;
  292. k : string;
  293. begin
  294. GetStartupProgramKey(When,OnlyOnce,rk,k);
  295. result := GetRegistryValues(rk,KeyCurrVer+k,List);
  296. end;
  297. end.