코딩하는 나귀

uses
    ActiveX, Mshtml, SHDocVw;

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

function TActiveFormX.GetIWebPointer: IWebBrowser2;
var
  pIOleObject : IOleObject;
  pIOleClient : IOleClientSite;
  pIOleContainer : IOleContainer;
  pUnk : IUnknown;
  pIWeb: IWebBrowser2;
  pISP: IServiceProvider;

begin
  Result := nil;
  pUnk := ComObject;
  if Assigned( pUnk ) then
  begin
    if SUCCEEDED(pUnk.QueryInterface(IOleObject, pIOleObject)) and Assigned( pIOleObject ) then
    begin
      if SUCCEEDED(pIOleObject.GetClientSite(pIOleClient)) and Assigned( pIOleClient ) then
      begin
        if SUCCEEDED(pIOleClient.GetContainer(pIOleContainer)) and Assigned( pIOleContainer ) then
        begin
          if SUCCEEDED(pIOleContainer.QueryInterface(IID_IServiceProvider, pISP)) and Assigned( pISP ) then
          begin
            if SUCCEEDED(pISP.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, pIWeb)) and Assigned( pIWeb ) then
            begin
              Result := pIWeb;
            end;
          end;
        end;
      end;
    end;
  end;
end;