buda
2009-04-14 20:03:42 UTC
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#
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#