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.

333 lines
8.3 KiB

  1. package Win32::NetResource;
  2. require Exporter;
  3. require DynaLoader;
  4. require AutoLoader;
  5. $VERSION = '0.03';
  6. @ISA = qw(Exporter DynaLoader);
  7. # Items to export into callers namespace by default. Note: do not export
  8. # names by default without a very good reason. Use EXPORT_OK instead.
  9. # Do not simply export all your public functions/methods/constants.
  10. @EXPORT = qw(
  11. RESOURCEDISPLAYTYPE_DOMAIN
  12. RESOURCEDISPLAYTYPE_FILE
  13. RESOURCEDISPLAYTYPE_GENERIC
  14. RESOURCEDISPLAYTYPE_GROUP
  15. RESOURCEDISPLAYTYPE_SERVER
  16. RESOURCEDISPLAYTYPE_SHARE
  17. RESOURCEDISPLAYTYPE_TREE
  18. RESOURCETYPE_ANY
  19. RESOURCETYPE_DISK
  20. RESOURCETYPE_PRINT
  21. RESOURCETYPE_UNKNOWN
  22. RESOURCEUSAGE_CONNECTABLE
  23. RESOURCEUSAGE_CONTAINER
  24. RESOURCEUSAGE_RESERVED
  25. RESOURCE_CONNECTED
  26. RESOURCE_GLOBALNET
  27. RESOURCE_REMEMBERED
  28. );
  29. =head1 NAME
  30. Win32::NetResource - manage network resources in perl
  31. =head1 SYNOPSIS
  32. use Win32::NetResource;
  33. $ShareInfo = {
  34. 'path' => "C:\\MyShareDir",
  35. 'netname' => "MyShare",
  36. 'remark' => "It is good to share",
  37. 'passwd' => "",
  38. 'current-users' =>0,
  39. 'permissions' => 0,
  40. 'maxusers' => -1,
  41. 'type' => 0,
  42. };
  43. Win32::NetResource::NetShareAdd( $ShareInfo,$parm )
  44. or die "unable to add share";
  45. =head1 DESCRIPTION
  46. This module offers control over the network resources of Win32.Disks,
  47. printers etc can be shared over a network.
  48. =head1 DATA TYPES
  49. There are two main data types required to control network resources.
  50. In Perl these are represented by hash types.
  51. =over 10
  52. =item %NETRESOURCE
  53. KEY VALUE
  54. 'Scope' => Scope of an Enumeration
  55. RESOURCE_CONNECTED,
  56. RESOURCE_GLOBALNET,
  57. RESOURCE_REMEMBERED.
  58. 'Type' => The type of resource to Enum
  59. RESOURCETYPE_ANY All resources
  60. RESOURCETYPE_DISK Disk resources
  61. RESOURCETYPE_PRINT Print resources
  62. 'DisplayType' => The way the resource should be displayed.
  63. RESOURCEDISPLAYTYPE_DOMAIN
  64. The object should be displayed as a domain.
  65. RESOURCEDISPLAYTYPE_GENERIC
  66. The method used to display the object does not matter.
  67. RESOURCEDISPLAYTYPE_SERVER
  68. The object should be displayed as a server.
  69. RESOURCEDISPLAYTYPE_SHARE
  70. The object should be displayed as a sharepoint.
  71. 'Usage' => Specifies the Resources usage:
  72. RESOURCEUSAGE_CONNECTABLE
  73. RESOURCEUSAGE_CONTAINER.
  74. 'LocalName' => Name of the local device the resource is
  75. connected to.
  76. 'RemoteName' => The network name of the resource.
  77. 'Comment' => A string comment.
  78. 'Provider' => Name of the provider of the resource.
  79. =back
  80. =item %SHARE_INFO
  81. This hash represents the SHARE_INFO_502 struct.
  82. =over 10
  83. KEY VALUE
  84. 'netname' => Name of the share.
  85. 'type' => type of share.
  86. 'remark' => A string comment.
  87. 'permissions' => Permissions value
  88. 'maxusers' => the max # of users.
  89. 'current-users' => the current # of users.
  90. 'path' => The path of the share.
  91. 'passwd' => A password if one is req'd
  92. =back
  93. =head1 FUNCTIONS
  94. =head2 NOTE
  95. All of the functions return FALSE (0) if they fail.
  96. =over 10
  97. =item GetSharedResources(\@Resources,dwType)
  98. Creates a list in @Resources of %NETRESOURCE hash references.
  99. =item AddConnection(\%NETRESOURCE,$Password,$UserName,$Connection)
  100. Makes a connection to a network resource specified by %NETRESOURCE
  101. =item CancelConnection($Name,$Connection,$Force)
  102. Cancels a connection to a network resource connected to local device
  103. $name.$Connection is either 1 - persistent connection or 0, non-persistent.
  104. =item WNetGetLastError($ErrorCode,$Description,$Name)
  105. Gets the Extended Network Error.
  106. =item GetError( $ErrorCode )
  107. Gets the last Error for a Win32::NetResource call.
  108. =item GetUNCName( $UNCName, $LocalPath );
  109. Returns the UNC name of the disk share connected to $LocalPath in $UNCName.
  110. =head2 NOTE
  111. $servername is optional for all the calls below. (if not given the
  112. local machine is assumed.)
  113. =item NetShareAdd(\%SHARE,$parm_err,$servername = NULL )
  114. Add a share for sharing.
  115. =item NetShareCheck($device,$type,$servername = NULL )
  116. Check if a share is available for connection.
  117. =item NetShareDel( $netname, $servername = NULL )
  118. Remove a share from a machines list of shares.
  119. =item NetShareGetInfo( $netname, \%SHARE,$servername=NULL )
  120. Get the %SHARE_INFO information about the share $netname on the
  121. server $servername.
  122. =item NetShareSetInfo( $netname,\%SHARE,$parm_err,$servername=NULL)
  123. Set the information for share $netname.
  124. =item AUTHOR
  125. Jesse Dougherty for Hip Communications.
  126. Gurusamy Sarathy <[email protected]> had to clean up the horrendous code
  127. and the bugs.
  128. =back
  129. =cut
  130. sub AUTOLOAD {
  131. # This AUTOLOAD is used to 'autoload' constants from the constant()
  132. # XS function. If a constant is not found then control is passed
  133. # to the AUTOLOAD in AutoLoader.
  134. my($constname);
  135. ($constname = $AUTOLOAD) =~ s/.*:://;
  136. #reset $! to zero to reset any current errors.
  137. $!=0;
  138. my $val = constant($constname, @_ ? $_[0] : 0);
  139. if ($! != 0) {
  140. if ($! =~ /Invalid/) {
  141. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  142. goto &AutoLoader::AUTOLOAD;
  143. }
  144. else {
  145. ($pack,$file,$line) = caller;
  146. die "Your vendor has not defined Win32::NetResource macro $constname, used at $file line $line.
  147. ";
  148. }
  149. }
  150. eval "sub $AUTOLOAD { $val }";
  151. goto &$AUTOLOAD;
  152. }
  153. sub AddConnection
  154. {
  155. my $h = $_[0];
  156. die "AddConnection: HASH reference required" unless ref($h) eq "HASH";
  157. #
  158. # The last four items *must* not be deallocated until the
  159. # _AddConnection() completes (since the packed structure is
  160. # pointing into these values.
  161. #
  162. my $netres = pack( 'i4 p4', $h->{Scope},
  163. $h->{Type},
  164. $h->{DisplayType},
  165. $h->{Usage},
  166. $h->{LocalName},
  167. $h->{RemoteName},
  168. $h->{Comment},
  169. $h->{Provider});
  170. _AddConnection($netres,$_[1],$_[2],$_[3]);
  171. }
  172. #use Data::Dumper;
  173. sub GetSharedResources
  174. {
  175. die "GetSharedResources: ARRAY reference required"
  176. unless ref($_[0]) eq "ARRAY";
  177. my $aref = [];
  178. # Get the shared resources.
  179. my $ret = _GetSharedResources( $aref ,$_[1] );
  180. # build the array of hashes in $_[0]
  181. # print Dumper($aref);
  182. if ($ret) {
  183. foreach ( @$aref ) {
  184. my %hash;
  185. @hash{'Scope',
  186. 'Type',
  187. 'DisplayType',
  188. 'Usage',
  189. 'LocalName',
  190. 'RemoteName',
  191. 'Comment',
  192. 'Provider'} = split /\001/, $_;
  193. push @{$_[0]}, \%hash;
  194. }
  195. }
  196. $ret;
  197. }
  198. sub NetShareAdd
  199. {
  200. my $shareinfo = _hash2SHARE( $_[0] );
  201. _NetShareAdd($shareinfo,$_[1], $_[2] || "");
  202. }
  203. sub NetShareGetInfo
  204. {
  205. my ($netinfo,$val);
  206. $val = _NetShareGetInfo( $_[0],$netinfo,$_[2] || "");
  207. $_[1] = _SHARE2hash( $netinfo );
  208. $val;
  209. }
  210. sub NetShareSetInfo
  211. {
  212. my $shareinfo = _hash2SHARE( $_[1] );
  213. _NetShareSetInfo( $_[0],$shareinfo,$_[2],$_[3] || "");
  214. }
  215. # These are private functions to work with the ShareInfo structure.
  216. # please note that the implementation of these calls requires the
  217. # SHARE_INFO_502 level of information.
  218. sub _SHARE2hash
  219. {
  220. my %hash = ();
  221. @hash{'type',
  222. 'permissions',
  223. 'maxusers',
  224. 'current-users',
  225. 'remark',
  226. 'netname',
  227. 'path',
  228. 'passwd'} = unpack('i4 A257 A81 A257 A257',$_[0]);
  229. return \%hash;
  230. }
  231. sub _hash2SHARE
  232. {
  233. my $h = $_[0];
  234. die "Argument must be a HASH reference" unless ref($h) eq "HASH";
  235. return pack 'i4 a257 a81 a257 a257',
  236. @$h{'type',
  237. 'permissions',
  238. 'maxusers',
  239. 'current-users',
  240. 'remark',
  241. 'netname',
  242. 'path',
  243. 'passwd'};
  244. }
  245. bootstrap Win32::NetResource;
  246. 1;
  247. __END__