2011-06-30 3 views
2

イムのHTMLDocumentのインスタンスを作成し、このよういくつかのテストをやって:(MSHTML)

object[] pageText = { "<p>some text...</p>" }; 
var document = new HTMLDocumentClass(); 
var document2 = (IHTMLDocument2)document; 
document2.write(pageText); 

とIMarkupServicesへの参照を取得する必要があります。

この

は私が現在使用しているコードです:

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; 
} 

私はそれを(それがコンソールアプリケーションでホストされている)は、次の例外がスローされる実行時:

Unhandled Exception: System.InvalidCastException: Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'IServiceProvider'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{4C9A623C-FF69-3A3B-B592-43371C50DF88}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 
at ConsoleApplication3.Program.GetService[T](IHTMLDocument2 document, Guid riid) in c:\users\admin\documents\visual studio 010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 35 
at ConsoleApplication3.Program.Main(String[] args) in c:\users\admin\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 29 

注: 私がしようとしているのは、mshtmlで実装したオブジェクトのユニットテストです。 BHO(Internet Explorer内)で同じコードを実行するとうまく動作します。私はそれが次WatiNの実装を検査することにより、作業しまった私はfinalyこの作業

を取得するために使用されるコードをhere's:便利シェン・ジャンに基づいてhttp://www.java2s.com/Open-Source/CSharp/Web-Testing/WatiN/WatiN/Examples/MsHtmlBrowser/MsHtmlNativeBrowser.cs.htm

はあなたに非常に多くの

編集ありがとうございました回答。 IMarkupServicesため

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 is also a important part, without this DoEvents() appz hangs on to the “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); 
     } 
    } 

答えて

2

QIあなたは、好ましくは、プロキシとしてUrl monikerで、IPersistStreamInitインターフェイスを介してドキュメントをロードした後。

+0

ありがとう!私はそれを動作させるために使用したコードで質問を更新しました。 – nick2083

関連する問題