/// <include file='doc\VsService.uex' path='docs/doc[@for="VsService._LoadState"]/*' /> /// <devdoc> /// Called by the package manager to allow this service to load state out of the solutions /// persistance file. /// </devdoc> internal virtual void _LoadState(NativeMethods.IStream pStream) { try { NativeMethods.DataStreamFromComStream writeStream = new NativeMethods.DataStreamFromComStream(pStream); LoadState(writeStream); writeStream.Dispose(); } catch (Exception e) { Debug.Fail("Service " + this.GetType().Name + " threw an exception loading state: " + e.ToString()); } }
/// <summary> /// Loads an image (of any format, including PNG or JPEG) that is embedded in a DLL. /// </summary> /// <param name="loader"> /// A <see cref="ResourceLoader"/> instance used to load the resource. /// </param> /// <param name="resourceTypeName"> /// The name of the resource type (for example, <c>IMAGE</c>). /// </param> /// <param name="resourceName"> /// The name (as used in the RC file) of the resource to load. /// </param> /// <returns> /// A <see cref="Bitmap"/> instance that must be properly disposed to avoid leaking memory. /// </returns> public static Bitmap LoadImageFromResource(ResourceLoader loader, string resourceTypeName, string resourceName) { IntPtr hResInfo; using (HGlobal namePtr = HGlobal.WithStringUni(resourceName)) { hResInfo = NativeMethods.FindResourceW(loader.ModuleHandle, resourceTypeName, namePtr.Handle); } IntPtr hResData = NativeMethods.LoadResource(loader.ModuleHandle, hResInfo); IntPtr data = NativeMethods.LockResource(hResData); int size = NativeMethods.SizeofResource(loader.ModuleHandle, hResData); if (RuntimeInformation.FrameworkDescription.Contains(".NET Native")) { throw new PlatformNotSupportedException(); } else { byte[] buffer = new byte[size]; Marshal.Copy(data, buffer, 0, size); IntPtr hMem = Marshal.AllocHGlobal(size); Marshal.Copy(buffer, 0, hMem, size); NativeMethods.IStream stream = null; try { int hr = NativeMethods.CreateStreamOnHGlobal(hMem, true, out stream); if (hr != 0) { Marshal.ThrowExceptionForHR(hr); } NativeMethods.IWICImagingFactory factory = (NativeMethods.IWICImagingFactory) new NativeMethods.WICImagingFactory(); NativeMethods.IWICBitmapDecoder decoder = factory.CreateDecoderFromStream(stream, options: NativeMethods.WICDecodeOptions.WICDecodeMetadataCacheOnDemand); decoder.GetFrame(0, out NativeMethods.IWICBitmapFrameDecode frame); return(LoadImageFromBitmapSource((NativeMethods.IWICBitmapSource)frame)); } finally { if (stream != null) { Marshal.ReleaseComObject(stream); } } } }
/// <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 (!this.IsCreated) { throw new Exception("HtmlControl.SaveHtml : No HTML to save!"); } string content = String.Empty; try { NativeMethods.IHTMLDocument2 document = this.site.Document; // First save the document to a stream NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit)document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); NativeMethods.IStream stream = null; NativeMethods.CreateStreamOnHGlobal(NativeMethods.NullIntPtr, true, out stream); psi.Save(stream, 1); // Now copy the stream to the string NativeMethods.STATSTG stat = new NativeMethods.STATSTG(); stream.Stat(stat, 1); int length = (int)stat.cbSize; byte[] bytes = new byte[length]; IntPtr hglobal; NativeMethods.GetHGlobalFromStream(stream, out hglobal); Debug.Assert(hglobal != NativeMethods.NullIntPtr, "Failed in GetHGlobalFromStream"); // First copy the stream to a byte array IntPtr pointer = NativeMethods.GlobalLock(hglobal); if (pointer != NativeMethods.NullIntPtr) { Marshal.Copy(pointer, bytes, 0, length); NativeMethods.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(); } } } } catch (Exception exception) { Debug.Fail("HtmlControl.SaveHtml" + exception.ToString()); content = String.Empty; } finally { } if (content == null) { content = String.Empty; } return(content); /* * HtmlFormatter formatter = new HtmlFormatter(); * StringWriter writer = new StringWriter(); * formatter.Format(content, writer); * return writer.ToString(); */ }
/* * public void LoadHtml(string content, string url) * { * this.LoadHtml(content, url, null); * } */ // 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) { if (content == null) { content = ""; } if (!this.isCreated) { this.desiredContent = content; this.desiredUrl = url; this.desiredLoad = true; return; } NativeMethods.IStream stream = null; //First we create a COM stream IntPtr hglobal = Marshal.StringToHGlobalUni(content); NativeMethods.CreateStreamOnHGlobal(hglobal, true, out stream); // Initialize a new document if there is nothing to load if (stream == null) { NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit) this.site.Document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.InitNew(); psi = null; } else { NativeMethods.IHTMLDocument2 document = this.site.Document; if (url == null) { // If there is no specified URL load the document from the stream. NativeMethods.IPersistStreamInit psi = (NativeMethods.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. NativeMethods.IPersistMoniker persistMoniker = (NativeMethods.IPersistMoniker)document; NativeMethods.IMoniker moniker = null; NativeMethods.CreateURLMoniker(null, url, out moniker); NativeMethods.IBindCtx bindContext = null; NativeMethods.CreateBindCtx(0, out bindContext); persistMoniker.Load(1, moniker, bindContext, 0); persistMoniker = null; moniker = null; bindContext = null; } } this.url = url; }
/* * public void LoadHtml(string content, string url) * { * this.LoadHtml(content, url, null); * } */ // 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) { if (content == null) { content = ""; } if (!this.isCreated) { this.desiredContent = content; this.desiredUrl = url; this.desiredLoad = true; return; } NativeMethods.IStream stream = null; // added by Erik Frey - UTF preamble byte[] preamble = UnicodeEncoding.Unicode.GetPreamble(); string byteOrderMark = UnicodeEncoding.Unicode.GetString(preamble, 0, preamble.Length); // i guess .NET 2.0 fixed up unicode string handling if (System.Environment.Version.Major > 1 || !content.StartsWith(byteOrderMark)) { content = byteOrderMark + content; } //First we create a COM stream IntPtr hglobal = Marshal.StringToHGlobalUni(content); NativeMethods.CreateStreamOnHGlobal(hglobal, true, out stream); // Initialize a new document if there is nothing to load if (stream == null) { NativeMethods.IPersistStreamInit psi = (NativeMethods.IPersistStreamInit) this.site.Document; Debug.Assert(psi != null, "Expected IPersistStreamInit"); psi.InitNew(); psi = null; } else { NativeMethods.IHTMLDocument2 document = this.site.Document; if (url == null) { // If there is no specified URL load the document from the stream. NativeMethods.IPersistStreamInit psi = (NativeMethods.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. NativeMethods.IPersistMoniker persistMoniker = (NativeMethods.IPersistMoniker)document; NativeMethods.IMoniker moniker = null; NativeMethods.CreateURLMoniker(null, url, out moniker); NativeMethods.IBindCtx bindContext = null; NativeMethods.CreateBindCtx(0, out bindContext); persistMoniker.Load(1, moniker, bindContext, 0); persistMoniker = null; moniker = null; bindContext = null; } } this.url = url; }
/// <include file='doc\VsService.uex' path='docs/doc[@for="VsService._SaveState"]/*' /> /// <devdoc> /// Called by the package manager to allow this service to persist state into the solution's /// persistance file. /// </devdoc> internal virtual void _SaveState(NativeMethods.IStream pStream) { NativeMethods.DataStreamFromComStream comStream = new NativeMethods.DataStreamFromComStream(pStream); SaveState(comStream); comStream.Dispose(); }