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

WSAStringToAddress (ws2_32)
 
.
Summary
The WSAStringToAddress function converts a network address in its standard text presentation form into its numeric binary form in a sockaddr structure, suitable for passing to Windows Sockets routines that take such a structure.

C# Signature:

    /* converts a network address in its standard text presentation form into its numeric binary form in a sockaddr structure.  */
    [ DllImport ( "Ws2_32.dll",
              CharSet = CharSet.Unicode,
              EntryPoint = "WSAStringToAddressW" ) ]
    public static extern uint WSAStringToAddress (
                      string        AddressString,
                      ADDRESS_FAMILIES    AddressFamily,
                      IntPtr        lpProtocolInfo,
                      ref sockaddr_in6    pAddr,
                      ref int        lpAddressLength);
    [DllImport("ws2_32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    public static extern int WSAStringToAddress(
        string addressString,
        System.Net.Sockets.AddressFamily addressfamily,
        IntPtr lpProtocolInfo,
        ref SockAddr socketAddress,
        ref int socketAddressSize);

VB Signature:

Declare Function WSAStringToAddress Lib "ws2_32.dll" (TODO) As TODO

Parameters:

  • addressStrings: A pointer to the string that contains the network address to convert.
  • addressFamily: The address family of the network address
  • lpProtocolInfo: The WSAPROTOCOL_INFO structure associated with the provider to be used. If this is NULL, the call is routed to the provider of the first protocol supporting the indicated AddressFamily.
  • socketAddress: A pointer to a buffer that is filled with a sockaddr structure for the address string if the function succeeds.
  • socketAddressSize: A pointer to the length, in bytes, of the buffer pointed to by the lpAddress parameter.

Notes:

The application must first call WSAStartup before calling WSAStringToAddress.

Sample Code:

    WSADATA data = new WSADATA();
    SockAddr sockAddr = new SockAddr();
    IntPtr pSockAddr = IntPtr.Zero;

    if (WSAStartup(0x201, ref data) == ERROR_SUCCESS)
    {
        int sockAddrSize = Marshal.SizeOf(sockAddr);

        int result = WSAStringToAddress(
        "1.2.3.4",
        System.Net.Sockets.AddressFamily.InterNetwork,
        IntPtr.Zero,
        ref sockAddr,
        ref sockAddrSize);

        WSACleanup();
    }

    if (result != ERROR_SUCCESS)
    {
         throw new Win32Exception(result);
    }

    pSockAddr = Marshal.AllocHGlobal(Marshal.SizeOf(sockAddr));
    Marshal.StructureToPtr(sockAddr, pSockAddr, true);

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