views:

25

answers:

1

Hi there,

I would like to know how to use the Invoke method of the TRttiMethod class in C++Builder 2010.

This is my code

Tpp *instance=new Tpp(this);
TValue *args;

TRttiContext * ctx=new TRttiContext();
TRttiType * t = ctx->GetType(FindClass(instance->ClassName()));

TRttiMethod *m=t->GetMethod("Show");
m->Invoke(instance,args,0);

Show has no arguments and it is __published. When I execute I get a EInvocationError with message 'Parameter count mismatch'.

Can someone demonstrate the use of Invoke? Both no arguments and with arguments in the called method.

Thanks

Josep

A: 

You get the error because you are telling Invoke() that you are passing in 1 method parameter (even though you really are not, but that is a separate bug in your code). Invoke() takes an OPENARRAY of TValue values as input. Despite its name, the Args_Size parameter is not the NUMBER of parameters being passed in, but rather is the INDEX of the last parameter in the array. So, to pass 0 method parameters to Show() via Invoke(), set the Args parameter to NULL and the Args_Size parameter to -1 instead of 0, ie:

Tpp *instance = new Tpp(this);

TRttiContext *ctx = new TRttiContext;
TRttiType *t = ctx->GetType(instance->ClassType());

TRttiMethod *m = t->GetMethod("Show");
m->Invoke(instance, NULL, -1);

delete ctx;

Now, once you fix that, you will notice Invoke() start to raise an EInsufficientRtti exception instead. That happens when Runtime Packages are enabled. Unfortunately, disabling Runtime Packages will cause TRttiContext::GetType() to raise an EAccessViolation in TRttiPool::GetPackageFor() because of a known linker bug under C++:

QC #76875, RAID #272782: InitContext.PackageTypeInfo shouldn't be 0 in a C++ module:

Which causes these bugs:

QC #76672, RAID #272419: Rtti.pas is unusable in a C++ application

QC #76877, RAID #272767: AV in TRttiContext::GetType() when Runtime Packages are disabled

So you are in a catch-22 situation. The new RTTI system is not ready for production work in C++ yet. You will have to use Delphi instead for the time being.

Remy Lebeau - TeamB