Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

545 lines
12 KiB

  1. package Win32::Registry;
  2. =head1 NAME
  3. Win32::Registry - accessing the Windows registry [obsolete, use Win32::TieRegistry]
  4. =head1 SYNOPSIS
  5. use Win32::Registry;
  6. my $tips;
  7. $::HKEY_LOCAL_MACHINE->Open("SOFTWARE\\Microsoft\\Windows"
  8. ."\\CurrentVersion\\Explorer\\Tips", $tips)
  9. or die "Can't open tips: $^E";
  10. my ($type, $value);
  11. $tips->QueryValueEx("18", $type, $value) or die "No tip #18: $^E";
  12. print "Here's a tip: $value\n";
  13. =head1 DESCRIPTION
  14. NOTE: This module provides a very klunky interface to access the
  15. Windows registry, and is not currently being developed actively. It
  16. only exists for backward compatibility with old code that uses it.
  17. For more powerful and flexible ways to access the registry, use
  18. Win32::TieRegistry.
  19. Win32::Registry provides an object oriented interface to the Windows
  20. Registry.
  21. The following "root" registry objects are exported to the main:: name
  22. space. Additional keys must be opened by calling the provided methods
  23. on one of these.
  24. $HKEY_CLASSES_ROOT
  25. $HKEY_CURRENT_USER
  26. $HKEY_LOCAL_MACHINE
  27. $HKEY_USERS
  28. $HKEY_PERFORMANCE_DATA
  29. $HKEY_CURRENT_CONFIG
  30. $HKEY_DYN_DATA
  31. =cut
  32. use strict;
  33. require Exporter;
  34. require DynaLoader;
  35. use Win32::WinError;
  36. use vars qw($VERSION $AUTOLOAD @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  37. $VERSION = '0.07';
  38. @ISA = qw( Exporter DynaLoader );
  39. @EXPORT = qw(
  40. HKEY_CLASSES_ROOT
  41. HKEY_CURRENT_USER
  42. HKEY_LOCAL_MACHINE
  43. HKEY_PERFORMANCE_DATA
  44. HKEY_CURRENT_CONFIG
  45. HKEY_DYN_DATA
  46. HKEY_USERS
  47. KEY_ALL_ACCESS
  48. KEY_CREATE_LINK
  49. KEY_CREATE_SUB_KEY
  50. KEY_ENUMERATE_SUB_KEYS
  51. KEY_EXECUTE
  52. KEY_NOTIFY
  53. KEY_QUERY_VALUE
  54. KEY_READ
  55. KEY_SET_VALUE
  56. KEY_WRITE
  57. REG_BINARY
  58. REG_CREATED_NEW_KEY
  59. REG_DWORD
  60. REG_DWORD_BIG_ENDIAN
  61. REG_DWORD_LITTLE_ENDIAN
  62. REG_EXPAND_SZ
  63. REG_FULL_RESOURCE_DESCRIPTOR
  64. REG_LEGAL_CHANGE_FILTER
  65. REG_LEGAL_OPTION
  66. REG_LINK
  67. REG_MULTI_SZ
  68. REG_NONE
  69. REG_NOTIFY_CHANGE_ATTRIBUTES
  70. REG_NOTIFY_CHANGE_LAST_SET
  71. REG_NOTIFY_CHANGE_NAME
  72. REG_NOTIFY_CHANGE_SECURITY
  73. REG_OPENED_EXISTING_KEY
  74. REG_OPTION_BACKUP_RESTORE
  75. REG_OPTION_CREATE_LINK
  76. REG_OPTION_NON_VOLATILE
  77. REG_OPTION_RESERVED
  78. REG_OPTION_VOLATILE
  79. REG_REFRESH_HIVE
  80. REG_RESOURCE_LIST
  81. REG_RESOURCE_REQUIREMENTS_LIST
  82. REG_SZ
  83. REG_WHOLE_HIVE_VOLATILE
  84. );
  85. @EXPORT_OK = qw(
  86. RegCloseKey
  87. RegConnectRegistry
  88. RegCreateKey
  89. RegCreateKeyEx
  90. RegDeleteKey
  91. RegDeleteValue
  92. RegEnumKey
  93. RegEnumValue
  94. RegFlushKey
  95. RegGetKeySecurity
  96. RegLoadKey
  97. RegNotifyChangeKeyValue
  98. RegOpenKey
  99. RegOpenKeyEx
  100. RegQueryInfoKey
  101. RegQueryValue
  102. RegQueryValueEx
  103. RegReplaceKey
  104. RegRestoreKey
  105. RegSaveKey
  106. RegSetKeySecurity
  107. RegSetValue
  108. RegSetValueEx
  109. RegUnLoadKey
  110. );
  111. $EXPORT_TAGS{ALL}= \@EXPORT_OK;
  112. bootstrap Win32::Registry;
  113. sub import {
  114. my $pkg = shift;
  115. if ($_[0] && "Win32" eq $_[0]) {
  116. Exporter::export($pkg, "Win32", @EXPORT_OK);
  117. shift;
  118. }
  119. Win32::Registry->export_to_level(1+$Exporter::ExportLevel, $pkg, @_);
  120. }
  121. #######################################################################
  122. # This AUTOLOAD is used to 'autoload' constants from the constant()
  123. # XS function. If a constant is not found then control is passed
  124. # to the AUTOLOAD in AutoLoader.
  125. sub AUTOLOAD {
  126. my($constname);
  127. ($constname = $AUTOLOAD) =~ s/.*:://;
  128. #reset $! to zero to reset any current errors.
  129. $!=0;
  130. my $val = constant($constname, 0);
  131. if ($! != 0) {
  132. if ($! =~ /Invalid/) {
  133. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  134. goto &AutoLoader::AUTOLOAD;
  135. }
  136. else {
  137. my ($pack,$file,$line) = caller;
  138. die "Unknown constant $constname in Win32::Registry "
  139. . "at $file line $line.\n";
  140. }
  141. }
  142. eval "sub $AUTOLOAD { $val }";
  143. goto &$AUTOLOAD;
  144. }
  145. #######################################################################
  146. # _new is a private constructor, not intended for public use.
  147. #
  148. sub _new {
  149. my $self;
  150. if ($_[0]) {
  151. $self->{'handle'} = $_[0];
  152. bless $self;
  153. }
  154. $self;
  155. }
  156. #define the basic registry objects to be exported.
  157. #these had to be hardwired unfortunately.
  158. # XXX Yuck!
  159. {
  160. package main;
  161. use vars qw(
  162. $HKEY_CLASSES_ROOT
  163. $HKEY_CURRENT_USER
  164. $HKEY_LOCAL_MACHINE
  165. $HKEY_USERS
  166. $HKEY_PERFORMANCE_DATA
  167. $HKEY_CURRENT_CONFIG
  168. $HKEY_DYN_DATA
  169. );
  170. }
  171. $::HKEY_CLASSES_ROOT = _new(&HKEY_CLASSES_ROOT);
  172. $::HKEY_CURRENT_USER = _new(&HKEY_CURRENT_USER);
  173. $::HKEY_LOCAL_MACHINE = _new(&HKEY_LOCAL_MACHINE);
  174. $::HKEY_USERS = _new(&HKEY_USERS);
  175. $::HKEY_PERFORMANCE_DATA = _new(&HKEY_PERFORMANCE_DATA);
  176. $::HKEY_CURRENT_CONFIG = _new(&HKEY_CURRENT_CONFIG);
  177. $::HKEY_DYN_DATA = _new(&HKEY_DYN_DATA);
  178. =head2 Methods
  179. The following methods are supported. Note that subkeys can be
  180. specified as a path name, separated by backslashes (which may
  181. need to be doubled if you put them in double quotes).
  182. =over 8
  183. =item Open
  184. $reg_obj->Open($sub_key_name, $sub_reg_obj);
  185. Opens a subkey of a registry object, returning the new registry object
  186. in $sub_reg_obj.
  187. =cut
  188. sub Open {
  189. my $self = shift;
  190. die 'usage: $obj->Open($sub_key_name, $sub_reg_obj)' if @_ != 2;
  191. my ($subkey) = @_;
  192. my ($result,$subhandle);
  193. $result = RegOpenKey($self->{'handle'},$subkey,$subhandle);
  194. $_[1] = _new($subhandle);
  195. return 0 unless $_[1];
  196. $! = Win32::GetLastError() unless $result;
  197. return $result;
  198. }
  199. =item Close
  200. $reg_obj->Close();
  201. Closes an open registry key.
  202. =cut
  203. sub Close {
  204. my $self = shift;
  205. die 'usage: $obj->Close()' if @_ != 0;
  206. return unless exists $self->{'handle'};
  207. my $result = RegCloseKey($self->{'handle'});
  208. if ($result) {
  209. delete $self->{'handle'};
  210. }
  211. else {
  212. $! = Win32::GetLastError();
  213. }
  214. return $result;
  215. }
  216. sub DESTROY {
  217. my $self = shift;
  218. return unless exists $self->{'handle'};
  219. RegCloseKey($self->{'handle'});
  220. delete $self->{'handle'};
  221. }
  222. =item Connect
  223. $reg_obj->Connect($node_name, $new_reg_obj);
  224. Connects to a remote Registry on the node specified by $node_name,
  225. returning it in $new_reg_obj. Returns false if it fails.
  226. =cut
  227. sub Connect {
  228. my $self = shift;
  229. die 'usage: $obj->Connect($node_name, $new_reg_obj)' if @_ != 2;
  230. my ($node) = @_;
  231. my ($result,$subhandle);
  232. $result = RegConnectRegistry ($node, $self->{'handle'}, $subhandle);
  233. $_[1] = _new($subhandle);
  234. return 0 unless $_[1];
  235. $! = Win32::GetLastError() unless $result;
  236. return $result;
  237. }
  238. =item Create
  239. $reg_obj->Create($sub_key_name, $new_reg_obj);
  240. Opens the subkey specified by $sub_key_name, returning the new registry
  241. object in $new_reg_obj. If the specified subkey doesn't exist, it is
  242. created.
  243. =cut
  244. sub Create {
  245. my $self = shift;
  246. die 'usage: $obj->Create($sub_key_name, $new_reg_obj)' if @_ != 2;
  247. my ($subkey) = @_;
  248. my ($result,$subhandle);
  249. $result = RegCreateKey($self->{'handle'},$subkey,$subhandle);
  250. $_[1] = _new ($subhandle);
  251. return 0 unless $_[1];
  252. $! = Win32::GetLastError() unless $result;
  253. return $result;
  254. }
  255. =item SetValue
  256. $reg_obj->SetValue($sub_key_name, $type, $value);
  257. Sets the default value for a subkey specified by $sub_key_name.
  258. =cut
  259. sub SetValue {
  260. my $self = shift;
  261. die 'usage: $obj->SetValue($subkey, $type, $value)' if @_ != 3;
  262. my $result = RegSetValue($self->{'handle'}, @_);
  263. $! = Win32::GetLastError() unless $result;
  264. return $result;
  265. }
  266. =item SetValueEx
  267. $reg_obj->SetValueEx($value_name, $reserved, $type, $value);
  268. Sets the value for the value name identified by $value_name
  269. in the key specified by $reg_obj.
  270. =cut
  271. sub SetValueEx {
  272. my $self = shift;
  273. die 'usage: $obj->SetValueEx($value_name, $reserved, $type, $value)' if @_ != 4;
  274. my $result = RegSetValueEx($self->{'handle'}, @_);
  275. $! = Win32::GetLastError() unless $result;
  276. return $result;
  277. }
  278. =item QueryValue
  279. $reg_obj->QueryValue($sub_key_name, $value);
  280. Gets the default value of the subkey specified by $sub_key_name.
  281. =cut
  282. sub QueryValue {
  283. my $self = shift;
  284. die 'usage: $obj->QueryValue($sub_key_name, $value)' if @_ != 2;
  285. my $result = RegQueryValue($self->{'handle'}, $_[0], $_[1]);
  286. $! = Win32::GetLastError() unless $result;
  287. return $result;
  288. }
  289. =item QueryKey
  290. $reg_obj->QueryKey($classref, $number_of_subkeys, $number_of_values);
  291. Gets information on a key specified by $reg_obj.
  292. =cut
  293. sub QueryKey {
  294. my $garbage;
  295. my $self = shift;
  296. die 'usage: $obj->QueryKey($classref, $number_of_subkeys, $number_of_values)'
  297. if @_ != 3;
  298. my $result = RegQueryInfoKey($self->{'handle'}, $_[0],
  299. $garbage, $garbage, $_[1],
  300. $garbage, $garbage, $_[2],
  301. $garbage, $garbage, $garbage, $garbage);
  302. $! = Win32::GetLastError() unless $result;
  303. return $result;
  304. }
  305. =item QueryValueEx
  306. $reg_obj->QueryValueEx($value_name, $type, $value);
  307. Gets the value for the value name identified by $value_name
  308. in the key specified by $reg_obj.
  309. =cut
  310. sub QueryValueEx {
  311. my $self = shift;
  312. die 'usage: $obj->QueryValueEx($value_name, $type, $value)' if @_ != 3;
  313. my $result = RegQueryValueEx($self->{'handle'}, $_[0], undef, $_[1], $_[2]);
  314. $! = Win32::GetLastError() unless $result;
  315. return $result;
  316. }
  317. =item GetKeys
  318. my @keys;
  319. $reg_obj->GetKeys(\@keys);
  320. Populates the supplied array reference with the names of all the keys
  321. within the registry object $reg_obj.
  322. =cut
  323. sub GetKeys {
  324. my $self = shift;
  325. die 'usage: $obj->GetKeys($arrayref)' if @_ != 1 or ref($_[0]) ne 'ARRAY';
  326. my ($result, $i, $keyname);
  327. $keyname = "DummyVal";
  328. $i = 0;
  329. $result = 1;
  330. while ( $result ) {
  331. $result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  332. if ($result) {
  333. push( @{$_[0]}, $keyname );
  334. }
  335. }
  336. return(1);
  337. }
  338. =item GetValues
  339. my %values;
  340. $reg_obj->GetValues(\%values);
  341. Populates the supplied hash reference with entries of the form
  342. $value_name => [ $value_name, $type, $data ]
  343. for each value in the registry object $reg_obj.
  344. =cut
  345. sub GetValues {
  346. my $self = shift;
  347. die 'usage: $obj->GetValues($hashref)' if @_ != 1;
  348. my ($result,$name,$type,$data,$i);
  349. $name = "DummyVal";
  350. $i = 0;
  351. while ( $result=RegEnumValue( $self->{'handle'},
  352. $i++,
  353. $name,
  354. undef,
  355. $type,
  356. $data ))
  357. {
  358. $_[0]->{$name} = [ $name, $type, $data ];
  359. }
  360. return(1);
  361. }
  362. =item DeleteKey
  363. $reg_obj->DeleteKey($sub_key_name);
  364. Deletes a subkey specified by $sub_key_name from the registry.
  365. =cut
  366. sub DeleteKey {
  367. my $self = shift;
  368. die 'usage: $obj->DeleteKey($sub_key_name)' if @_ != 1;
  369. my $result = RegDeleteKey($self->{'handle'}, @_);
  370. $! = Win32::GetLastError() unless $result;
  371. return $result;
  372. }
  373. =item DeleteValue
  374. $reg_obj->DeleteValue($value_name);
  375. Deletes a value identified by $value_name from the registry.
  376. =cut
  377. sub DeleteValue {
  378. my $self = shift;
  379. die 'usage: $obj->DeleteValue($value_name)' if @_ != 1;
  380. my $result = RegDeleteValue($self->{'handle'}, @_);
  381. $! = Win32::GetLastError() unless $result;
  382. return $result;
  383. }
  384. =item Save
  385. $reg_obj->Save($filename);
  386. Saves the hive specified by $reg_obj to a file.
  387. =cut
  388. sub Save {
  389. my $self = shift;
  390. die 'usage: $obj->Save($filename)' if @_ != 1;
  391. my $result = RegSaveKey($self->{'handle'}, @_);
  392. $! = Win32::GetLastError() unless $result;
  393. return $result;
  394. }
  395. =item Load
  396. $reg_obj->Load($sub_key_name, $file_name);
  397. Loads a key specified by $sub_key_name from a file.
  398. =cut
  399. sub Load {
  400. my $self = shift;
  401. die 'usage: $obj->Load($sub_key_name, $file_name)' if @_ != 2;
  402. my $result = RegLoadKey($self->{'handle'}, @_);
  403. $! = Win32::GetLastError() unless $result;
  404. return $result;
  405. }
  406. =item UnLoad
  407. $reg_obj->Unload($sub_key_name);
  408. Unloads a registry hive.
  409. =cut
  410. sub UnLoad {
  411. my $self = shift;
  412. die 'usage: $obj->UnLoad($sub_key_name)' if @_ != 1;
  413. my $result = RegUnLoadKey($self->{'handle'}, @_);
  414. $! = Win32::GetLastError() unless $result;
  415. return $result;
  416. }
  417. 1;
  418. __END__