NetUseEnum (netapi32)
Last changed: -

NetUseEnum - The NetUseEnum function lists all current connections between the local computer and resources on remote servers.

C# Signature:

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern NET_API_STATUS NetUseEnum(
            LPWSTR UncServerName,
            DWORD Level,
            ref IntPtr Buf,
            DWORD PreferedMaximumSize,
            out int EntriesRead,
            out int TotalEntries,
            IntPtr resumeHandle);

VB Signature:

<DllImport("netapi32.dll", SetLastError:=True)> _
    Private Shared Function NetUseEnum( _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal UncServerName As String, _
    ByVal Level As UInt32, _
    ByRef Buf As IntPtr, _
    ByVal PreferedMaximumSize As UInt32, _
    ByRef EntriesRead As Integer, _
    ByRef TotalEntries As Integer, _
    ByRef resumeHandle As IntPtr) As NET_API_STATUS
    End Function

User-Defined Types:


    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct USE_INFO_2
        internal LPWSTR ui2_local;
        internal LPWSTR ui2_remote;
        internal LPWSTR ui2_password;
        internal DWORD  ui2_status;
        internal DWORD  ui2_asg_type;
        internal DWORD  ui2_refcount;
        internal DWORD  ui2_usecount;
        internal LPWSTR ui2_username;
        internal LPWSTR ui2_domainname;


    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure USE_INFO_2
    <MarshalAs(UnmanagedType.LPTStr)> Public ui2_local As String
    <MarshalAs(UnmanagedType.LPTStr)> Public ui2_remote As String
    <MarshalAs(UnmanagedType.LPTStr)> Public ui2_password As String
    Public ui2_status As UInt32
    Public ui2_asg_type As UInt32
    Public ui2_refcount As UInt32
    Public ui2_usecount As UInt32
    <MarshalAs(UnmanagedType.LPTStr)> Public ui2_username As String
    <MarshalAs(UnmanagedType.LPTStr)> Public ui2_domainname As String
    End Structure

    Private Enum NET_USE_STATUS As UInteger
    USE_OK = 0
    USE_PAUSED = 1
    USE_NETERR = 3
    USE_CONN = 4
    USE_RECONN = 5
    End Enum

see NetUseAdd

Alternative Managed API:

Do you know one? Please contribute it!


use NetApiBufferFree to release the buffer even if NetUseEnum returned an error!

Tips & Tricks:

Please add some!

Sample Code:


// This code steps through the returned buffer of NetUseEnum and Marshals the buffer member to the structure

            IntPtr lBuffer=IntPtr.Zero;
            int    lRead;
            int    lTotal;
            IntPtr lHandle=IntPtr.Zero;

            NetUseEnum(null,2,ref lBuffer,0xffffffff,out lRead,out lTotal,lHandle);

            // now step through all network shares and check if we have already a connection to the server
            int li=0;
            USE_INFO_2 lInfo;
                IntPtr ptr = IntPtr.Add(lBuffer, Marshal.SizeOf(typeof(USE_INFO_2)) * li);
                // lInfo=(USE_INFO_2)Marshal.PtrToStructure(new IntPtr(lBuffer.ToInt32()+(Marshal.SizeOf(typeof(USE_INFO_2))*li)),typeof(USE_INFO_2));
                // previous line has been causing "arithmetic operation resulted in an overflow" exception on x86 systems for me. Fixed with the .Add method.
                lInfo = (USE_INFO_2)Marshal.PtrToStructure(ptr, typeof(USE_INFO_2));

                if (lInfo.ui2_remote.StartsWith(lUNCPath))



      Public Shared Function ReadNetworkConns() As USE_INFO_2()
        Dim lBuffer As IntPtr = IntPtr.Zero
        Dim lRead As Integer
        Dim lTotal As Integer
        Dim lHandle As IntPtr = IntPtr.Zero

        NetUseEnum(Nothing, 2, lBuffer, UInt32.MaxValue, lRead, lTotal, lHandle)
        Dim Conns(lRead - 1) As USE_INFO_2
        For i As Integer = 0 To lRead - 1
        Dim RawPos As IntPtr = lBuffer.ToInt32() + (Marshal.SizeOf(GetType(USE_INFO_2)) * i)
        Conns(i) = Marshal.PtrToStructure(lBuffer, GetType(USE_INFO_2))

        Return Conns
    End Function

NetUseEnum on MSDN