Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
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());
        }