.net - How to obtain IServiceProvider and IMarkupServices from HTMLDocument (mshtml) -
im doing test creating instance of htmldocument way:
object[] pagetext = { "<p>some text...</p>" }; var document = new htmldocumentclass(); var document2 = (ihtmldocument2)document; document2.write(pagetext);
and need reference imarkupservices.
this code i'm using:
guid iid_imarkupservices = new guid("3050f4a0-98b5-11cf-bb82-00aa00bdce0b"); imarkupservices markupservices = getservice<imarkupservices>(document, id_imarkupservices); static guid htmldocumentclassguid = new guid("25336920-03f9-11cf-8fd0-00aa00686f13"); private static t getservice<t>(ihtmldocument2 document, guid riid) { var serviceprovider = (iserviceprovider) document; object service; serviceprovider.queryservice(ref htmldocumentclassguid, ref riid, out service); return (t)service; }
when run (it's hosted in console app) following exception thrown:
unhandled exception: system.invalidcastexception: unable cast com object of type 'mshtml.htmldocumentclass' interface type 'iserviceprovider'. operation failed because queryinterface call on com component interface iid '{4c9a623c-ff69-3a3b-b592-43371c50df88}' failed due following error: no such interface supported (exception hresult: 0x80004002 (e_nointerface)). @ consoleapplication3.program.getservice[t](ihtmldocument2 document, guid riid) in c:\users\admin\documents\visual studio 010\projects\consoleapplication3\consoleapplication3\program.cs:line 35 @ consoleapplication3.program.main(string[] args) in c:\users\admin\documents\visual studio 2010\projects\consoleapplication3\consoleapplication3\program.cs:line 29
note: i'm trying unit test objects have implemented mshtml. when run same code in bho (within internet explorer) works fine.
thank much
edit: here´s code finaly used working
i got working inspecting following watin's implementation: http://www.java2s.com/open-source/csharp/web-testing/watin/watin/examples/mshtmlbrowser/mshtmlnativebrowser.cs.htm based on sheng jiang helpful answer.
public class program { [guid("7fd52380-4e07-101b-ae2d-08002b2ec713")] [interfacetypeattribute(cominterfacetype.interfaceisiunknown)] public interface ipersiststreaminit { void getclassid(out guid pclassid); int isdirty(); void load(system.runtime.interopservices.comtypes.istream pstm); void save(system.runtime.interopservices.comtypes.istream pstm, bool fcleardirty); void getsizemax(out long pcbsize); void initnew(); } [stathread] static void main(string[] args) { var anhtmldocument = new htmldocumentclass(); var apersiststream = (wb.program.ipersiststreaminit)anhtmldocument; apersiststream.initnew(); var anhtmldocument2 = (ihtmldocument2)anhtmldocument; anhtmldocument2.write(new object[] { "test <b> foo </b>" }); anhtmldocument2.close(); while (anhtmldocument.readystate != "complete") { //this important part, without doevents() appz hangs on “loading” application.doevents(); } var amarkupservice = (imarkupservices)anhtmldocument; imarkuppointer apointer; amarkupservice.createmarkuppointer(out apointer); var anhtmlbody = (ihtmlbodyelement)anhtmldocument.body; var aselection = anhtmlbody.createtextrange(); aselection.findtext("foo", 0, 0); } }
qi imarkupservices after loaded document via ipersiststreaminit interface, preferably url moniker proxy.
Comments
Post a Comment