MSHTML IMarkupServices - C++ Conversion help required

Can't get the below C++ translated correctly.

HRESULT GetMarkupServices(IDispatch *pDocument, IMarkupServices ** pMarkupServices)
{
    CComQIPtr<IHTMLDocument2>    pDoc;
    CComQIPtr<IHTMLWindow2>      pWindow;
    CComQIPtr<IServiceProvider>  pService;
    HRESULT                      hr = S_OK;
      
    pDoc = pDocument;
      if( ! pDoc)
            return E_FAIL;

    hr = pDoc->get_parentWindow( &pWindow );
    if (hr)
            return E_FAIL;
      
    pService = pWindow;
    if ( !pService )
            return E_FAIL;
      
    hr = pService->QueryService( CLSID_HTMLDocument,
            IID_IMarkupServices,
            (void **) pMarkupServices);
    if (hr)
            return E_FAIL;
      return S_OK;
}


Here's my Delphi translation but it keeps failing on line marked,
know what I'm doing wrong?

function GetMarkupServices(Document: IHTMLDocument2): IMarkupServices;
var
  pWindow:         IHTMLWindow2;
  pService:        IServiceProvider;
  pMarkupServices: IMarkupServices;
  hr:              HRESULT;
begin
  if Document <> nil then
  begin
    pWindow := Document.get_parentWindow;
    if (pWindow = nil) then
    begin
      Result := nil;
      Exit;
    end;
    {
      Tried using QueryInterface but still doesn't work,
      pMarkupServices is always nil

    pWindow.QueryInterface(IID_IMarkupServices, pMarkupServices);
    if pMarkupServices <> nil then
    begin
      Result := pMarkupServices;
      Exit;
    end;
    }
    pService := pWindow as IServiceProvider;
    if ( pService = nil) then
    begin
      Result := nil;
      Exit;
    end;
    // Fails here! Fails by saying that pMarkupServices is returning nil and hr <> S_OK.
    hr := pService.QueryService(IID_IHTMLDocument2, IID_IMarkupServices, pMarkupServices);  
    if (hr <> S_OK) then
    begin
      Result := nil;
      Exit;
    end;
    Result := pMarkupServices;
  end;
end;
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
 
Russell LibbyConnect With a Mentor Software Engineer, Advisory Commented:
The following works fine for me...

---------

Russell

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ShellApi, OleCtrls, MSHTML_TLB, SHDocVw;


type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  IID_IServiceProvider:   TGUID =  '{6d5140c1-7436-11ce-8034-00aa006009fa}';

type
  IServiceProvider  =  interface(IUnknown)
    ['{6d5140c1-7436-11ce-8034-00aa006009fa}']
    function QueryService(const guidService, riid: TGUID; out pvObject): HResult; stdcall;
  end;

var
  Form1:         TForm1;

implementation
{$R *.DFM}

function GetMarkupServices(Document: IDispatch): IMarkupServices;
var  pvDocument:    IHTMLDocument2;
     pvWindow:      IHTMLWindow2;
     pvService:     IServiceProvider;
begin

  // Set default result
  result:=nil;

  // Check pointer
  if Assigned(Document) then
  begin
     // Resource protection
     try
        // Safe query (does not raise like delphi's "as")
        if (Document.QueryInterface(IHTMLDocument2, pvDocument) = S_OK) then
        begin
           // Get parent window
           pvWindow:=pvDocument.parentWindow;
           if Assigned(pvWindow) then
           begin
              // Window does expose service provider
              pvService:=pvWindow as IServiceProvider;
              // Query service
              pvService.QueryService(CLASS_HTMLDocument, IMarkupServices, result);
           end;
        end;
     finally
        // Release interfaces
        pvService:=nil;
        pvWindow:=nil;
        pvDocument:=nil;
     end;
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var  pvMarkup:      IMarkupServices;
begin
  pvMarkup:=GetMarkupServices(WebBrowser1.Document);
  if Assigned(pvMarkup) then
  begin
     ShowMessage('Ready to go...');
     pvMarkup:=nil;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.google.com');
end;

end.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.