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

NetUseEnum (netapi32)
 
.
Summary
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:

Declare Function NetUseEnum Lib "netapi32.dll" (TODO) As TODO

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;
    }

see NetUseAdd

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

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;
            while(li<lRead)
            {
                lInfo=(USE_INFO_2)Marshal.PtrToStructure(new IntPtr(lBuffer.ToInt32()+(Marshal.SizeOf(typeof(USE_INFO_2))*li)),typeof(USE_INFO_2));
                if (lInfo.ui2_remote.StartsWith(lUNCPath))
                {
                    lBack=true;
                    break;
                }
                ++li;
            }

            NetApiBufferFree(lBuffer);

Documentation
NetUseEnum on MSDN

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