InvokeMember Woes

G

Guest

All,
I am trying to access a complex COM library with nested COM classes. In
order to get at a certain class you must call a method on class A and this
method will return a instance of COM class B. FOr instance the VB code looks
like this:

Public Function GetFee(sFeeCode As String) As Test.FeeCls
....
End Function

I've written code to access the method GetFee in Class A, but the object
returned from the InvokeMember is undefined. No exceptions are generated but
the COM class B is not instantiated. Here is the "late Bound" code:

object[] args = new object[1];
args[0] = "";

tyClsB = Type.GetTypeFromProgID("Test.FeeCls",true);
oClsB = tyClsA.InvokeMember("GetFee", BindingFlags.InvokeMethod, null,
oClsA, args, null, null, null);

The variable oClsB is not valid.

if I use the interop lib generated and write this code it works:

string strFeeTy = "a03";
Test.FeeCls clsB;

clsB = clsA.GetFee(ref strFeeTy);

Any help or tips would be appreciated...

Stan
 
N

Nicholas Paldino [.NET/C# MVP]

Stan,

I am curious, why not just use the early bound call? It will be quicker
than the late bound call. After all, VB is really just COM, which is
interface based, so you can access the interface (it will be prefixed by an
underscore) and cast your dynamically loaded type to that.

As for doing it dynamically, is an exception thrown, or it just returns
null upon return?
 
W

Wessel Troost

oClsB = tyClsA.InvokeMember("GetFee", BindingFlags.InvokeMethod, null,
oClsA, args, null, null, null);
How did you declare oClsB ?

Greetings,
Wessel
 
G

Guest

Thanks for the quick reply. This is 3rd party software and we get new
versions quite often. I do not want to have to recompile the code every
single time it is deployed. I believe when I get a new version I will need a
new interop. Thus late binding. The just returns null. I wrote a sample
app just like this and it worked. I'm starting to think it is the 3rd party
objects.

Stan

Nicholas Paldino said:
Stan,

I am curious, why not just use the early bound call? It will be quicker
than the late bound call. After all, VB is really just COM, which is
interface based, so you can access the interface (it will be prefixed by an
underscore) and cast your dynamically loaded type to that.

As for doing it dynamically, is an exception thrown, or it just returns
null upon return?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Stan said:
All,
I am trying to access a complex COM library with nested COM classes. In
order to get at a certain class you must call a method on class A and this
method will return a instance of COM class B. FOr instance the VB code
looks
like this:

Public Function GetFee(sFeeCode As String) As Test.FeeCls
...
End Function

I've written code to access the method GetFee in Class A, but the object
returned from the InvokeMember is undefined. No exceptions are generated
but
the COM class B is not instantiated. Here is the "late Bound" code:

object[] args = new object[1];
args[0] = "";

tyClsB = Type.GetTypeFromProgID("Test.FeeCls",true);
oClsB = tyClsA.InvokeMember("GetFee", BindingFlags.InvokeMethod, null,
oClsA, args, null, null, null);

The variable oClsB is not valid.

if I use the interop lib generated and write this code it works:

string strFeeTy = "a03";
Test.FeeCls clsB;

clsB = clsA.GetFee(ref strFeeTy);

Any help or tips would be appreciated...

Stan
 
W

Wessel Troost

Here is the "late Bound" code:
object[] args = new object[1];
args[0] = "";

tyClsB = Type.GetTypeFromProgID("Test.FeeCls",true);
oClsB = tyClsA.InvokeMember("GetFee", BindingFlags.InvokeMethod, null,
oClsA, args, null, null, null);

The variable oClsB is not valid.

If I use the interop lib generated and write this code it works:

string strFeeTy = "a03";
Test.FeeCls clsB;

clsB = clsA.GetFee(ref strFeeTy);
For one thing, the argument to GetFee() seems to differ. In the late
bound example you pass "", and in the interop example you pass "a03".
Does it make a difference if you pass "a03" in the late bound code?

A second difference is the "ref" you use in the interop example. You
don't pass that in the late bound example. You could by doing something
like this:

Args = new Object[1];
Args[0] = "a03";
// ParameterModifier, construct with #parameters
ParameterModifier ParMod = new ParameterModifier(1);
// Set VT_BYREF
ParMod[0] = true;
// Put in array
ParameterModifier[] ArrParMod = { ParMod };
object oClsB = tyClsA.InvokeMember("GetFee", BindingFlags.InvokeMethod,
null, oChild, Args, ArrParMod, null, null);

Does this produce any other results?

Greetings,
Wessel
 

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