public static void LoadObjectFromStream(object obj, Stream stm) { IStreamImpl istm = new IStreamImpl(stm); IPersistStream ps = obj as IPersistStream; if (ps != null) { ps.Load(istm); } else { IPersistStreamInit psi = (IPersistStreamInit)obj; psi.InitNew(); psi.Load(istm); } }
public static void LoadDocument(ref HTMLDocument doc, String documentVal, bool LoadAsAnsi, bool CreateSite, Encoding EncodingToAddPreamble) { if (doc == null) { throw new HtmlEditorException("Null document passed to LoadDocument"); } IHTMLDocument2 htmldoc = (IHTMLDocument2)doc; bool isWin98 = (System.Environment.OSVersion.Platform == PlatformID.Win32Windows); if (documentVal == string.Empty) { documentVal = "<html></html>"; } if (CreateSite) { //set client site to DownloadOnlySite, to suppress scripts DownloadOnlySite ds = new DownloadOnlySite(); IOleObject ob = (IOleObject)doc; ob.SetClientSite(ds); } IStream stream = null; if (isWin98 | LoadAsAnsi) { win32.CreateStreamOnHGlobal(Marshal.StringToHGlobalAnsi(documentVal), 1, out stream); } else { if (!isBOMPresent(documentVal)) //add bytemark if needed { if (EncodingToAddPreamble != null) { byte[] preamble = EncodingToAddPreamble.GetPreamble(); String byteOrderMark = EncodingToAddPreamble.GetString(preamble, 0, preamble.Length); documentVal = byteOrderMark + documentVal; } } win32.CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(documentVal), 1, out stream); } if (stream == null) { throw new HtmlEditorException("Could not allocate document stream"); } if (isWin98) { //fix string termination on Win98 ulong thesize = 0; IntPtr ptr = IntPtr.Zero; int iSizeOfInt64 = Marshal.SizeOf(typeof(Int64)); ptr = Marshal.AllocHGlobal(iSizeOfInt64); if (ptr == IntPtr.Zero) { throw new HtmlEditorException("Could not load document"); } //seek to end of stream stream.Seek(0, 2, ptr); //read the size thesize = (ulong)Marshal.ReadInt64(ptr); //free the pointer Marshal.FreeHGlobal(ptr); //truncate the stream stream.SetSize((long)thesize); //2nd param, 0 is beginning, 1 is current, 2 is end if (thesize != (ulong)documentVal.Length + 1) { //fix the size by truncating the stream Debug.Assert(true, "Size of stream is unexpected", "The size of the stream is not equal to the length of the string passed to it."); stream.SetSize(documentVal.Length + 1); } } //set stream to start stream.Seek(0, 0, IntPtr.Zero); //2nd param, 0 is beginning, 1 is current, 2 is end IPersistStreamInit persistentStreamInit = (IPersistStreamInit) doc; if (persistentStreamInit != null) { int iRetVal = 0; iRetVal = persistentStreamInit.InitNew(); if (iRetVal == HRESULT.S_OK) { iRetVal = persistentStreamInit.Load(stream); if (iRetVal != HRESULT.S_OK) { throw new HtmlEditorException("Could not load document"); } } else { throw new HtmlEditorException("Could not load document"); } persistentStreamInit = null; } else { throw new HtmlEditorException("Could not load document"); } stream = null; }
public void LoadDocument(String documentVal) { if ((documentVal != string.Empty) | (!this.bLoadDocumentWhenReady)) { //if doc is waiting to load, it is already in string variable sDocument = documentVal; this.bLoadDocumentWhenReady = false; } else { this.bLoadDocumentWhenReady = false; this.iLoadAttempts += 1; } if (!IsCreated) { throw new HtmlEditorException("Document not created"); } if ((this.m_htmldoc.readyState.ToLower() != "complete") & (this.m_htmldoc.readyState.ToLower() != "interactive")) //try to load on interactive as well as complete { if (iLoadAttempts < 2) { this.bLoadDocumentWhenReady = true; return; } else { throw new HtmlEditorException("Document not ready"); } } if ((sDocument == null) || (sDocument == String.Empty)) { sDocument = "<html><body></body></html>"; } UCOMIStream stream = null; if (this.mIsWin98 | this.mAlwaysLoadAnsi) { ComSupport.CreateStreamOnHGlobal(Marshal.StringToHGlobalAnsi(sDocument), 1, out stream); } else { ComSupport.CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(sDocument), 1, out stream); } if (stream == null) { throw new HtmlEditorException("Could not allocate document stream"); } if (this.mIsWin98) { //This code fixes a problem in Win98 - Framework bug? - where the string //is sometimes incorrectly terminated //It assumes an ANSI string ulong thesize = 0; IntPtr ptr = IntPtr.Zero; int iSizeOfIntPtr = Marshal.SizeOf(typeof(Int64)); ptr = Marshal.AllocHGlobal(iSizeOfIntPtr); if (ptr == IntPtr.Zero) { throw new HtmlEditorException("Could not load document"); } //seek to end of stream stream.Seek(0, 2, ptr); //read the size thesize = (ulong)Marshal.ReadInt64(ptr); //free the pointer Marshal.FreeHGlobal(ptr); //2nd param, 0 is beginning, 1 is current, 2 is end if (thesize != (ulong)sDocument.Length + 1) { //fix the size by truncating the stream Debug.Assert(true, "Size of stream is unexpected", "The size of the stream is not equal to the length of the string passed to it."); stream.SetSize((sDocument.Length) + 1); } } //set stream to start stream.Seek(0, 0, IntPtr.Zero); IPersistStreamInit persistentStreamInit = (IPersistStreamInit) this.m_htmldoc; if (persistentStreamInit != null) { int iRetVal = 0; iRetVal = persistentStreamInit.InitNew(); if (iRetVal == HRESULT.S_OK) { //this is a fix for exception raised in UpdateUI site.mFullyActive = false; iRetVal = persistentStreamInit.Load(stream); if (iRetVal != HRESULT.S_OK) { throw new HtmlEditorException("Could not load document"); } } else { throw new HtmlEditorException("Could not load document"); } persistentStreamInit = null; } else { throw new HtmlEditorException("Could not load document"); } stream = null; this.iLoadAttempts = 0; }