Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6380617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:17:25+00:00 2026-05-25T02:17:25+00:00

Im doing some test creating an instance of HTMLDocument this way: object[] pageText =

  • 0

Im doing some test creating an instance of HTMLDocument this way:

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

and need to get a reference to a IMarkupServices.

This is the code I’m currently 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 i run it (it’s hosted in a Console App) the following exception is thrown:

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

Note:
What i’m trying to do is to unit test some objects i have implemented with mshtml. When i run the same code in an BHO (within Internet Explorer) it works fine.

Thank you very much

Edit: here´s the code I finaly used to get this working

I got it working by inspecting the 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 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);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:17:26+00:00Added an answer on May 25, 2026 at 2:17 am

    QI for IMarkupServices after you loaded the document via the IPersistStreamInit interface, preferably with a Url moniker as a proxy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking in to doing some test driven development for one of
I am doing some simple sanity validation on various types. The current test I'm
I've been doing some HTML scraping in PHP using regular expressions. This works, but
After doing some work with Ruby, Rails, and RSpec last summer and I learned
I'm doing some PHP stuff on an Ubuntu server. The path I'm working in
I'm doing some funky authentication work (and yes, I know, open-id is awesome, but
Currently I'm doing some unit tests which are executed from bash. Unit tests are
I am doing some research on Unicode for a white-paper I am writing. Does
I've been doing some mocking with RhinoMocks and it requires that mocked methods be
I'm doing some Android development, and I much prefer Visual Studio, but I'll have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.