Example #1
0
        internal IntPtr ConvertToMemory()
        {
            int size = Marshal.SizeOf(typeof(EncoderParameter));

            Debug.Assert(size == (16 + 4 + 4 + 4), "wrong size! (" + size + ")");

            IntPtr memory = (IntPtr)((long)Marshal.AllocHGlobal(param.Length * size + Marshal.SizeOf(typeof(Int32))));

            if (memory == IntPtr.Zero)
            {
                throw SafeNativeMethods.StatusException(SafeNativeMethods.OutOfMemory);
            }

            Marshal.WriteInt32(memory, param.Length);

            for (int i = 0; i < param.Length; i++)
            {
                Marshal.StructureToPtr(param[i], (IntPtr)((long)memory + Marshal.SizeOf(typeof(Int32)) + i * size), false);
            }

            return(memory);
        }
Example #2
0
        /// <devdoc>
        ///     Copy the EncoderParameters data into a chunk of memory to be consumed by native GDI+ code.
        ///
        ///     We need to marshal the EncoderParameters info from/to native GDI+ ourselve since the definition of the managed/unmanaged classes
        ///     are different and the native class is a bit weird. The native EncoderParameters class is defined in GDI+ as follows:
        ///
        ///      class EncoderParameters {
        ///          UINT Count;                      // Number of parameters in this structure
        ///          EncoderParameter Parameter[1];   // Parameter values
        ///      };
        ///
        ///     We don't have the 'Count' field since the managed array contains it. In order for this structure to work with more than one
        ///     EncoderParameter we need to preallocate memory for the extra n-1 elements, something like this:
        ///
        ///         EncoderParameters* pEncoderParameters = (EncoderParameters*) malloc(sizeof(EncoderParameters) + (n-1) * sizeof(EncoderParameter));
        ///
        ///     Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for
        ///     that (See VSW#451333).
        /// </devdoc>
        internal IntPtr ConvertToMemory()
        {
            int size = Marshal.SizeOf(typeof(EncoderParameter));

            int    length = param.Length;
            IntPtr memory = Marshal.AllocHGlobal(checked (length * size + Marshal.SizeOf(typeof(IntPtr))));

            if (memory == IntPtr.Zero)
            {
                throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.OutOfMemory);
            }

            Marshal.WriteIntPtr(memory, (IntPtr)length);

            long arrayOffset = checked ((long)memory + Marshal.SizeOf(typeof(IntPtr)));

            for (int i = 0; i < length; i++)
            {
                Marshal.StructureToPtr(param[i], (IntPtr)(arrayOffset + i * size), false);
            }

            return(memory);
        }
Example #3
0
        public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false)
            where From : class
            where To : class
        {
            RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) =>
            {
                var data = serialize(from);
                var to   = deserialize(data);
                Assert.IsTrue(from.IsEqual(to));
            };

            RoundtripPointer <From, To> pointerRoundtrip = (serialize, deserialize) =>
            {
                var ptr  = RMarshal.AllocHGlobal(UnsafeBufferSize);
                var data = serialize(from, ptr, UnsafeBufferSize);
                var to   = deserialize(data, UnsafeBufferSize);
                Assert.IsTrue(from.IsEqual(to));
                RMarshal.FreeHGlobal(data);
            };

            RoundtripMemoryPointer <From, To> memoryPointerRoundtrip = (serialize, deserialize) =>
            {
                var data   = serialize(from);
                var pinned = GCHandle.Alloc(data.Array, GCHandleType.Pinned);
                var to     = deserialize(RMarshal.UnsafeAddrOfPinnedArrayElement(data.Array, data.Offset), data.Count);
                Assert.IsTrue(from.IsEqual(to));
                pinned.Free();
            };

            RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) =>
            {
                var stream = new MemoryStream();

                serialize(from, stream);
                stream.Position = 0;
                var to = deserialize(stream);

                Assert.IsTrue(from.IsEqual(to));
            };

            MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal <To> .From(new InputStream(stream)));
            });

            MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>());
            });

            MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream =>
            {
                stream.Position = 0;
                return(Unmarshal.From(new InputStream(stream)).Deserialize <To>());
            });

            MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From);

            TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) =>
                                                         streamRoundtrip((obj, stream) =>
            {
                using (var tmp = new MemoryStream())
                {
                    serialize(obj, tmp);
                    tmp.Position = 0;
                    transcode(tmp, stream);
                }
            }, deserialize);

            if (noTranscoding)
            {
                streamTranscode = (serialize, transcode, deserialize) => { }
            }
            ;

            // Compact Binary
            streamRoundtrip(SerializeCB, DeserializeCB <To>);
            memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>);
            memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>);
            memoryPointerRoundtrip(SerializeUnsafeCB, DeserializePointerCB <To>);
            pointerRoundtrip(SerializePointerCB, DeserializePointerCB <To>);
            memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>);
            memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>);
            memoryPointerRoundtrip(SerializeSafeCB, DeserializePointerCB <To>);
            memoryRoundtrip(SerializeSafeCBNoInlining, DeserializeSafeCB <To>);

            streamMarshal(MarshalCB);
            streamMarshal(SerializerMarshalCB);
            streamMarshalSchema(MarshalCB);
            streamMarshalNoSchema(MarshalCB);
            memoryMarshal(MarshalCB);

            streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>);
            streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>);

            streamRoundtrip(SerializeCB, stream =>
            {
                var input  = new InputStream(stream);
                var reader = new CompactBinaryReader <InputStream>(input);
                return(DeserializeTagged <To>(reader));
            });

            // Fast Binary
            streamRoundtrip(SerializeFB, DeserializeFB <To>);
            memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>);
            memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>);
            memoryPointerRoundtrip(SerializeFB, DeserializePointerFB <To>);

            streamMarshal(MarshalFB);
            streamMarshal(SerializerMarshalFB);
            streamMarshalSchema(MarshalFB);
            streamMarshalNoSchema(MarshalFB);
            memoryMarshal(MarshalFB);

            streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>);
            streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>);

            streamRoundtrip(SerializeFB, stream =>
            {
                var input  = new InputStream(stream);
                var reader = new FastBinaryReader <InputStream>(input);
                return(DeserializeTagged <To>(reader));
            });

            // Simple doesn't support omitting fields
            if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing))
            {
                streamRoundtrip(SerializeSP, DeserializeSP <From, To>);
                memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>);
                memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>);
                memoryPointerRoundtrip(SerializeSP, DeserializePointerSP <From, To>);

                streamRoundtrip(SerializeSP2, DeserializeSP2 <From, To>);
                memoryRoundtrip(SerializeSP2, DeserializeSafeSP2 <From, To>);
                memoryRoundtrip(SerializeSP2, DeserializeUnsafeSP2 <From, To>);

                streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>);
                streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>);
                streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>);

                // Pull parser doesn't supprot bonded<T>
                if (AnyField <From>(Reflection.IsBonded))
                {
                    streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>);

                    // NewtonSoft JSON doesn't support uint64
                    if (typeof(From) != typeof(MaxUInt64))
                    {
                        streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>);
                    }
                }

                streamRoundtrip(SerializeSP, stream =>
                {
                    var input  = new InputStream(stream);
                    var reader = new SimpleBinaryReader <InputStream>(input);
                    return(DeserializeUntagged <From, To>(reader));
                });

                streamMarshalSchema(MarshalSP);
            }

            // Pull parser doesn't supprot bonded<T>
            if (AnyField <From>(Reflection.IsBonded))
            {
                streamRoundtrip(SerializeXml, DeserializeXml <To>);
                streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>);
                streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>);

                // NewtonSoft JSON doesn't support uint64
                if (typeof(From) != typeof(MaxUInt64))
                {
                    streamRoundtrip(SerializeJson, DeserializeJson <To>);
                    streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>);
                    streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>);
                }
            }
        }

        delegate bool TypePredicate(Type field);
Example #4
0
        public byte[][] GetServiceRecordsUnparsedWindowsRaw(Guid service)
        {
            WqsOffset.AssertCheckLayout();
            BlobOffsets.AssertCheckLayout();
            //temporary workaround - sockets must be initialised
            Socket s = new Socket(AddressFamily32.Bluetooth, SocketType.Stream, BluetoothProtocolType.RFComm);

            //store variable length collection of records
            System.Collections.ArrayList records = new System.Collections.ArrayList();

            byte[] sdp = null;

            WSAQUERYSET wqs = new WSAQUERYSET();
            wqs.dwSize = WqsOffset.StructLength_60;
            wqs.dwNameSpace = WqsOffset.NsBth_16;

#if NETCF
            CSADDR_INFO sainfo = new CSADDR_INFO(null, this.DeviceAddress , SocketType.Unknown, ProtocolType.Unknown);
            wqs.dwNumberOfCsAddrs = 1;
            
            IntPtr pSaInfo = Marshal32.AllocHGlobal(24);
            IntPtr pBrb = Marshal32.AllocHGlobal(256);
            IntPtr pService = Marshal32.AllocHGlobal(240);

            Marshal.StructureToPtr(sainfo, pSaInfo, false);
            wqs.lpcsaBuffer = pSaInfo;            

            Marshal.WriteInt32(pBrb, 0, (int)SdpQueryType.SearchAttributeRequest);

            //write the service guid
            Marshal.Copy(service.ToByteArray(), 0, new IntPtr(pBrb.ToInt32() + 8), 16);
            Marshal.WriteInt16(pBrb, 24, (short)SdpSpecificType.Uuid128);
            Marshal.WriteInt16(pBrb, 26, 0);

            //write an empty guid to the next position
            Marshal.Copy(Guid.Empty.ToByteArray(), 0, new IntPtr(pBrb.ToInt32() + 28), 16);
            Marshal.WriteInt16(pBrb, 44, 0x0);
            Marshal.WriteInt16(pBrb, 46, 0);

            //number of attribute ranges
            Marshal.WriteInt32(pBrb, 248, 1);
            //min attribute
            Marshal.WriteInt16(pBrb, 252, 0);
            //max attribute
            Marshal.StructureToPtr((ushort)0xffff, (IntPtr)(pBrb.ToInt32() + 254), false);
            //Marshal.WriteInt16(pBrb, 254, 0x800);
            
            BLOB b = new BLOB(256, pBrb);

            IntPtr pb = Marshal32.AllocHGlobal(8);

            Marshal.StructureToPtr(b, pb, false);
            wqs.lpBlob = pb;
            
#endif


#if WinXP
            GCHandle hservice = GCHandle.Alloc(service.ToByteArray(), GCHandleType.Pinned);
            wqs.lpServiceClassId = hservice.AddrOfPinnedObject();
            wqs.lpszContext = "(" + this.DeviceAddress.ToString("C") + ")"; // sb.ToString(); // hContext.AddrOfPinnedObject();
#endif

            IntPtr handle = IntPtr.Zero;

            int lookupresult;

            //start looking for Bluetooth services


#if NETCF
            LookupFlags flagsForBegin; // Move above the #if is changed to use for desktop build too.
            flagsForBegin = 0;
            bool isQueryingLocalhost = false;
            try {
                BluetoothRadio theOne = BluetoothRadio.PrimaryRadio; // Only ever one on CE...
                if (theOne != null) {
                    BluetoothAddress localAddr = theOne.LocalAddress;
                    if (localAddr != null) {
                        if (localAddr == this.DeviceAddress) {
                            isQueryingLocalhost = true;
                        }
                    }
                }
            } catch (Exception ex) {
                System.Diagnostics.Debug.Fail("Exception in hack on CE to check if localhost: " + ex);
            }
            if (isQueryingLocalhost) {
                flagsForBegin |= LookupFlags.ResService;
            }
#endif
#if NETCF
            try
            {
                lookupresult = NativeMethods.WSALookupServiceBegin(ref wqs, flagsForBegin, out handle);
#else
            lookupresult = NativeMethods.WSALookupServiceBegin(ref wqs, LookupFlags.FlushCache | LookupFlags.ReturnName | LookupFlags.ReturnBlob, out handle);
#endif
            SocketBluetoothClient.ThrowSocketExceptionForHR(lookupresult);


#if WinXP
            hservice.Free();
#endif

            while (lookupresult == 0) {
                byte[] sdpBuffer = new byte[6000];
                BitConverter.GetBytes(WqsOffset.StructLength_60).CopyTo(sdpBuffer, WqsOffset.dwSize_0);
                BitConverter.GetBytes(WqsOffset.NsBth_16).CopyTo(sdpBuffer, WqsOffset.dwNameSpace_20);
                int size = sdpBuffer.Length;

#if NETCF
                    lookupresult = NativeMethods.WSALookupServiceNext(handle, (LookupFlags)0, ref size, sdpBuffer);
#else
                lookupresult = NativeMethods.WSALookupServiceNext(handle, LookupFlags.FlushCache | LookupFlags.ReturnBlob, ref size, sdpBuffer);
#endif

                if (lookupresult == -1) {
                    const int WSA_E_NO_MORE = 10110;
                    SocketBluetoothClient.ThrowSocketExceptionForHrExceptFor(lookupresult, WSA_E_NO_MORE);
                } else {
                    IntPtr pBlob = InTheHand.Runtime.InteropServices.Marshal32.ReadIntPtr(sdpBuffer, WqsOffset.lpBlob_56);
                    if (pBlob != IntPtr.Zero) {
                        IntPtr pSdpBlob = InTheHand.Runtime.InteropServices.Marshal32.ReadIntPtr(pBlob, BlobOffsets.Offset_pBlobData_4);
                        int cSdpBlob = Marshal.ReadInt32(pBlob);

                        if (cSdpBlob > 2) {
                            sdp = new byte[cSdpBlob];
                            Marshal.Copy(pSdpBlob, sdp, 0, cSdpBlob);
                            records.Add(sdp);
                        }
                    }

                }

            }

#if NETCF
            }
            finally
            {
                sainfo.Dispose();

                Marshal32.FreeHGlobal(pSaInfo);
                Marshal32.FreeHGlobal(pb);
                Marshal32.FreeHGlobal(pBrb);
            }
#endif

            //stop looking
            lookupresult = NativeMethods.WSALookupServiceEnd(handle);
            SocketBluetoothClient.ThrowSocketExceptionForHR(lookupresult);

            return (byte[][])records.ToArray(typeof(byte[]));
        }