//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 -