First of all Thank you Michael
Yes, it works but still there is a little problem now i am getting
following error
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
on this line
iobjectWithSite.SetSite(browserControl);
I have logged in with administrator account and I know that this
account has full access right.
my code
Class file name: GearsBHO.cs
----------------------------
using System.Runtime.InteropServices;
// Declare GearsBHO as a COM coclass
[ComImport, Guid("C93A7319-17B3-4504-87CD-03EFC6103E6E")]
class GearsBHO
{
}
// Declare IObjectWithSite as a COM interface which
// derives from the IDispatch interface.
[Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352"), InterfaceType
(ComInterfaceType.InterfaceIsDual)]
interface IObjectWithSite // cannot list any base interfaces here
{
// Note that the members of IUnknown and Interface are NOT
// listed here
void SetSite([In, MarshalAs(UnmanagedType.Interface)] object
unk );
}
public class Gears
{
public void initGears(object browserControl)
{
// Create an instance of a COM coclass:
object gearsBHO = new GearsBHO();
// See if it supports the IObjectWithSite COM interface.
// Note that this will throw a System.InvalidCastException if
// the cast fails. This is equivalent to QueryInterface for
// COM objects:
IObjectWithSite iobjectWithSite = (IObjectWithSite)gearsBHO;
// Call it
iobjectWithSite.SetSite(browserControl);
}
}
-----------------------------------------
form1.cs contains pageload method
private void Form1_Load(object sender, EventArgs e)
{
Gears objGears = new Gears();
objGears.initGears(webBrowser1);
webBrowser1.Url = new Uri("http://localhost/GearSite");
}
On Dec 9, 12:53 am, Michael Nordman <[EMAIL PROTECTED]> wrote:
> The InterfaceID (IID) of IObjectWithSite is
> "FC4801A3-2BA9-11CF-A229-00AA003D7352".
>
> http://www.pinvoke.net/default.aspx/Interfaces/IObjectWithSite.html
>
>
>
> On Mon, Dec 8, 2008 at 5:31 AM, TatvaSoft <[EMAIL PROTECTED]> wrote:
>
> > //I tried with following code but got error
> > //Unable to cast COM object of type 'GearsBHO' to interface type
> > 'IObjectWithSite'. This operation failed because the QueryInterface
> > call on the COM component for the interface with IID
> > '{C93A7319-17B3-4504-87CD-03EFC6103E6E}' failed due to the following
> > error: No such interface supported (Exception from HRESULT: 0x80004002
> > (E_NOINTERFACE)).
>
> > using System.Runtime.InteropServices;
>
> > // Declare GearsBHO as a COM coclass
> > [ComImport, Guid("C93A7319-17B3-4504-87CD-03EFC6103E6E")]
> > class GearsBHO
> > {
> > }
>
> > // Declare IObjectWithSite as a COM interface which
> > // derives from the IDispatch interface.
> > [Guid("C93A7319-17B3-4504-87CD-03EFC6103E6E"), InterfaceType
> > (ComInterfaceType.InterfaceIsDual)]
> > interface IObjectWithSite // cannot list any base interfaces here
> > {
> > // Note that the members of IUnknown and Interface are NOT
> > // listed here
>
> > void SetSite([In, MarshalAs(UnmanagedType.Interface)] object
> > unk );
>
> > }
>
> > public class Gears
> > {
> > public void initGears(object browserControl)
> > {
> > // Create an instance of a COM coclass:
> > object gearsBHO = new GearsBHO();
>
> > // See if it supports the IObjectWithSite COM interface.
> > // Note that this will throw a System.InvalidCastException if
> > // the cast fails. This is equivalent to QueryInterface for
> > // COM objects:
> > IObjectWithSite iobjectWithSite = (IObjectWithSite)gearsBHO;
>
> > // Call it
> > iobjectWithSite.SetSite(browserControl);
> > }
> > }
>
> > On Nov 21, 5:04 am, Michael Nordman <[EMAIL PROTECTED]> wrote:
> >> This page looks like it discusses the relevant .NET / COM Interop
> >> stuff.http://msdn.microsoft.com/en-us/library/aa645736(VS.71).aspx
>
> >> Looks like something like the following is needed...
>
> >> using System.Runtime.InteropServices;
>
> >> // Declare GearsBHO as a COM coclass
> >> [ComImport, Guid("...")]
> >> class GearsBHO
> >> {
>
> >> }
>
> >> // Declare IObjectWithSite as a COM interface which
> >> // derives from the IDispatch interface.
> >> [Guid("..."), InterfaceType(ComInterfaceType.InterfaceIsDual)]
> >> interface IObjectWithSite // cannot list any base interfaces here
> >> {
> >> // Note that the members of IUnknown and Interface are NOT
> >> // listed here
>
> >> void SetSite([In, MarshalAs(UnmanagedType.Interface)] object unk );
>
> >> }
>
> >> void initGears(object browserControl) {
> >> // Create an instance of a COM coclass:
> >> GearsBHO gearsBHO= new GearsBHO ();
>
> >> // See if it supports the IObjectWithSite COM interface.
> >> // Note that this will throw a System.InvalidCastException if
> >> // the cast fails. This is equivalent to QueryInterface for
> >> // COM objects:
> >> IObjectWithSite iobjectWithSite = (IObjectWithSite ) gearsBHO;
>
> >> // Call it
> >> objectWithSite.SetSite(browserControl);
>
> >> }- Hide quoted text -
>
> >> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -