Share a directory on Windows
#!/usr/bin/perl -w
use Win32;
use Win32::NetResource;
use Win32::WinError; # Error code
# Set up a default host name.
$node = Win32::NodeName();
# Generate "proper" UNC name.
$default_host = "\\\\" . $node;
$host = $default_host;
$directory = "d:\\MyShare";
$name = "MyShare";
$remark = "MyShare directory";
%share_def = (
'maxusers' => -1,
'netname' => $name,
'passwd' => "",
'path' => $directory,
'permissions' => 0x01 | 0x02, # read-write
'remark' => $remark,
'type' => 0x00, # directory
);
$parm_error = 0; # ignore
$status = Win32::NetResource::NetShareAdd(\%share_def,$parm_error, $host);
if ($status) {
print "Shared $directory as $host\\$name\n";
} else {
print_netresource_error();
}
# Note that you may see no message for an error.
sub print_netresource_error {
my($error_code) = 0;
my($type);
my($description);
Win32::NetResource::GetError( $error_code );
if ($error_code == ERROR_EXTENDED_ERROR) {
Win32::NetResource::WNetGetLastError($error_code,
$description, $type);
print "Extended $type error: $description\n";
} else {
print "Error $error_code\n";
print Win32::FormatMessage( $error_code );
}
}
Related examples in the same category