Search code examples
delphidelphi-xe7

how to properly grab a url from ie with activex?


i am trying to grab url of a page that running my activex , I used DDEMAN unit but its seems doesn't work in activex here what I have done

function Get_URL(Servicio: string): String;
var
  Cliente_DDE: TDDEClientConv;
  S: String;
begin
  Result := '';
  Cliente_DDE := TDDEClientConv.Create(nil);
  with Cliente_DDE do
  begin
    SetLink(Servicio, 'WWW_GetWindowInfo');
    S := RequestData('0xFFFFFFFF');
    Result := S;
    form1.memo1.Lines.Add(Result);
    CloseLink;
  end;
  Cliente_DDE.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
URI: TIdURI;
LinK : String;
Temp: string;
Host,sExt: String;
Path : string;
begin
Link := Get_URL('Iexplore');
Temp := Copy(Link, 1, Pos(',', Link) - 1);
Temp := StringReplace(Temp, '"', '', [rfReplaceAll]);

URI := TIdURI.Create(Temp);
try
Host := URI.Host;
Path := URI.Path;
Host := StringReplace(Host ,'www.', '',[rfReplaceAll,rfIgnoreCase]);
sExt  := ExtractFileExt(Host);
Host := Copy(Host, 1, Length(Host) - Length(sExt));
finally
  URI.Free;
end;

end;

after running the code its seems that GET_URL function return empty result in ActiveX . I tested this code on exe and its working normal . what could be the problem ? how to properly get a URL that running in IE with ActiveX ?

as remy suggested I tried

procedure Tactiveform.Button1Click(Sender: TObject);
var
 punk:       IUnknown;
 poo:        IOleObject;
 pcs:        IOleClientSite;
 psp:        IServiceProvider;
 pwb2:       IWebBrowser2;
 pvDoc:      Variant;
 pvElems:    Variant;
 pvElem:     Variant;
 i:          Integer;
 Getsite : string;
begin
punk:=Self.ComObject as IUnKnown;
  if punk.QueryInterface(IOleObject, poo) = S_OK then
  begin
     // Get the client site
     if poo.GetClientSite(pcs) = S_OK then
     begin
        // Have the site, now try to get the Service provider
        if pcs.QueryInterface(IServiceProvider, psp) = S_OK then
        begin
           // Query for WebBrowser2
           if psp.QueryService(IWebBrowserApp, IWebBrowser2, pwb2) = S_OK then
           begin
              memo1.Lines.Add(pwb2.LocationURL);
           end
           else
              memo1.Lines.Add('Didnt get the web browser');
        end
        else
           memo1.Lines.Add('No service provider');
     end
     else
        memo1.Lines.Add('No ole client site');
  end
  else
     memo1.Lines.Add('No ole object');
end;

but couldn't compile the code at this line memo1.Lines.Add(getsite as IWebBrowser2.LocationURL);

Compiler error fixed

is this good way or maybe I could do it better ?


Solution

  • Your ActiveX control's IOleObject.SetClientSite() method will receive the browser's IClientSite interface, which you can use to retrieve the URL of the HTML page that has loaded your ActiveX control. There are two different ways you can do that, depending on your version of Internet Explorer.

    1. Query the IClientSite for IServiceProvider, then call IServiceProvider.QueryService() to get an IWebBrowser2, then you can read the IWebBrowser2.LocationURL property.

    2. Query the IClientSite for IHTMLDocument2, then read the IHTMLDocument2.URL property.

    See the following MSN article for more details:

    Accessing the Object Model from Within an ActiveX Control