3

从 P/Invoked 本机函数,我得到一个IntPtr指向sockaddr结构的。如何将其转换为IPAddress?

谢谢!

4

3 回答 3

5

在@aevitas 的回答之后,我想出了以下解决方案:

public enum SockAddrFamily
{
  Inet = 2,
  Inet6 = 23
}

[StructLayout(LayoutKind.Sequential)]
public struct SockAddr
{
  public ushort Family;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)]
  public byte[] Data;
};

[StructLayout(LayoutKind.Sequential)]
public struct SockAddrIn 
{
  public ushort Family;
  public ushort Port;
  public uint Addr;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
  public byte[] Zero;
}

[StructLayout(LayoutKind.Sequential)]
public struct SockAddrIn6 
{
  public ushort Family;
  public ushort Port;
  public uint FlowInfo;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] 
  public byte[] Addr;
  public uint ScopeId;
};

public IPAddress ConvertSockAddrPtrToIPAddress(IntPtr sockAddrPtr)
{
  SockAddr sockAddr = (SockAddr)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddr));
  switch ((SockAddrFamily)sockAddr.Family)
  {
    case SockAddrFamily.Inet:
    {
      SockAddrIn sockAddrIn = (SockAddrIn)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddrIn));
      return new IPAddress(sockAddrIn.Addr);
    }
    case SockAddrFamily.Inet6:
    {
      SockAddrIn6 sockAddrIn6 = (SockAddrIn6)Marshal.PtrToStructure(sockAddrPtr, typeof(SockAddrIn6));
      return new IPAddress(sockAddrIn6.Addr);
    }
    default:
      throw new Exception(string.Format("Non-IP address family: {0}", sockAddr.Family));
  }
}

我用 IPv4 和 IPv6 地址成功地测试了它。

于 2013-07-02T16:22:27.003 回答
2

由于您不能直接Marshalsockaddr类型转换为IPAddress类型,因此您必须首先从中创建一个托管结构以将其编组为:

[StructLayout(LayoutKind.Sequential)]
internal struct sockaddr_in
{
    internal EnumLib.ADDRESS_FAMILIES sin_family;
    internal ushort sin_port;
    internal in_addr sin_addr;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] internal byte[] sin_zero;

    internal static sockaddr_in FromString(string host, int port)
    {
    var sockaddr = new sockaddr_in();
    var lpAddressLength = Marshal.SizeOf(sockaddr);
    WSAStringToAddress(host + ":" + port, EnumLib.ADDRESS_FAMILIES.AF_INET, IntPtr.Zero,
                      ref sockaddr, ref lpAddressLength);
    return sockaddr;
    }
}

然后,您可以使用从您喜欢Marshal.PtrToStructure的类型中获取类型:sockaddr_inIntPtr

(sockaddr_in) Marshal.PtrToStructure(address, typeof(sockaddr_in));

IPAddress之后,从您刚刚检索到的数据创建一个新对象应该相当简单。

您可以在此处找到上述示例ADDRESS_FAMILIES中包含的枚举:http: //pastie.org/8103489EnumLib

pinvoke.net 上最初有更多该结构的成员,如果您对它们感兴趣,请看这里,但我认为在这种特定情况下您根本不需要它们。

于 2013-07-02T13:50:20.923 回答
0

您必须在 C# 中创建相应的结构。使用以下代码(p是你IntPtr):

(MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));

您可以将其“读取”到 C#。其余代码很简单,因为您将拥有 IP 地址sa_data

于 2013-07-02T13:35:10.020 回答