NetDfsEnum (netapi32)
Last changed: -

TODO - a short description

C# Signature:

    /// <summary>
    /// The NetDfsEnum function enumerates all the Distributed File System (Dfs) links in the named Dfs root. The function returns information about the Dfs links based on information specified by the Level parameter.
    /// </summary>
    /// <param name="DfsName">[in] Pointer to a string that specifies the name of the DFS root or a DFS link. </param>
    /// <param name="Level">[in] Specifies the information level of the request. This parameter can be one of the following values. </param>
    /// <param name="PrefMaxLen">[in] Specifies the preferred maximum number of bytes that should be returned by this enumeration function call in the information structure buffer. If this parameter is MAX_PREFERRED_LENGTH, the function allocates the amount of memory required for the data. For more information, see the following Remarks section. This parameter is ignored if you specify level 200 or level 300.</param>
    /// <param name="Buffer">[out] Pointer to the address of a buffer that receives the requested information structures. The format of this data depends on the value of the Level parameter. This buffer is allocated by the system and must be freed using the NetApiBufferFree function. </param>
    /// <param name="EntriesRead">[out] Pointer to a value that receives the actual enumerated DFS link count. </param>
    /// <param name="ResumeHandle">[in, out] Pointer to a value that contains a handle which is used to continue the enumeration. The handle should be zero on the first call and left unchanged for subsequent calls. For more information, see the following Remarks section. Windows Server 2003:  If ResumeHandle is NULL, then no resume handle is stored.</param>
    /// <returns>If the function succeeds, the return value is NERR_Success. If no more entries are available to be enumerated, the return value is ERROR_NO_MORE_ITEMS. If the function fails, the return value is a system error code</returns>
    [DllImport("Netapi32.dll", CharSet = CharSet.Auto/*, SetLastError=true //Return value (NET_API_STATUS) contains error */)]
    public static extern int NetDfsEnum(
        [MarshalAs(UnmanagedType.LPWStr)]string DfsName,
        int Level,
        int PrefMaxLen,
        out IntPtr Buffer,
        [MarshalAs(UnmanagedType.I4)]out int EntriesRead,
        [MarshalAs(UnmanagedType.I4)]ref int ResumeHandle);

VB Signature:

    <DllImport("Netapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function NetDfsEnum( _
        <MarshalAs(UnmanagedType.LPWStr)> ByVal DfsName As String, _
        ByVal Level As Integer, _
        ByVal PrefMaxLen As Integer, _
        ByRef Buffer As IntPtr, _
        ByRef EntriesRead As Integer, _
        ByRef ResumeHandle As Integer) As Integer
    End Function

User-Defined Types:

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Public Structure DFS_INFO_1
        <MarshalAs(UnmanagedType.LPWStr)> Public EntryPath As String
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Public Structure DFS_INFO_3
        <MarshalAs(UnmanagedType.LPWStr)> Dim EntryPath As String
        <MarshalAs(UnmanagedType.LPWStr)> Dim Comment As String
        Dim State As UInt32
        Dim NumberOfStorages As UInt32
        Dim Storages As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Public Structure DFS_STORAGE_INFO
        Dim state As DFS_STORAGE_STATES
        <MarshalAs(UnmanagedType.LPWStr)> Dim ServerName As String
        <MarshalAs(UnmanagedType.LPWStr)> Dim ShareName As String
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    Public Structure SHARE_INFO_0
        Public shi0_netname As String
    End Structure

C# User Defined Types:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct DFS_INFO_1
    string EntryPath;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct DFS_INFO_3
    string EntryPath;
    string Comment;
    UInt32 State;
    UInt32 NumberOfStorages;
    IntPtr Storages;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    string Servername;
    string ShareName;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    struct SHARE_INFO_0
    string shi0_netname;

Alternative Managed API:

Do you know one? Please contribute it!


[2006-07-05] VB Signatures added by KRONOS

[2009-04-02] C# User Defined types added by dalehirt

[2014-02-27] The function does not set the last error value, the error is returned directly and is in NET_API_STATUS format /Stoyan Sabev/

Tips & Tricks:

Remember to free buffer, see NetApiBufferFree

Sample Code:

Please add some!

NetDfsEnum on MSDN