WSAEnumNameSpaceProviders (ws2_32)
Last changed: -61.206.127.148

.
Summary
The WSAEnumNameSpaceProviders function retrieves information on available namespace providers.

C# Signature:

    [DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int WSAEnumNameSpaceProviders(ref int bufferLength, IntPtr nspBuffer);

VB Signature:

Not yet.

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct WSANAMESPACE_INFOW
    {
        Guid NSProviderId;
        uint dwNameSpace;
        bool fActive;
        uint dwVersion;
        string lpszIdentifier;
    }

    const int SOCKET_ERROR = -1;

    static IEnumerable<WSANAMESPACE_INFOW> WSAEnumNameSpaceProviders()
    {
        int bytes = 4000;
        IntPtr buffer = Marshal.AllocCoTaskMem(bytes);
        try
        {
            int count = WSAEnumNameSpaceProviders(ref bytes, buffer);
            if (count == SOCKET_ERROR)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            for (int x = 0; x < count; x++)
            {
                yield return (WSANAMESPACE_INFOW)Marshal.PtrToStructure(
                    IntPtr.Add(buffer, x * Marshal.SizeOf(typeof(WSANAMESPACE_INFOW))),
                    typeof(WSANAMESPACE_INFOW)
                    );

            }
        }
        finally
        {
            Marshal.FreeCoTaskMem(buffer);
        }
    }

Documentation