Exemplo n.º 1
0
        public void Alloc_XpcomInitialize_ReturnsValidPointer()
        {
            IntPtr memory = Xpcom.Alloc(new IntPtr(100));

            Assert.IsFalse(memory == IntPtr.Zero);
            Xpcom.Free(memory);
        }
        public void AdoptData(byte[] data, int dataLen)
        {
            // DON'T FREE THIS BUFFER
            var buffer = Xpcom.Alloc(new IntPtr(dataLen));

            Marshal.Copy(data, 0, buffer, dataLen);
            // the input stream takes
            // ownership of the given data buffer and will nsMemory::Free it when
            // the input stream is destroyed.
            _stringInputStream.AdoptData(buffer, dataLen);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert the passed string to Utf16 encoded string
        /// written to memory allocated by xpcom/xulrunner itself.
        /// </summary>
        /// <param name="ManagedObj"></param>
        /// <returns>ptr to native memory containing utf16 encoded string + null term</returns>
        public IntPtr MarshalManagedToNative(object ManagedObj)
        {
            var    str            = ManagedObj as string;
            var    bytes          = System.Text.Encoding.Unicode.GetBytes(str);
            IntPtr unmangedMemory = Xpcom.Alloc(bytes.Length + 2);

            Marshal.Copy(bytes, 0, unmangedMemory, bytes.Length);
            Marshal.WriteByte(unmangedMemory, bytes.Length, 0);
            Marshal.WriteByte(unmangedMemory, bytes.Length + 1, 0);

            return(unmangedMemory);
        }
Exemplo n.º 4
0
            public void GetInterfaces(ref uint count, ref IntPtr array)
            {
                count = 1;
                array = Xpcom.Alloc(new IntPtr(IntPtr.Size * count));
                Guid   guid      = typeof(nsIDOMDocument).GUID;
                var    bytearray = guid.ToByteArray();
                IntPtr guidPtr   = Xpcom.Alloc(new IntPtr(16));

                for (int i = 0; i < 16; i++)
                {
                    Marshal.WriteByte(guidPtr, i, bytearray[i]);
                }
                Marshal.WriteIntPtr(array, guidPtr);
            }
        /// <summary>
        /// Convert the passed string to Utf16 encoded string
        /// written to memory allocated by xpcom/xulrunner itself.
        /// </summary>
        /// <param name="ManagedObj"></param>
        /// <returns>ptr to native memory containing utf16 encoded string + null term</returns>
        public IntPtr MarshalManagedToNative(object ManagedObj)
        {
            var    str            = ManagedObj as string;
            var    bytes          = System.Text.Encoding.Unicode.GetBytes(str);
            IntPtr unmangedMemory = Xpcom.Alloc(bytes.Length + 2);

            for (int i = 0; i < bytes.Length; ++i)
            {
                Marshal.WriteByte(unmangedMemory, i, bytes[i]);
            }

            Marshal.WriteByte(unmangedMemory, bytes.Length, 0);
            Marshal.WriteByte(unmangedMemory, bytes.Length + 1, 0);

            return(unmangedMemory);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Convert the passed string to Utf16 encoded string
        /// written to memory allocated by xpcom/xulrunner itself.
        /// </summary>
        /// <param name="ManagedObj"></param>
        /// <returns>ptr to native memory containing utf16 encoded string + null term</returns>
        public IntPtr MarshalManagedToNative(object ManagedObj)
        {
            var str = ManagedObj as string;

            if (str == null)
            {
                return(IntPtr.Zero);
            }
            // unicode char is 2 bytes and 2 bytes '\0\0'
            byte[] bytes = new byte[str.Length * 2 + 2];
            // translate string into bytes
            Encoding.Unicode.GetBytes(str, 0, str.Length, bytes, 0);
            // allocate memory by xulrunner runtime
            IntPtr unmanagedMemory = Xpcom.Alloc(new IntPtr(bytes.Length));

            // copy byte array into unmanaged memory
            Marshal.Copy(bytes, 0, unmanagedMemory, bytes.Length);
            return(unmanagedMemory);
        }