In short, in the following definition:
HRESULT GetStuff([in] long count,
[out, size_is(count)] long * a,
[out, size_is(count)] long * b);
which fills a and b with count elements, is it legal for the caller to set a and / or b to null?
I want to allow the caller to query only one set of results, so the method may be called with
long only_a_s[10];
itf->GetStuff(10, a, 0);
Do I need to modify the MIDL declaration? I'm unsertain how the pointer/pointer_default attributes play into this.
Notes: There's overhead in acquiring them separately, but so is acquiring values the caller doesn't need, so separate getters or always having to get both is sub-par. I know it does work for inproc / in-apartment calls, but would the MIDL-generated proxy/stub deal with that correctly?
You cannot pass a null pointer as an argument (a.k.a. top-level pointer), as they're
[ref]by default.But you may pass a null pointer, in or out, for non-top-level pointers.
The IDL method definition goes like this:
The C++ implementation:
Not shown is whatever other code you must implement yourself to get the amount of
As and the amount ofBs, and theAs andBs themselves.If you must get the items to know the amounts, and you're not using RAII, you should free those resources manually before returning.
As an alternative to using
CoTaskMemAllocandCoTaskMemFree, you may want to use ATL'sCComHeapPtr, which will automatically free memory RAII-style, thus simplifying your code. Just make sure you callDetachinto*pAand*pBbefore returning successfully.