By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Invoke and InvokeMember methods work differently!?

P: n/a
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
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.