Using MSHTML.IHTMLElementRender from VB
|
|
Thread rating:  |
Garett - 25 Feb 2008 19:18 GMT Hi,
I am getting a type mismatch error when attempting to use the IHTMLElementRender inteface from VB6. I noticed that this interface does not implement IDispatch, just IUnknown. Does this have something to do with it?
Also, I had to create a type library with the IHTMLElementRender::DrawToDC definition, which accepts a long, because the definition that is provided with the MSHTML type library uses a parameter type called RemoteHandle, which VB does not recognize. In this type library I inherited from stdole.IUnknown instead. Could this be a problem? Thanks in advanced for all the help.
Sample code is provided below:
Type library definitions:
typedef LONG HDC;
[ odl, uuid(3050F669-98B5-11CF-BB82-00AA00BDCE0B) ] interface IHTMLElementRender : stdole.IUnknown { HRESULT DrawToDC([in] HDC hdc); HRESULT SetDocumentPrinter( [in] BSTR bstrPrinterName, [in] HDC hdc); };
VB Code:
Public Function CreateWebPageImage() As Object Dim lErrorNum As Long Dim sSource As String Dim sDescription As String
Dim pDocument As MSHTML.IHTMLDocument2 Dim pBody As MSHTML.IHTMLElement2 Dim pRender As cptsoleapi.IHTMLElementRender ' type library containing new definition of IHTMLElementRender Dim hwndBrowser As Long
On Error GoTo ERROR_HANDLER
Set pDocument = Parent.Script.document Set pBody = pDocument.body
'hwndBrowser = GetBrowserWindow()
'Call LockWindowUpdate(hwndBrowser)
'Call GetBodyElement(pBody)
If Not pBody Is Nothing Then Set pRender = pBody ' This causes a type mismatch error
End If
CLEAN_UP: On Error Resume Next
If lErrorNum <> 0 Then On Error GoTo 0
Call Err.Raise(lErrorNum, sSource, sDescription) End If
' unlock window 'Call LockWindowUpdate(0)
Exit Function ERROR_HANDLER: lErrorNum = Err.Number sSource = Err.Source sDescription = Err.Description
Resume CLEAN_UP End Function
Regards,
Garett
Anthony Jones - 26 Feb 2008 12:06 GMT > Hi, > [quoted text clipped - 75 lines] > Resume CLEAN_UP > End Function The fact that IHTMLElementRender derives from IUnknown would not cause a type mismatch. However,according mshtml.tlb HTMLbody doesn't implement IHTMLElementRender, in which case that would cause a type mismatch.
 Signature Anthony Jones - MVP ASP/ASP.NET
Garett - 26 Feb 2008 14:00 GMT Hi Anthony,
Thanks for the reply. You are correct, I looked at mshtml.tlb and I realized this, but now I'm a little confused. There are quite a few examples out there, written in other languages admittedly, that use a similar approach as I am attempting. For example,
http://www.codeproject.com/KB/IP/htmlimagecapture.aspx?msg=783507 http://www.developerfusion.co.uk/show/4712/ http://blogs.msdn.com/rfarber/archive/2004/10/12/240943.aspx http://www.delphipages.com/tips/thread.cfm?ID=292&SR=html
Maybe there some subletly in these approaches that I am missing? Maybe I need to navigate the DOM from the IWebBrowser2 interface? Thanks again from the response.
Regards,
Garett
>> Hi, >> [quoted text clipped - 84 lines] > type mismatch. However,according mshtml.tlb HTMLbody doesn't implement > IHTMLElementRender, in which case that would cause a type mismatch. Anthony Jones - 28 Feb 2008 08:32 GMT > Hi Anthony, > [quoted text clipped - 7 lines] > http://blogs.msdn.com/rfarber/archive/2004/10/12/240943.aspx > http://www.delphipages.com/tips/thread.cfm?ID=292&SR=html I see. It is possible for QueryInterface to respond positively to a request for an interface that isn't listed in the type library. From those articles it does look like it ought to work.
> Maybe there some subletly in these approaches that I am missing? Maybe I > need to navigate the DOM from the IWebBrowser2 interface? Thanks again from > the response.
 Signature Anthony Jones - MVP ASP/ASP.NET
Ajay Porwal - 14 Apr 2008 15:31 GMT Hi, I am trying to convert html text into an image, below is my code:
using System; using System.Collections.Generic; using System.Text; using mshtml; using System.Drawing; using System.Runtime.InteropServices; using System.Drawing.Imaging; using SHDocVw; namespace WhiteHallClasses { [ Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComVisible(true), ComImport ] interface IHTMLElementRender { void DrawToDC([In] IntPtr hDC); void SetDocumentPrinter([In, MarshalAs(UnmanagedType.BStr)] string bstrPrinterName, [In] IntPtr hDC); };
public class Class1 { public void Draw() {
HTMLDocumentClass hc = new HTMLDocumentClass(); IHTMLBodyElement b = (IHTMLBodyElement)hc.createElement("body"); b.text = "<b>Hi</b>"; IHTMLElementRender render = (IHTMLElementRender)b;
Bitmap oCanvas = new Bitmap(850, 1100); Graphics g = Graphics.FromImage(oCanvas); g.FillRectangle(System.Drawing.Brushes.Bisque, 0, 0, 850, 1100); g.Clear(Color.BurlyWood); IntPtr memDC = g.GetHdc(); render.DrawToDC(memDC); string TempBlankPage = "c:\\" + System.Guid.NewGuid().ToString() + "BlankPage.tif"; oCanvas.Save(TempBlankPage, ImageFormat.Tiff);
g.Dispose(); oCanvas.Dispose(); } } }
I am getting the exception on line IHTMLElementRender render = (IHTMLElementRender)b;
The exception detail is as below:
Unable to cast COM object of type 'mshtml.HTMLBodyClass' to interface type 'WhiteHallClasses.IHTMLElementRender'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F669-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Could you please me, what's wrong in my sample code. I refered many articles and forums but no luck till now.....
Ralph - 14 Apr 2008 15:48 GMT > Hi, I am trying to convert html text into an image, below is my code: > [quoted text clipped - 7 lines] > using SHDocVw; > namespace WhiteHallClasses You need to post to a dotNet newsgroup. This newsgroup is for classic VB (VB6 and lower) and COM. Your question has nothing to do with any of that.
-ralph
Ajay Porwal - 14 Apr 2008 15:34 GMT Hi, I am trying to convert html text into an image, below is my code:
using System; using System.Collections.Generic; using System.Text; using mshtml; using System.Drawing; using System.Runtime.InteropServices; using System.Drawing.Imaging; using SHDocVw; namespace WhiteHallClasses { [ Guid("3050f669-98b5-11cf-bb82-00aa00bdce0b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComVisible(true), ComImport ] interface IHTMLElementRender { void DrawToDC([In] IntPtr hDC); void SetDocumentPrinter([In, MarshalAs(UnmanagedType.BStr)] string bstrPrinterName, [In] IntPtr hDC); };
public class Class1 { public void Draw() {
HTMLDocumentClass hc = new HTMLDocumentClass(); IHTMLBodyElement b = (IHTMLBodyElement)hc.createElement("body"); b.text = "<b>Hi</b>"; IHTMLElementRender render = (IHTMLElementRender)b;
Bitmap oCanvas = new Bitmap(850, 1100); Graphics g = Graphics.FromImage(oCanvas); g.FillRectangle(System.Drawing.Brushes.Bisque, 0, 0, 850, 1100); g.Clear(Color.BurlyWood); IntPtr memDC = g.GetHdc(); render.DrawToDC(memDC); string TempBlankPage = "c:\\" + System.Guid.NewGuid().ToString() + "BlankPage.tif"; oCanvas.Save(TempBlankPage, ImageFormat.Tiff);
g.Dispose(); oCanvas.Dispose(); } } }
I am getting the exception on line IHTMLElementRender render = (IHTMLElementRender)b;
The exception detail is as below:
Unable to cast COM object of type 'mshtml.HTMLBodyClass' to interface type 'WhiteHallClasses.IHTMLElementRender'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F669-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Could you please me, what's wrong in my sample code. I refered many articles and forums but no luck till now.....
Anthony Jones - 19 Apr 2008 21:30 GMT > Hi, I am trying to convert html text into an image, below is my code: > [quoted text clipped - 57 lines] > > Unable to cast COM object of type 'mshtml.HTMLBodyClass' to interface type 'WhiteHallClasses.IHTMLElementRender'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F669-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
> Could you please me, what's wrong in my sample code. I refered many articles and forums but no luck till now..... We'll it has me stumped as to how that previous set of references ever worked. Clearly the code is making a QI call for the correct interface and is being told its not implemented. This fact is backed up by the content of the mshtml.tlb indicates that the Body doesn't implement this interface.
It doesn't help you're using C#. Perhaps the best place for this question is microsoft.public.dotnet.framework.interop. There you should find people who are doing C# to COM library stuff a lot and they'll like have a better insight.
 Signature Anthony Jones - MVP ASP/ASP.NET
Ryan - 27 Aug 2008 18:23 GMT You can host System.Windows.Forms.WebBrowser in a form to get to IHTMLElementRender.
// method 1 IWebBrowser2 browser2 = ((IWebBrowser2)Browser.ActiveXInstance); IHTMLDocument3 doc3 = (IHTMLDocument3)browser2.Document; IHTMLElement element = (IHTMLElement)doc3.documentElement; IHTMLElementRender render = (IHTMLElementRender)element;
// method 2 IHTMLDocument2 doc2 = (IHTMLDocument2)browser2.Document; IHTMLElement doc2Element = (IHTMLElement)doc2.body; IHTMLElementRender doc2Render = (IHTMLElementRender)doc2Element;
// method 3 (frameset, code not verified) IHTMLElementCollection elementColl = (IHTMLElementCollection) doc3.getElementsByTagName("FRAMESET"); IHTMLElement framesetElement = (IHTMLElement) elementColl.item(0, 0); IHTMLElementRender framesetRender = (IHTMLElementRender) framesetElement;
Ryan S - 27 Aug 2008 18:28 GMT You can host System.Windows.Forms.WebBrowser in a form to get to IHTMLElementRender.
// method 1 IWebBrowser2 browser2 = ((IWebBrowser2)Browser.ActiveXInstance); IHTMLDocument3 doc3 = (IHTMLDocument3)browser2.Document; IHTMLElement element = (IHTMLElement)doc3.documentElement; IHTMLElementRender render = (IHTMLElementRender)element;
// method 2 IHTMLDocument2 doc2 = (IHTMLDocument2)browser2.Document; IHTMLElement doc2Element = (IHTMLElement)doc2.body; IHTMLElementRender doc2Render = (IHTMLElementRender)doc2Element;
// method 3 (frameset, code not verified) IHTMLElementCollection elementColl = (IHTMLElementCollection) doc3.getElementsByTagName("FRAMESET"); IHTMLElement framesetElement = (IHTMLElement) elementColl.item(0, 0); IHTMLElementRender framesetRender = (IHTMLElementRender) framesetElement;
Tony Proctor - 29 Aug 2008 11:10 GMT This is not a VB.Net newsgroup Ryan. It's for versions of VB up to and not including the VB.Net.
The "com" in the newsgroup name must have been a clue, eh? :-)
Tony Proctor
> You can host System.Windows.Forms.WebBrowser in a form to get to > IHTMLElementRender. [quoted text clipped - 18 lines] > > *** Sent via Developersdex http://www.developersdex.com ***
|
|
|