Discussion:
How to get browser object from SetSite method of my ActiveX C#?
(too old to reply)
buda
2009-04-14 20:03:42 UTC
Permalink
I have an ActiveX written in C#
I have added to the project SHDocVw and mshtml
I need in my ActiveX to have access to the browser object

I try it to do


private static SHDocVw.IWebBrowser2 m_pIWebBrowser2; // the browser
class object
public void SetSite(object pUnkSite)
{
// get IWebBrowser2 reference
if (m_pIWebBrowser2 != null) Release();
if (pUnkSite == null) return;
m_pIWebBrowser2 = pUnkSite as SHDocVw.IWebBrowser2;
}


but if later I try to call any method on m_pIWebBrowser2 - there is
an
exception appears "Object reference not set ..."


I realize that I havent a real browser object in m_pIWebBrowser2


have this function correctly working in Delphi

var FOleClientSite : IOleClientSite;


function TMyActiveX.SetSite(const pUnkSite: IInterface): HResult;
var
ClientSite: IOleClientSite;
begin
ClientSite := pUnkSite as IOleClientSite;
if ClientSite <> nil then
begin
if FOleClientSite <> nil then
begin
Result := S_FALSE;
Exit;
end;
FOleClientSite := ClientSite;
ClientSite.QueryInterface(IOleControlSite, FOleClientSite);
end else
begin
FOleClientSite := nil;
end;


Result := S_OK;
end;


function TMyActiveX.CheckLocation: string;
var
spSP : IServiceProvider;
spSPTop : IServiceProvider;
spBrowser : IWebBrowser2;
url : WideString;
begin
spSP := FOleClientSite as IServiceProvider;
OleCheck (spSP.QueryService(SID_STopLevelBrowser, IServiceProvider,
spSPTop));
OleCheck (spSPTop.QueryService(IID_IWebBrowserApp, IWebBrowser2,
spBrowser));


url := spBrowser.LocationURL;
Result := url;
end;


Help me please to port it in C#
buda
2009-04-14 21:55:41 UTC
Permalink
I have found 2 useful links
1 - PRB: Cannot Retrieve Top-Level IWebBrowser2 Interface from a .NET
UserControl (http://support.microsoft.com/kb/311299)
2 - Medr's manage wrapper for IWebBrowser2(http://social.microsoft.com/
Forums/en-US/vcgeneral/thread/97244e4e-25a1-4572-9b76-e73ac2be4e9d)

but i'm still havn't manage to assemble all it together because I dont
know C++ so I havnt wrapper dll and cannot translate it into C# to
call :(

help me please
Post by buda
I have an ActiveX written in C#
I have added to the project SHDocVw and mshtml
I need in my ActiveX to have access to the browser object
I try it to do
private static SHDocVw.IWebBrowser2 m_pIWebBrowser2; // the browser
class object
    public void SetSite(object pUnkSite)
    {
      // get IWebBrowser2 reference
      if (m_pIWebBrowser2 != null) Release();
      if (pUnkSite == null) return;
      m_pIWebBrowser2 = pUnkSite as SHDocVw.IWebBrowser2;
   }
but if later I try to call any method on m_pIWebBrowser2 - there is
an
exception appears "Object reference not set ..."
I realize that I havent a real browser object in m_pIWebBrowser2
 have this function correctly working in Delphi
var FOleClientSite : IOleClientSite;
function TMyActiveX.SetSite(const pUnkSite: IInterface): HResult;
var
   ClientSite: IOleClientSite;
begin
  ClientSite := pUnkSite as IOleClientSite;
  if ClientSite <> nil then
  begin
    if FOleClientSite <> nil then
    begin
      Result := S_FALSE;
      Exit;
    end;
    FOleClientSite := ClientSite;
    ClientSite.QueryInterface(IOleControlSite, FOleClientSite);
  end else
  begin
    FOleClientSite := nil;
  end;
  Result := S_OK;
end;
function TMyActiveX.CheckLocation: string;
var
  spSP      : IServiceProvider;
  spSPTop   : IServiceProvider;
  spBrowser : IWebBrowser2;
  url       : WideString;
begin
  spSP := FOleClientSite as IServiceProvider;
  OleCheck (spSP.QueryService(SID_STopLevelBrowser, IServiceProvider,
spSPTop));
  OleCheck (spSPTop.QueryService(IID_IWebBrowserApp, IWebBrowser2,
spBrowser));
  url := spBrowser.LocationURL;
  Result := url;
end;
Help me please to port it in C#
Loading...