?
Solved

MSHTML IMarkupServices - C++ Conversion help required

Posted on 2005-04-25
1
Medium Priority
?
748 Views
Last Modified: 2011-10-03
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;
0
Comment
Question by:Eddie Shipman
1 Comment
 
LVL 26

Accepted Solution

by:
Russell Libby earned 1600 total points
ID: 13870975
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question