Source code of Windows XP (NT5)
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.

430 lines
10 KiB

  1. package Win32::Registry;
  2. #######################################################################
  3. #Perl Module for Registry Extensions
  4. # This module creates an object oriented interface to the Win32
  5. # Registry.
  6. #
  7. # NOTE: This package exports the following "key" objects to
  8. # the main:: name space.
  9. #
  10. # $main::HKEY_CLASSES_ROOT
  11. # $main::HKEY_CURRENT_USER
  12. # $main::HKEY_LOCAL_MACHINE
  13. # $main::HKEY_USERS
  14. # $main::HKEY_PERFORMANCE_DATA
  15. # $main::HKEY_CURRENT_CONFIG
  16. # $main::HKEY_DYN_DATA
  17. #
  18. #######################################################################
  19. require Exporter;
  20. require DynaLoader;
  21. use Win32::WinError;
  22. $VERSION = '0.06';
  23. @ISA= qw( Exporter DynaLoader );
  24. @EXPORT = qw(
  25. HKEY_CLASSES_ROOT
  26. HKEY_CURRENT_USER
  27. HKEY_LOCAL_MACHINE
  28. HKEY_PERFORMANCE_DATA
  29. HKEY_CURRENT_CONFIG
  30. HKEY_DYN_DATA
  31. HKEY_USERS
  32. KEY_ALL_ACCESS
  33. KEY_CREATE_LINK
  34. KEY_CREATE_SUB_KEY
  35. KEY_ENUMERATE_SUB_KEYS
  36. KEY_EXECUTE
  37. KEY_NOTIFY
  38. KEY_QUERY_VALUE
  39. KEY_READ
  40. KEY_SET_VALUE
  41. KEY_WRITE
  42. REG_BINARY
  43. REG_CREATED_NEW_KEY
  44. REG_DWORD
  45. REG_DWORD_BIG_ENDIAN
  46. REG_DWORD_LITTLE_ENDIAN
  47. REG_EXPAND_SZ
  48. REG_FULL_RESOURCE_DESCRIPTOR
  49. REG_LEGAL_CHANGE_FILTER
  50. REG_LEGAL_OPTION
  51. REG_LINK
  52. REG_MULTI_SZ
  53. REG_NONE
  54. REG_NOTIFY_CHANGE_ATTRIBUTES
  55. REG_NOTIFY_CHANGE_LAST_SET
  56. REG_NOTIFY_CHANGE_NAME
  57. REG_NOTIFY_CHANGE_SECURITY
  58. REG_OPENED_EXISTING_KEY
  59. REG_OPTION_BACKUP_RESTORE
  60. REG_OPTION_CREATE_LINK
  61. REG_OPTION_NON_VOLATILE
  62. REG_OPTION_RESERVED
  63. REG_OPTION_VOLATILE
  64. REG_REFRESH_HIVE
  65. REG_RESOURCE_LIST
  66. REG_RESOURCE_REQUIREMENTS_LIST
  67. REG_SZ
  68. REG_WHOLE_HIVE_VOLATILE
  69. );
  70. @EXPORT_OK = qw(
  71. RegCloseKey
  72. RegConnectRegistry
  73. RegCreateKey
  74. RegCreateKeyEx
  75. RegDeleteKey
  76. RegDeleteValue
  77. RegEnumKey
  78. RegEnumValue
  79. RegFlushKey
  80. RegGetKeySecurity
  81. RegLoadKey
  82. RegNotifyChangeKeyValue
  83. RegOpenKey
  84. RegOpenKeyEx
  85. RegQueryInfoKey
  86. RegQueryValue
  87. RegQueryValueEx
  88. RegReplaceKey
  89. RegRestoreKey
  90. RegSaveKey
  91. RegSetKeySecurity
  92. RegSetValue
  93. RegSetValueEx
  94. RegUnLoadKey
  95. );
  96. $EXPORT_TAGS{ALL}= \@EXPORT_OK;
  97. bootstrap Win32::Registry;
  98. sub import
  99. {
  100. my( $pkg )= shift;
  101. if ( $_[0] && "Win32" eq $_[0] ) {
  102. Exporter::export( $pkg, "Win32", @EXPORT_OK );
  103. shift;
  104. }
  105. Win32::Registry->export_to_level( 1+$Exporter::ExportLevel, $pkg, @_ );
  106. }
  107. #######################################################################
  108. # This AUTOLOAD is used to 'autoload' constants from the constant()
  109. # XS function. If a constant is not found then control is passed
  110. # to the AUTOLOAD in AutoLoader.
  111. sub AUTOLOAD {
  112. my($constname);
  113. ($constname = $AUTOLOAD) =~ s/.*:://;
  114. #reset $! to zero to reset any current errors.
  115. $!=0;
  116. my $val = constant($constname, @_ ? $_[0] : 0);
  117. if ($! != 0) {
  118. if ($! =~ /Invalid/) {
  119. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  120. goto &AutoLoader::AUTOLOAD;
  121. }
  122. else {
  123. ($pack,$file,$line) = caller;
  124. die "Your vendor has not defined Win32::Registry macro $constname, used at $file line $line.";
  125. }
  126. }
  127. eval "sub $AUTOLOAD { $val }";
  128. goto &$AUTOLOAD;
  129. }
  130. #######################################################################
  131. # _new is a private constructor, not intended for public use.
  132. #
  133. sub _new
  134. {
  135. my $self;
  136. if ($_[0]) {
  137. $self->{'handle'} = $_[0];
  138. bless $self;
  139. }
  140. $self;
  141. }
  142. #define the basic registry objects to be exported.
  143. #these had to be hardwired unfortunately.
  144. # XXX Yuck!
  145. $main::HKEY_CLASSES_ROOT = _new(&HKEY_CLASSES_ROOT);
  146. $main::HKEY_CURRENT_USER = _new(&HKEY_CURRENT_USER);
  147. $main::HKEY_LOCAL_MACHINE = _new(&HKEY_LOCAL_MACHINE);
  148. $main::HKEY_USERS = _new(&HKEY_USERS);
  149. $main::HKEY_PERFORMANCE_DATA = _new(&HKEY_PERFORMANCE_DATA);
  150. $main::HKEY_CURRENT_CONFIG = _new(&HKEY_CURRENT_CONFIG);
  151. $main::HKEY_DYN_DATA = _new(&HKEY_DYN_DATA);
  152. #######################################################################
  153. #Open
  154. # creates a new Registry object from an existing one.
  155. # usage: $RegObj->Open( "SubKey",$SubKeyObj );
  156. # $SubKeyObj->Open( "SubberKey", *SubberKeyObj );
  157. sub Open
  158. {
  159. my $self = shift;
  160. die 'usage: Open( $SubKey, $ObjRef )' if @_ != 2;
  161. my ($subkey) = @_;
  162. my ($result,$subhandle);
  163. $result = RegOpenKey($self->{'handle'},$subkey,$subhandle);
  164. $_[1] = _new( $subhandle );
  165. return 0 unless $_[1];
  166. $! = Win32::GetLastError() unless $result;
  167. return $result;
  168. }
  169. #######################################################################
  170. #Close
  171. # close an open registry key.
  172. #
  173. sub Close
  174. {
  175. my $self = shift;
  176. die "usage: Close()" if @_ != 0;
  177. my $result = RegCloseKey($self->{'handle'});
  178. $! = Win32::GetLastError() unless $result;
  179. return $result;
  180. }
  181. #######################################################################
  182. #Connect
  183. # connects to a remote Registry object, returning it in $ObjRef.
  184. # returns false if it fails.
  185. # usage: $RegObj->Connect( $NodeName, $ObjRef );
  186. sub Connect
  187. {
  188. my $self = shift;
  189. die 'usage: Connect( $NodeName, $ObjRef )' if @_ != 2;
  190. my ($node) = @_;
  191. my ($result,$subhandle);
  192. $result = RegConnectRegistry ($node, $self->{'handle'}, $subhandle);
  193. $_[1] = _new( $subhandle );
  194. return 0 unless $_[1];
  195. $! = Win32::GetLastError() unless $result;
  196. return $result;
  197. }
  198. #######################################################################
  199. #Create
  200. # open a subkey. If it doesn't exist, create it.
  201. #
  202. sub Create
  203. {
  204. my $self = shift;
  205. die 'usage: Create( $SubKey,$ScalarRef )' if @_ != 2;
  206. my ($subkey) = @_;
  207. my ($result,$subhandle);
  208. $result = RegCreateKey($self->{'handle'},$subkey,$subhandle);
  209. $_[1] = _new ( $subhandle );
  210. return 0 unless $_[1];
  211. $! = Win32::GetLastError() unless $result;
  212. return $result;
  213. }
  214. #######################################################################
  215. #SetValue
  216. # SetValue sets a value in the current key.
  217. #
  218. sub SetValue
  219. {
  220. my $self = shift;
  221. die 'usage: SetValue($SubKey,$Type,$value )' if @_ != 3;
  222. my $result = RegSetValue( $self->{'handle'}, @_);
  223. $! = Win32::GetLastError() unless $result;
  224. return $result;
  225. }
  226. sub SetValueEx
  227. {
  228. my $self = shift;
  229. die 'usage: SetValueEx( $SubKey,$Reserved,$type,$value )' if @_ != 4;
  230. my $result = RegSetValueEx( $self->{'handle'}, @_);
  231. $! = Win32::GetLastError() unless $result;
  232. return $result;
  233. }
  234. #######################################################################
  235. #QueryValue and QueryKey
  236. # QueryValue gets information on a value in the current key.
  237. # QueryKey " " " " key " " "
  238. sub QueryValue
  239. {
  240. my $self = shift;
  241. die 'usage: QueryValue( $SubKey,$valueref )' if @_ != 2;
  242. my $result = RegQueryValue( $self->{'handle'}, @_);
  243. $! = Win32::GetLastError() unless $result;
  244. return $result;
  245. }
  246. sub QueryKey
  247. {
  248. my $garbage;
  249. my $self = shift;
  250. die 'usage: QueryKey( $classref, $numberofSubkeys, $numberofVals )'
  251. if @_ != 3;
  252. my $result = RegQueryInfoKey($self->{'handle'}, $_[0],
  253. $garbage, $garbage, $_[1],
  254. $garbage, $garbage, $_[2],
  255. $garbage, $garbage, $garbage, $garbage);
  256. $! = Win32::GetLastError() unless $result;
  257. return $result;
  258. }
  259. #######################################################################
  260. #QueryValueEx
  261. # QueryValueEx gets information on a value in the current key.
  262. sub QueryValueEx
  263. {
  264. my $self = shift;
  265. die 'usage: QueryValueEx( $SubKey,$type,$valueref )' if @_ != 3;
  266. my $result = RegQueryValueEx( $self->{'handle'}, $_[0], NULL, $_[1], $_[2] );
  267. $! = Win32::GetLastError() unless $result;
  268. return $result;
  269. }
  270. #######################################################################
  271. #GetKeys
  272. #Note: the list object must be passed by reference:
  273. # $myobj->GetKeys( \@mylist )
  274. sub GetKeys
  275. {
  276. my $self = shift;
  277. die 'usage: GetKeys( $arrayref )' if @_ != 1 or ref($_[0]) ne 'ARRAY';
  278. my ($result, $i, $keyname);
  279. $keyname = "DummyVal";
  280. $i = 0;
  281. $result = 1;
  282. while ( $result ) {
  283. $result = RegEnumKey( $self->{'handle'},$i++, $keyname );
  284. if ($result) {
  285. push( @{$_[0]}, $keyname );
  286. }
  287. }
  288. return(1);
  289. }
  290. #######################################################################
  291. #GetValues
  292. # GetValues creates a hash containing 'name'=> ( name,type,data )
  293. # for each value in the current key.
  294. sub GetValues
  295. {
  296. my $self = shift;
  297. die 'usage: GetValues( $hashref )' if @_ != 1;
  298. my ($result,$name,$type,$data,$i);
  299. $name = "DummyVal";
  300. $i = 0;
  301. while ( $result=RegEnumValue( $self->{'handle'},
  302. $i++,
  303. $name,
  304. NULL,
  305. $type,
  306. $data ))
  307. {
  308. $_[0]->{$name} = [ $name, $type, $data ];
  309. }
  310. return(1);
  311. }
  312. #######################################################################
  313. #DeleteKey
  314. # delete a key from the registry.
  315. # eg: $CLASSES_ROOT->DeleteKey( "KeyNameToDelete");
  316. #
  317. sub DeleteKey
  318. {
  319. my $self = shift;
  320. die 'usage: DeleteKey( $SubKey )' if @_ != 1;
  321. my $result = RegDeleteKey($self->{'handle'}, @_);
  322. $! = Win32::GetLastError() unless $result;
  323. return $result;
  324. }
  325. #######################################################################
  326. #DeleteValue
  327. # delete a value from the current key in the registry
  328. # $CLASSES_ROOT->DeleteValue( "\000" );
  329. sub DeleteValue
  330. {
  331. my $self = shift;
  332. die 'usage: DeleteValue( $SubKey )' if @_ != 1;
  333. my $result = RegDeleteValue($self->{'handle'}, @_);
  334. $! = Win32::GetLastError() unless $result;
  335. return $result;
  336. }
  337. #######################################################################
  338. #save
  339. #saves the current hive to a file.
  340. #
  341. sub Save
  342. {
  343. my $self = shift;
  344. die 'usage: Save( $FileName )' if @_ != 1;
  345. my $result = RegSaveKey($self->{'handle'}, @_);
  346. $! = Win32::GetLastError() unless $result;
  347. return $result;
  348. }
  349. #######################################################################
  350. #Load
  351. #loads a saved key from a file.
  352. sub Load
  353. {
  354. my $self = shift;
  355. die 'usage: Load( $SubKey,$FileName )' if @_ != 2;
  356. my $result = RegLoadKey($self->{'handle'}, @_);
  357. $! = Win32::GetLastError() unless $result;
  358. return $result;
  359. }
  360. #######################################################################
  361. #UnLoad
  362. #unloads a registry hive
  363. sub UnLoad
  364. {
  365. my $self = shift;
  366. die 'usage: UnLoad( $SubKey )' if @_ != 1;
  367. my $result = RegUnLoadKey($self->{'handle'}, @_);
  368. $! = Win32::GetLastError() unless $result;
  369. return $result;
  370. }
  371. #######################################################################
  372. 1;
  373. __END__