public static void MarshalSafeAddressesToNative(WsaQuerySetSafe safeQuery, IntPtr target)
 {
     // marshal the addresses
     if (safeQuery.addressList != null && safeQuery.addressList.Length > 0)
     {
         int sizeOfCsAddrInfo = Marshal.SizeOf(typeof(CsAddrInfoNative));
         Int64 start = target.ToInt64();
         Fx.Assert(start % IntPtr.Size == 0, "Invalid alignment!!");
         foreach (CsAddrInfoSafe safeAddress in safeQuery.addressList)
         {
             CsAddrInfoSafe.StructureToPtr(safeAddress, (IntPtr)start);
             start += sizeOfCsAddrInfo;
         }
     }
 }
                static public void StructureToPtr(WsaQuerySetSafe input, IntPtr target)
                {
                    WsaQuerySetNative native = new WsaQuerySetNative();
                    native.dwSize = input.dwSize;
                    native.lpszServiceInstanceName = input.lpszServiceInstanceName;
                    native.lpServiceClassId = input.lpServiceClassId;
                    native.lpVersion = IntPtr.Zero; // not used
                    native.lpszComment = input.lpszComment;
                    native.dwNameSpace = input.dwNameSpace;
                    native.lpNSProviderId = input.lpNSProviderId;
                    native.lpszContext = input.lpszContext;
                    native.dwNumberOfProtocols = 0; // 0
                    native.lpafpProtocols = IntPtr.Zero; // not used
                    native.lpszQueryString = IntPtr.Zero; // not used
                    native.dwNumberOfCsAddrs = input.dwNumberOfCsAddrs;
                    native.dwOutputFlags = 0; // 0
                    native.lpBlob = input.lpBlob;

                    Int64 sockAddressStart = target.ToInt64() + Marshal.SizeOf(typeof(WsaQuerySetNative));
                    native.lpcsaBuffer = (IntPtr)sockAddressStart;

                    Marshal.StructureToPtr(native, target, false);
                    MarshalSafeAddressesToNative(input, (IntPtr)sockAddressStart);

                }
 static int CalculateSize(WsaQuerySetSafe safeQuerySet)
 {
     int structSize = Marshal.SizeOf(typeof(WsaQuerySetNative));
     if (safeQuerySet.addressList != null)
         structSize += safeQuerySet.addressList.Length * Marshal.SizeOf(typeof(CsAddrInfoNative));
     return structSize;
 }
 public static CriticalAllocHandle FromWsaQuerySetSafe(WsaQuerySetSafe safeQuerySet)
 {
     CriticalAllocHandle result = CriticalAllocHandle.FromSize(CalculateSize(safeQuerySet));
     WsaQuerySetSafe.StructureToPtr(safeQuerySet, (IntPtr)result);
     return result;
 }
                static public WsaQuerySetSafe ToWsaQuerySetSafe(WsaQuerySet input)
                {

                    WsaQuerySetSafe result = new WsaQuerySetSafe();
                    if (input == null)
                        return result;

                    result.dwSize = Marshal.SizeOf(typeof(WsaQuerySetNative));
                    result.lpszServiceInstanceName = CriticalAllocHandleString.FromString(input.ServiceInstanceName);
                    result.lpServiceClassId = CriticalAllocHandleGuid.FromGuid(input.ServiceClassId);
                    result.lpszComment = CriticalAllocHandleString.FromString(input.Comment);
                    result.dwNameSpace = input.NameSpace;
                    result.lpNSProviderId = CriticalAllocHandleGuid.FromGuid(input.NSProviderId);
                    result.lpszContext = CriticalAllocHandleString.FromString(input.Context);
                    result.dwNumberOfProtocols = 0;
                    result.lpafpProtocols = IntPtr.Zero; // not used
                    result.lpszQueryString = IntPtr.Zero;

                    if (input.CsAddrInfos != null)
                    {
                        result.dwNumberOfCsAddrs = input.CsAddrInfos.Length;
                        result.addressList = CsAddrInfoSafe.FromAddresses(input.CsAddrInfos);
                    }
                    result.dwOutputFlags = 0;
                    result.lpBlob = CriticalAllocHandlePnrpBlob.FromPnrpBlob(input.Blob);

                    return result;
                }