Monday, July 6, 2015

MIDL: [ptr] vs [ref] vs [unique] vs [optional]

If you author IDL files for RPC, COM, or WRL, you will probably need to pass arrays.  There are a few parameter attributes you can use for pointers, but it may be unclear which one to use.  You may have seen [ptr], [ref], [unique], and maybe [optional].  Straight off, even though optional seems like a logical choice for emulating SAL's _opt, it is actually a red hearing.  For some inexplicable reason, optional only applies to VARIANT structs according to MSDN.

That leaves us with: ptr, ref, and unique.


In general, I would recommend using ref for const type in parameters, and unique for opt type in parameters.  You should probably stay away from ptr pointers due to the RPC runtime overhead unless you have a good reason to use them.  Also you may experiment with the MIDL compiler and see what SAL annotations are generated with which IDL attributes.  There is a lot more nuance, so dive into the MSDN articles if you need more depth on the topic.

No comments:

Post a Comment