473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Invoke and InvokeMember methods work differently!?

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
0 3055

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: PJ | last post by:
Hello, I am trying to use late binding to call a COM object. I am trying to call a 'GetTables' method on the object. It's essentially a 'MetadataService' which is used to return the names of...
0
by: Dominic | last post by:
Hi, I'm tring to call the DisableAutoMacros method (well I think it's a method) of the Word.WordBasic object. This is returned as a _ComObject so I can not access this method directly. I...
1
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
1
by: Rein Petersen | last post by:
Hi All, I'm invoking Type.InvokeMember() on a COM class (via COMInterop) through a generalized interface. However, each specialized instance (of the COMInterop generic interface) requires...
12
by: Gus | last post by:
Hi, I´m triying to invoke a member using reflection, but this member requiere two parameters, the first patrameter is a string and the second is a reference to a dataset (output parameter). The...
1
by: Jenny Fletcher | last post by:
I am trying to dynamically invoke a property of enumeration type but I can't find a way to get it to work. This is my code which works fine until I call InvokeMember and I get an ArgumentException....
3
by: =?Utf-8?B?Sm9l?= | last post by:
I know that I have posted this question before, but it is still unresolved and I don't know where to turn to next. I have code that is creating a user (works fine), then sets the account flags...
4
by: James | last post by:
Hello everyone, While loading a page (http://www.edmonton.ca/portal/server.pt?space=CommunityPage&control=SetCommunity&CommunityID=239) into a webbrowser control I use invokemember on the...
4
by: puzzlecracker | last post by:
Say, I have classname string, static method string, parameters list (which is char). Can I use reflection to invoke the method?
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.