home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Win32 / NetResource.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  7.8 KB  |  313 lines

  1. package Win32::NetResource;
  2.  
  3. require Exporter;
  4. require DynaLoader;
  5. require AutoLoader;
  6.  
  7. $VERSION = '0.02';
  8.  
  9. @ISA = qw(Exporter DynaLoader);
  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.  
  30. =head1 NAME
  31.  
  32. Win32::NetResource - manage network resources in perl
  33.  
  34. =head1 SYNOPSIS
  35.  
  36.     use Win32::NetResource;
  37.  
  38.     $ShareInfo = {
  39.                     'path' => "C:\\MyShareDir",
  40.                     'netname' => "MyShare",
  41.                     'remark' => "It is good to share",
  42.                     'passwd' => "",
  43.                     'current-users' =>0,
  44.                     'permissions' => 0,
  45.                     'maxusers' => -1,
  46.                     'type'  => 0,
  47.                     };
  48.     
  49.     Win32::NetResource::NetShareAdd( $ShareInfo,$parm )
  50.         or die "unable to add share";
  51.  
  52.  
  53. =head1 DESCRIPTION
  54.  
  55. This module offers control over the network resources of Win32.Disks,
  56. printers etc can be shared over a network.
  57.  
  58. =head1 DATA TYPES
  59.  
  60. There are two main data types required to control network resources.
  61. In Perl these are represented by hash types.
  62.  
  63. =over 10
  64.  
  65. =item %NETRESOURCE
  66.  
  67.         KEY                    VALUE
  68.         
  69.         'Scope'         =>  Scope of an Enumeration
  70.                             RESOURCE_CONNECTED,
  71.                             RESOURCE_GLOBALNET,
  72.                             RESOURCE_REMEMBERED.
  73.         
  74.         'Type'          =>  The type of resource to Enum
  75.                             RESOURCETYPE_ANY    All resources
  76.                             RESOURCETYPE_DISK    Disk resources
  77.                             RESOURCETYPE_PRINT    Print resources
  78.         
  79.         'DisplayType'   =>  The way the resource should be displayed.
  80.                             RESOURCEDISPLAYTYPE_DOMAIN    
  81.                             The object should be displayed as a domain.
  82.                             RESOURCEDISPLAYTYPE_GENERIC    
  83.                             The method used to display the object does not matter.
  84.                             RESOURCEDISPLAYTYPE_SERVER    
  85.                             The object should be displayed as a server.
  86.                             RESOURCEDISPLAYTYPE_SHARE    
  87.                             The object should be displayed as a sharepoint.
  88.         
  89.         'Usage'         =>  Specifies the Resources usage:
  90.                             RESOURCEUSAGE_CONNECTABLE
  91.                             RESOURCEUSAGE_CONTAINER.
  92.         
  93.         'LocalName'     =>  Name of the local device the resource is 
  94.                             connected to.
  95.         
  96.         'RemoteName'    =>  The network name of the resource.
  97.         
  98.         'Comment'       =>  A string comment.
  99.         
  100.         'Provider'      =>  Name of the provider of the resource.
  101.  
  102. =back    
  103.  
  104. =item %SHARE_INFO
  105.  
  106. This hash represents the SHARE_INFO_502 struct.
  107.  
  108. =over 10
  109.  
  110.         KEY                    VALUE
  111.         'netname'        =>    Name of the share.
  112.         'type'           =>    type of share.
  113.         'remark'         =>    A string comment.
  114.         'permissions'    =>    Permissions value
  115.         'maxusers'       =>    the max # of users.
  116.         'current-users'  =>    the current # of users.
  117.         'path'           =>    The path of the share.
  118.         'passwd'         =>    A password if one is req'd
  119.  
  120. =back
  121.  
  122. =head1 FUNCTIONS
  123.  
  124. =head2 NOTE
  125.  
  126. All of the functions return FALSE (0) if they fail.
  127.  
  128. =over 10
  129.  
  130. =item GetSharedResources(\@Resources,dwType)
  131.  
  132. Creates a list in @Resources of %NETRESOURCE hash references.
  133.  
  134. =item AddConnection(\%NETRESOURCE,$Password,$UserName,$Connection)
  135.  
  136. Makes a connection to a network resource specified by %NETRESOURCE
  137.  
  138. =item CancelConnection($Name,$Connection,$Force)
  139.  
  140. Cancels a connection to a network resource connected to local device 
  141. $name.$Connection is either 1 - persistent connection or 0, non-persistent.
  142.  
  143. =item WNetGetLastError($ErrorCode,$Description,$Name)
  144.  
  145. Gets the Extended Network Error.
  146.  
  147. =item GetError( $ErrorCode )
  148.  
  149. Gets the last Error for a Win32::NetResource call.
  150.  
  151. =item GetUNCName( $UNCName, $LocalPath );
  152.  
  153. Returns the UNC name of the disk share connected to $LocalPath in $UNCName.
  154.  
  155. =head2 NOTE
  156.  
  157. $servername is optional for all the calls below. (if not given the
  158. local machine is assumed.)
  159.  
  160. =item NetShareAdd(\%SHARE,$parm_err,$servername = NULL )
  161.  
  162. Add a share for sharing.
  163.  
  164. =item NetShareCheck($device,$type,$servername = NULL )
  165.  
  166. Check if a share is available for connection.
  167.  
  168. =item NetShareDel( $netname, $servername = NULL )
  169.  
  170. Remove a share from a machines list of shares.
  171.  
  172. =item NetShareGetInfo( $netname, \%SHARE,$servername=NULL )
  173.  
  174. Get the %SHARE_INFO information about the share $netname on the
  175. server $servername.
  176.  
  177. =item NetShareSetInfo( $netname,\%SHARE,$parm_err,$servername=NULL)
  178.  
  179. Set the information for share $netname.
  180.  
  181. =item AUTHOR
  182.  
  183. Jesse Dougherty for Hip Communications.
  184. Gurusamy Sarathy <gsar@umich.edu> had to clean up the horrendous code
  185. and the bugs.
  186.  
  187. =back
  188.  
  189. =cut
  190.  
  191. sub AUTOLOAD {
  192.  
  193.     my($constname);
  194.     ($constname = $AUTOLOAD) =~ s/.*:://;
  195.     $!=0;
  196.     my $val = constant($constname, @_ ? $_[0] : 0);
  197.     if ($! != 0) {
  198.         if ($! =~ /Invalid/) {
  199.             $AutoLoader::AUTOLOAD = $AUTOLOAD;
  200.             goto &AutoLoader::AUTOLOAD;
  201.         }
  202.         else {
  203.             ($pack,$file,$line) = caller;
  204.             die "Your vendor has not defined Win32::NetResource macro $constname, used at $file line $line.
  205. ";
  206.         }
  207.     }
  208.     eval "sub $AUTOLOAD { $val }";
  209.     goto &$AUTOLOAD;
  210. }
  211.  
  212. sub AddConnection
  213. {
  214.     my $h = $_[0];
  215.     die "AddConnection: HASH reference required" unless ref($h) eq "HASH";
  216.  
  217.     my $netres = pack( 'i4 p4', $h->{Scope},
  218.                     $h->{Type},
  219.                 $h->{DisplayType},
  220.                 $h->{Usage},
  221.                 $h->{LocalName},
  222.                 $h->{RemoteName},
  223.                 $h->{Comment},
  224.                 $h->{Provider});
  225.     _AddConnection($netres,$_[1],$_[2],$_[3]);
  226. }
  227.  
  228.  
  229. sub GetSharedResources
  230. {
  231.     die "GetSharedResources: ARRAY reference required"
  232.     unless ref($_[0]) eq "ARRAY";
  233.  
  234.     my $aref = [];
  235.  
  236.  
  237.     my $ret = _GetSharedResources( $aref ,$_[1] );
  238.     
  239.     foreach ( @$aref ) {
  240.     my %hash;
  241.     @hash{'Scope',
  242.               'Type',
  243.           'DisplayType',
  244.           'Usage',
  245.           'LocalName',
  246.           'RemoteName',
  247.           'Comment',
  248.           'Provider'} = split /\001/, $_;
  249.         push @{$_[0]}, \%hash;
  250.     }
  251.  
  252.     $ret;
  253. }
  254.  
  255. sub NetShareAdd
  256. {
  257.     my $shareinfo = _hash2SHARE( $_[0] );
  258.     _NetShareAdd($shareinfo,$_[1], $_[2] || "");
  259. }
  260.  
  261. sub NetShareGetInfo
  262. {
  263.     my ($netinfo,$val);
  264.     $val = _NetShareGetInfo( $_[0],$netinfo,$_[2] || "");
  265.     $_[1] = _SHARE2hash( $netinfo );    
  266.     $val;
  267. }
  268.  
  269. sub NetShareSetInfo
  270. {
  271.     my $shareinfo = _hash2SHARE( $_[1] );
  272.     _NetShareSetInfo( $_[0],$shareinfo,$_[2],$_[3] || "");
  273. }
  274.  
  275.  
  276.  
  277. sub _SHARE2hash
  278. {
  279.     my %hash = ();
  280.     @hash{'type',
  281.           'permissions',
  282.           'maxusers',
  283.           'current-users',
  284.           'remark',
  285.           'netname',
  286.           'path',
  287.           'passwd'} = unpack('i4 A257 A81 A257 A257',$_[0]);
  288.  
  289.     return \%hash;
  290. }
  291.  
  292. sub _hash2SHARE
  293. {
  294.     my $h = $_[0];
  295.     die "Argument must be a HASH reference" unless ref($h) eq "HASH";
  296.  
  297.     return pack 'i4 a257 a81 a257 a257',
  298.          @$h{'type',
  299.             'permissions',
  300.             'maxusers',
  301.             'current-users',
  302.             'remark',
  303.             'netname',
  304.             'path',
  305.             'passwd'};
  306. }
  307.  
  308.  
  309. bootstrap Win32::NetResource;
  310.  
  311. 1;
  312. __END__
  313.