ITypeInfo and fast searching for function signature

143 views Asked by At

Maybe I'm blind, but how did Microsoft think of finding a function signature quickly using the ITypeInfo interface? How do others do it better than me?

In order to find a function, you need a member ID, which you have specified in the interface, and the InvokeKind, i.e. whether it is a func, a property get, let or set. In order to carry out an invocation, whether with DispCallFunc or Invoke from IDispatch, you need the function signature in order to set the parameters correctly.

This is where ITypeInfo comes into play. The way seems to me to first get the MemberID if the function name is known. However, there can be up to three different signatures in COM for a function, which must be determined after the InvokeKind.

My way to find the function signature:

First you have to fetch a pointer to GetTypeAttr. There in the TYPEATTR struct you have to find out, how many function member entries are existing. And because FUNCDESC entries are index based, you have to loop over all FUNCDESC entries to find a match for the combination MemberID, InvokeKind. And finally you have to release all stuff to free the memory.

This job has to be done very often. Am I stupid or is it Microsoft? Why isn't there a quick way to do this? Or is there a quicker way?

1

There are 1 answers

8
SoronelHaetir On

The usual answer is that when using IDispatch it is up to the programmer to get the number of arguments right. Once the number of arguments is right the signature is not generally checked on the call side, Instead the callee attempts to coerce arguments to the needed types (and there are entire libraries dedicated to easing this process). Note that for this part getting the signature is fast because it is known what function is being invoked.

For example, the usual IDispatch-to-dual interface call (DispCallFunc) will check that the argument count is at least as many as are needed, as well as perform argument coercion for you (if possible). Even if you are implementing a pure dispinterface you can use the VariantChangeType API to perform coercion to the correct type. In neither case does the caller check beforehand that the arguments are what the function expects.