Invoke and InvokeMember methods work differently!?

A

Alex Zhitlenok

Hi,
On Google, one can find a lot of statements that Invoke and
InvokeMemeber work alike. However, my own experience contradicts this
statement.

1. Surprisingly, Invoke and InvokeMember methods work differently!
I'm working with some legacy COM. The COM object (CMyCOM) implements
an interface that is not dual. The interface has a method, let say,
HRESULT Foo();
The corresponded coclass defines an interface
coclass MyCOM
{
[default] interface ITheInterface;
};
My C# client successfully calls Foo() method:
directly
MyCOMClass mcc = new MyCOMClass();
mcc.Foo();
or indirectly using Invoke()
MyCOMClass mcc = new MyCOMClass();
Type mccType = mcc.GetType();
MethodInfo mi = mccType.GetMethod("Foo");
mi.Invoke(mcc, null);

However, the following code (using InvokeMember()) does not work
MyCOMClass mcc = new MyCOMClass();
Type mccType = mcc.GetType();
mccType.InvokeMember("Foo",
System.Reflection.BindingFlags.InvokeMethod, null, mcc, null);

It results in an exception "COM target does not implement IDispatch",
which is very reasonable. For me, it looks like the system could not
access IReflection interface. But it also means that Invoke and
InvokeMember use absolutely different mechanisms under the hood!
Can anybody explain me the difference and in which situation what
method must be used?

2. To resolve the situation I define dispinterface as a wrapper of a
legacy non-dual interface.
Dispinterface IDTheInterface
{
#interface ITheInterface;
}

And I make CMyCOM class to implement both ITheInterface and
IDTheInterface. That case, CMyCOM, of course, implements IDispatch.
CoClass now looks like
coclass MyCOM
{
[default] interface ITheInterface;
dispinterface IDTheInterface;
};
However, that case also, InvokeMemeber call returns "Invalid callee"
exception. If I send (just for testing purpose) wrong number of
parameters, exception is "Wrong number of parameters".
It looks like, the system knows the method signature however it could
not dispatch call to the method implementation.

Can anyone explain me:
1. What is the REAL difference between Invoke and InvokeMember?
2. How to make InvokeMember work without redefining legacy interface
as a dual one?
Environment: .NET, C#, C++.

Alex
 

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