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

WNetGetConnection (mpr)
 
.
Summary
You can use this to get the UNC-Path (Format: "\\Computer\Path\To\Somewhere\") of a Network drive

C# Signature:

[DllImport("mpr.dll", CharSet=CharSet.Auto, SetLastError=true)]

public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);

VB Signature:

<DllImport("mpr.dll")> _

Shared Function WNetGetConnection(<MarshalAs(UnmanagedType.LPTStr)> ByVal localName As String, ByVal remoteName As StringBuilder, ByRef length As Integer) As Integer

End Function

User-Defined Types:

None.

Notes:

localName is the local name for the network resource (drive or printer)

remoteName is a string builder used to return the network name for the resource

length is the current capacity of the StringBuilder

If the call fails because the StringBuilder is not large enough, the call will return a value of 234, and the Length parameter will contain the required size.

Tips & Tricks:

Please add some!

Sample Code:

// This sample code assumes you currently have a drive mapped to p:

// Find out what remote device a local mapping is to

int rc=0;

// Size for the buffer we will use

int bsize=200;

// Create a new stringbuilder, pre-sized as above

StringBuilder rname = new StringBuilder(bsize);

// Call the function

rc = WNetGetConnection("p:", rname, ref bsize);

Sample Code for Visal Basic 2005 Express Edition:

(Without any Error-Handling)

    Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal localName As String, _
    ByVal remoteName As System.Text.StringBuilder, ByRef length As Integer) As Integer

    Private Function strToUnc(ByVal path As String)
        Dim length As Integer = 255
        Dim UNC As New System.Text.StringBuilder(length)
        WNetGetConnection(Microsoft.VisualBasic.Left(path, 2), UNC, length)
        Return UNC.ToString
    End Function

    'to get the UNC-Path of a network-drive use something like:
    Private Sub Test()
        Dim dr As String = "J:\"
        MsgBox("The UNC-Path of drive " & dr & " is: " & strToUnc(dr))
    End Sub

Alternative Managed API:

Do you know one? Please contribute it!

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
Find References
Show Printable Version
Revisions