Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Connecting to a \\server\share on a remote system as a local user

403 views
Skip to first unread message

David Thielen

unread,
Dec 15, 2006, 5:17:01 PM12/15/06
to
Hi;

This is a follow-on question to "Not sure on ICredentials issue".

I need to be able to read an XML file from a share that only allows access
to local users on that remote system. The previous post says I should use
NetUseAdd. A couple of questions:
1) Is that better than WNetAddConnection2?
2) If not, what parameters if I do not want to map a drive letter as I don't
know what letters are in use.
3) In the case of NetUseAdd - do I close it with NetUseDel or
WNetCancelConnection2
4) For the correct close call - what are the params - it's not clear if it's
the unc or server & share are split out.

And for NetUseAdd if the remote user is a local user, what do I set
ui2_domain to?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm


Walter Wang [MSFT]

unread,
Dec 18, 2006, 8:06:36 AM12/18/06
to
Hi Dave,

1) It's not better than if WNetAddConnection2 since WNetAddConnection2 can
also connect to WebDAV or DFS share:

#NetUseAdd
http://msdn2.microsoft.com/en-gb/library/aa370645.aspx
This function applies only to the Server Message Block (LAN Manager
Workstation) client. The NetUseAdd function does not support Distributed
File System (DFS) shares. To add a share using a different network provider
(WebDAV or a DFS share, for example), use the WNetAddConnection2 or
WNetAddConnection3 function.


2) You could just pass null as the local drive letter, no local drive will
be mapped and you can use unc path format to access the remote resource.

3) It's depend on your requirement to either disconnect the resource or
not. If not, the user can still use the resource after your program quits.

4) See following code list.

In case of NetUseAdd and a local user to the remote server, pass the remote
server computer name as ui2_domain.


Full code listing:

public static class Netapi32
{
[DllImport("NetApi32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern UInt32 NetUseAdd(
string UncServerName,
int Level,
ref USE_INFO_2 Buf,
out uint ParmError);

/*
NET_API_STATUS NetUseDel(
LMSTR UncServerName,
LMSTR UseName,
DWORD ForceCond
);

ForceCond:

USE_NOFORCE Do not fail the disconnection if open files exist on the
connection.
USE_FORCE Fail the disconnection if open files exist on the connection.
USE_LOTS_OF_FORCE Close any open files and delete the connection.

*/
[DllImport("NetApi32.dll", SetLastError = true, CharSet =
CharSet.Unicode)]
public static extern UInt32 NetUseDel(
/*string UncServerName,*/
IntPtr UncServerName, // Use IntPtr.Zero
string UseName,
UInt32 ForceCond);
public const UInt32 USE_NOFORCE = 0;
public const UInt32 USE_FORCE = 1;
public const UInt32 USE_LOTS_OF_FORCE = 2;

}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USE_INFO_2
{
public string ui2_local;
public string ui2_remote;
public string ui2_password;
public uint ui2_status;
public uint ui2_asg_type;
public uint ui2_refcount;
public uint ui2_usecount;
public string ui2_username;
public string ui2_domainname;
}


public struct NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}

public static class Mpr
{
[DllImport("mpr.dll")]
public static extern UInt32 WNetAddConnection2(ref NETRESOURCE
netResource,
string password, string username, int flags);

[DllImport("mpr.dll")]
public static extern UInt32 WNetCancelConnection2(string lpName,
Int32 dwFlags, bool bForce);
}


public static class NetUtil
{
public static void NetUseAdd(string uncShare, string user, string
domain, string password)
{
USE_INFO_2 useInfo = new USE_INFO_2();
useInfo.ui2_remote = uncShare;
useInfo.ui2_password = password;
useInfo.ui2_asg_type = 0; //disk drive
useInfo.ui2_usecount = 1;
useInfo.ui2_username = user;
useInfo.ui2_domainname = domain;

uint paramErrorIndex;
uint returnCode = Netapi32.NetUseAdd(String.Empty, 2, ref
useInfo, out paramErrorIndex);
if (returnCode != 0)
{
throw new Win32Exception((int)returnCode);
}
}

public static void NetUseDel(string uncShare)
{
uint returnCode = Netapi32.NetUseDel(IntPtr.Zero, uncShare,
Netapi32.USE_FORCE);
if (returnCode != 0) throw new Win32Exception((int)returnCode);
}

public static void NetUseConnect(string uncShare, string
localDrive, string user, string password)
{
NETRESOURCE nr = new NETRESOURCE();
nr.dwScope = 2;
nr.dwType = 1;
nr.dwDisplayType = 3;
nr.LocalName = localDrive;
nr.RemoteName = uncShare;
nr.dwUsage = 0;
nr.Comment = null;
nr.Provider = null;
UInt32 ret = Mpr.WNetAddConnection2(ref nr, password, user, 0);
if (ret != 0) throw new Win32Exception((int)ret);
}

public static void NetUseDisconnect(string localDrive)
{
UInt32 ret = Mpr.WNetCancelConnection2(localDrive, 1, true);
if (ret != 0) throw new Win32Exception((int)ret);
}
}

Test code:

// NetUtil.NetUseAdd(@"\\chnwawang01\temp$", "test1",
"chnwawang01", "Password01!");
NetUtil.NetUseConnect(@"\\chnwawang01\temp$", null,
"chnwawang01\\test1", "Password01!");

XmlDocument xmlDoc = new XmlDocument();
string filename = @"\\chnwawang01\temp$\test.xml";
FileInfo fi = new FileInfo(filename);

xmlDoc.Load(filename);

NetUtil.NetUseDel(@"\\chnwawang01\temp$");
//NetUtil.NetUseDisconnect("S:");

Sincerely,
Walter Wang (waw...@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

0 new messages