Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

WNetUseConnection (mpr)
 
.
Summary
Makes a connection to a network resource. The function can redirect a local device to a network resource.
Summary
TODO - a short description

C# Signature:

[DllImport("Mpr.dll")]
private static extern int WNetUseConnection
(
    IntPtr      hwndOwner,
    NETRESOURCE lpNetResource,
    string      lpPassword,
    string      lpUserID,
    Connect     dwFlags,
    string      lpAccessName,
    string      lpBufferSize,
    string      lpResult
);
[DllImport("mpr.dll", SetLastError=true)]
static extern TODO WNetUseConnection(TODO);

VB Signature:

Declare Function WNetUseConnection Lib "mpr.dll" (TODO) As TODO

User-Defined Types:

public enum ResourceScope
{
    CONNECTED   = 0x00000001,
    GLOBALNET   = 0x00000002,
    REMEMBERED  = 0x00000003,
}

public enum ResourceType
{
    ANY     = 0x00000000,
    DISK    = 0x00000001,
    PRINT   = 0x00000002,
}

public enum ResourceDisplayType
{
    GENERIC      = 0x00000000,
    DOMAIN       = 0x00000001,
    SERVER       = 0x00000002,
    SHARE        = 0x00000003,
    FILE         = 0x00000004,
    GROUP        = 0x00000005,
    NETWORK      = 0x00000006,
    ROOT         = 0x00000007,
    SHAREADMIN   = 0x00000008,
    DIRECTORY    = 0x00000009,
    TREE         = 0x0000000A,
    NDSCONTAINER = 0x0000000A,
}

[Flags]
public enum ResourceUsage
{
    CONNECTABLE    = 0x00000001,
    CONTAINER      = 0x00000002,
    NOLOCALDEVICE  = 0x00000004,
    SIBLING        = 0x00000008,
    ATTACHED       = 0x00000010,
}

[Flags]
public enum Connect
{
    UPDATE_PROFILE  = 0x00000001,
    INTERACTIVE     = 0x00000008,
    PROMPT          = 0x00000010,
    REDIRECT        = 0x00000080,
    LOCALDRIVE      = 0x00000100,
    COMMANDLINE     = 0x00000800,
    CMD_SAVECRED    = 0x00001000,
}

[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
    public ResourceScope        dwScope       = 0;
    public ResourceType         dwType        = 0;
    public ResourceDisplayType  dwDisplayType = 0;
    public ResourceUsage        dwUsage       = 0;

    public string lpLocalName  = "";
    public string lpRemoteName = "";
    public string lpComment    = "";
    public string lpProvider   = "";
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

public static void UseConnection(string localName, string remoteName, string username, string password)
{
    NETRESOURCE nr = new NETRESOURCE()
    {
       dwType       = ResourceType.DISK,
       lpLocalName  = localName,
       lpRemoteName = remoteName,
    };

    ThrowIfError(WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null));
}

public static void UseConnection(string localName, string remoteName, bool promptUser = false)
{
    NETRESOURCE nr = new NETRESOURCE()
    {
       dwType       = ResourceType.DISK,
       lpLocalName  = localName,
       lpRemoteName = remoteName,
    };

    ThrowIfError(WNetUseConnection(IntPtr.Zero, nr, null, null, promptUser ? Connect.INTERACTIVE | Connect.PROMPT : 0, null, null, null));
}

Please add some!

[DebuggerStepThrough]
internal static void ThrowIfError(int win32ErrorCode)
{
    if (win32ErrorCode != 0)
    {
       throw new Win32Exception(win32ErrorCode);
    }
}

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions