COM Interop (calling late bound IDispatch based method within C#)

  • Thread starter Christopher Fairbairn
  • Start date
C

Christopher Fairbairn

Hi,

I am currently working on some COM interop code within a .NET CF 2.0
application to hook into a third part COM library. In part this is a port of
some desktop code that currently works. I am having difficulty calling a
method implemented via an IDispatch based interface.

My native COM object has a method that returns a reference to another
IDispatch based object. I want my C# code to call a method via this
IDispatch based reference, knowing only the method's name.

The method within the COM object is declared in the C# wrapper class as
follows:

[ComImport()]
[Guid(".....")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface ISomeInterface
{
....
void GetManager([Out,MarshalAs(UnmanagedType.IDispatch)]out object
dispatch);
....
}

The code I have tried (and that works correctly on the desktop) is as
follows:

public void CallMethod(string method)
{
// Call the native COM object to obtain a
// reference to another IDispatch based object
object dispatch = null;
myComObject.GetManager(out dispatch);

// Call the desired method on this object
// The method accepts no parameters
Type t = dispatch.GetType();
t.InvokeMember(method, BindingFlags.InvokeMethod, null, dispatch, null);
}

The problem I am getting on the .NET Compact Framework is the call to
Type.InvokeMember is throwing a NotSupportedException, while on the desktop
this code looks up the required method via the IDispatch interface and
correctly invokes it.

The API I am attempting to access dynamically creates the contents of this
IDispatch based interface, meaning I can't use the delcarative approach to
wrap up an IDispatch based interface (using C#'s [DispId(1)] attributes
etc), since at compile time I do not know the names or dispatch ids for the
methods the COM object contains.

Is this NotSupportedException showing something wrong with my code, or is
this type of COM Interop not support within the .NET CF?

Thanks,
Christopher Fairbairn
 
C

Christopher Fairbairn

Hi,

I seem to recall some issue with pure IDispatch-derived classes, though a
cursory search didn't find it. The answer may (or may not - it's just a
starting point) lie somewhere is this really long (but good) post by Katie:

http://blogs.msdn.com/netcfteam/archive/2005/07/24/442612.aspx

Thanks for that brilliant reference.

The managed NotSupportedException I am getting from Type.InvokeMember
appears to be intentional. According to the documentation on MSDN
(http://msdn2.microsoft.com/en-us/library/66btctbe.aspx) "this members
throws a NotSupportedException on the .NET Compact Framework". So I guess
this isn't a supported way to (very) late bind to IDispatch based interfaces
(where the details of the interface aren't known at compile time).

Usually I would use GetMethod et al and call Invoke on the MemberInfo
instance returned (which is why I probably hadn't seen that InvokeMember
isn't supported). However this technique doesn't work for COM objects
returned by the interop layer, leaving me a little stuck (other than calling
QueryInterface for IDispatch and implementing all the marshaling code
myself).

Thanks,
Christopher Fairbairn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top