Example #1
0
        //REVIEW: Add a load method for stream and url

        /// <summary>
        /// Loads HTML content from a string into this control identified by the specified URL.
        /// If MSHTML has not yet been created, the loading is postponed until MSHTML has been created.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="url"></param>
        public void LoadHtml(string content, string url, string style)
        {
            if (content == null)
            {
                content = "";
            }

            if (!_isCreated)
            {
                _desiredContent = content;
                _desiredUrl     = url;
                _loadDesired    = true;
                return;
            }

            if (_fullDocumentMode == false)
            {
                content = CreateHtmlContent(content, style);
            }

            OnBeforeLoad();

            Interop.IStream stream = null;

            //First we create a COM stream
            IntPtr hglobal = Marshal.StringToHGlobalUni(content);

            Interop.CreateStreamOnHGlobal(hglobal, true, out stream);

            // Initialize a new document if there is nothing to load
            if (stream == null)
            {
                Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)_site.MSHTMLDocument;
                Debug.Assert(psi != null, "Expected IPersistStreamInit");
                psi.InitNew();
                psi = null;
            }
            else
            {
                Interop.IHTMLDocument2 document = _site.MSHTMLDocument;
                //If there is no specified URL load the document from the stream
                if (url == null)
                {
                    Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)document;
                    Debug.Assert(psi != null, "Expected IPersistStreamInit");
                    psi.Load(stream);
                    psi = null;
                }
                else
                {
                    //Otherwise we create a moniker and load the stream to that moniker
                }
            }
            _url = url;

            OnAfterLoad();
        }
Example #2
0
 public ComStream OpenStream(string streamName, ComStorage.OpenMode openMode)
 {
     this.CheckDisposed("ComStorage::OpenStream");
     Util.ThrowOnNullArgument(streamName, "streamName");
     ComStorage.CheckOpenMode(openMode, "openMode", ComStorage.validOpenModes);
     Interop.IStream iStream = null;
     Util.InvokeComCall(MsgStorageErrorCode.FailedOpenStream, delegate
     {
         this.iStorage.OpenStream(streamName, IntPtr.Zero, (uint)openMode, 0U, out iStream);
     });
     return(new ComStream(iStream));
 }
Example #3
0
 public unsafe byte[] ReadFromStreamMaxLength(string streamName, int maxSize)
 {
     byte[] result = null;
     Util.InvokeComCall(MsgStorageErrorCode.FailedRead, delegate
     {
         Interop.IStream stream = null;
         try
         {
             this.iStorage.OpenStream(streamName, IntPtr.Zero, 16U, 0U, out stream);
             System.Runtime.InteropServices.ComTypes.STATSTG statstg;
             stream.Stat(out statstg, 1U);
             if (statstg.cbSize > (long)maxSize)
             {
                 throw new MsgStorageException(MsgStorageErrorCode.StorageStreamTooLong, MsgStorageStrings.StreamTooBig(streamName, statstg.cbSize));
             }
             int num  = (int)statstg.cbSize;
             int num2 = 0;
             result   = new byte[num];
             if (result.Length != 0)
             {
                 try
                 {
                     fixed(byte *ptr = &result[0])
                     {
                         stream.Read(ptr, result.Length, out num2);
                     }
                 }
                 finally
                 {
                     byte *ptr = null;
                 }
             }
             if (num2 != result.Length)
             {
                 throw new MsgStorageException(MsgStorageErrorCode.StorageStreamTruncated, MsgStorageStrings.FailedRead(streamName));
             }
         }
         finally
         {
             if (stream != null)
             {
                 Marshal.ReleaseComObject(stream);
             }
         }
     });
     return(result);
 }
Example #4
0
        public unsafe int ReadFromStream(string streamName, byte[] buffer, int size)
        {
            this.CheckDisposed("ComStream::ReadFromStream");
            Util.ThrowOnNullArgument(streamName, "streamName");
            Util.ThrowOnNullArgument(buffer, "buffer");
            Util.ThrowOnOutOfRange(size >= 0, "size");
            int result = 0;

            Util.InvokeComCall(MsgStorageErrorCode.FailedRead, delegate
            {
                Interop.IStream stream = null;
                try
                {
                    this.iStorage.OpenStream(streamName, IntPtr.Zero, 16U, 0U, out stream);
                    int result = 0;
                    if (size != 0)
                    {
                        try
                        {
                            fixed(byte *ptr = &buffer[0])
                            {
                                stream.Read(ptr, size, out result);
                            }
                        }
                        finally
                        {
                            byte *ptr = null;
                        }
                    }
                    result = result;
                }
                finally
                {
                    if (stream != null)
                    {
                        Marshal.ReleaseComObject(stream);
                    }
                }
            });
            return(result);
        }
Example #5
0
 public unsafe void WriteBytesToStream(string streamName, byte[] data, int length)
 {
     this.CheckDisposed("ComStream::WriteToStream");
     Util.ThrowOnNullArgument(streamName, "streamName");
     Util.ThrowOnNullArgument(data, "data");
     Util.InvokeComCall(MsgStorageErrorCode.FailedWrite, delegate
     {
         Interop.IStream stream = null;
         try
         {
             int num = 0;
             this.iStorage.CreateStream(streamName, 17U, 0U, 0U, out stream);
             if (length != 0)
             {
                 try
                 {
                     fixed(byte *ptr = &data[0])
                     {
                         stream.Write(ptr, length, out num);
                     }
                 }
                 finally
                 {
                     byte *ptr = null;
                 }
             }
             if (num != length)
             {
                 throw new MsgStorageException(MsgStorageErrorCode.FailedWrite, MsgStorageStrings.FailedWrite(streamName));
             }
         }
         finally
         {
             if (stream != null)
             {
                 Marshal.ReleaseComObject(stream);
             }
         }
     });
 }
Example #6
0
 internal ComStream(Interop.IStream iStream)
 {
     this.iStream    = iStream;
     this.isDisposed = false;
 }
Example #7
0
        /// <summary>
        /// Saves the HTML contained in control to a string and return it.
        /// </summary>
        /// <returns>string - The HTML in the control</returns>
        public string SaveHtml()
        {
            if (!IsCreated)
            {
                throw new Exception("HtmlControl.SaveHtml : No HTML to save!");
            }

            string content = String.Empty;

            try
            {
                OnBeforeSave();

                Interop.IHTMLDocument2 document = _site.MSHTMLDocument;

                if (_fullDocumentMode)
                {
                    // First save the document to a stream
                    Interop.IPersistStreamInit psi = (Interop.IPersistStreamInit)document;
                    Debug.Assert(psi != null, "Expected IPersistStreamInit");

                    Interop.IStream stream = null;
                    Interop.CreateStreamOnHGlobal(Interop.NullIntPtr, true, out stream);

                    psi.Save(stream, 1);

                    // Now copy the stream to the string
                    STATSTG stat = new STATSTG();
                    stream.Stat(stat, 1);
                    int    length = (int)stat.cbSize;
                    byte[] bytes  = new byte[length];

                    IntPtr hglobal;
                    Interop.GetHGlobalFromStream(stream, out hglobal);
                    Debug.Assert(hglobal != Interop.NullIntPtr, "Failed in GetHGlobalFromStream");

                    // First copy the stream to a byte array
                    IntPtr pointer = Interop.GlobalLock(hglobal);
                    if (pointer != Interop.NullIntPtr)
                    {
                        Marshal.Copy(pointer, bytes, 0, length);

                        Interop.GlobalUnlock(hglobal);

                        // Then create the string from the byte array (use a StreamReader to eat the preamble in the UTF8 encoding case)
                        StreamReader streamReader = null;
                        try
                        {
                            streamReader = new StreamReader(new MemoryStream(bytes), Encoding.Default);
                            content      = streamReader.ReadToEnd();
                        }
                        finally
                        {
                            if (streamReader != null)
                            {
                                streamReader.Close();
                            }
                        }
                    }
                }
                else
                {
                    // Save only the contents of the <body> tag
                    Interop.IHTMLElement bodyElement = document.GetBody();
                    Debug.Assert(bodyElement != null, "Could not get BODY element from document");

                    if (bodyElement != null)
                    {
                        content = SavePartialHtml(Element.GetWrapperFor(bodyElement, this));
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Fail("HtmlControl.SaveHtml" + e.ToString());
                content = String.Empty;
            }
            finally
            {
                OnAfterSave();
            }

            if (content == null)
            {
                content = String.Empty;
            }
            HtmlFormatter formatter = new HtmlFormatter();
            StringWriter  writer    = new StringWriter();

            formatter.Format(content, writer, new HtmlFormatterOptions(' ', 4, 80, true));

            return(writer.ToString());
        }