The Windows Runtime supports C-style arrays. These are contiguous blocks of memory that consist of multiple consecutive instances of the same type. (This is not to be confused with a Windows Runtime vector, which is an interface that resembles an array but which does not require any particular storage format.)
Arrays are kind of weird, because they aren’t “objects” (there is no identity), but they aren’t scalars either (they are variable-sized). And there are multiple patterns for passing these arrays across the ABI boundary, depending on who is allocating the memory, whether the size is known to the caller, and whether the memory is being passed into or out of the function.
- PassArray: The caller passes a read-only array, and the implementation reads from it.
- FillArray: The caller passes a write-only array, and the implementation fills it with data.
- ReceiveArray: The implementation allocates a block of memory for the array and the caller receives a pointer to that block of memory, as well as the number of elements in the array.
Here’s a table, since people tend to like tables.
 | PassArray | FillArray | ReceiveArray | |
---|---|---|---|---|
Parameter | Return value | |||
Allocated by | Caller | Caller | Callee | Callee |
Size | Caller decides | Caller decides | Callee decides | Callee decides |
Freed by | Caller | Caller | Caller | Caller |
Allocator | Caller decides | Caller decides | COM allocator | COM allocator |
Policy | Read-only | Write-only | Write-only | Write-only |
IDL | void M(T[] value); |
void M( |
void M( |
T[] M(); |
ABI | HRESULT M( |
HRESULT M( |
HRESULT M( |
|
C++/WinRT | void M( |
void M( |
void M( |
com_array<T> M(); |
C++/CX | void M( |
void M( |
void M( |
Array<T>^ M(); |
C# | void M(T[] value); |
void M(T[] value); |
void M( |
T[] M(); |
VB | Sub M(value As T[]) |
Sub M(value As T[]) |
Sub M(ByRef value |
Function M() |
JS | function M(value |
function M(value |
function M() |
Function M() |
I gave the JavaScript prototypes in TypeScript notation so I could annotate the data types. The case of an out
parameter in JavaScript is a bit more complicated than it looks. I’ll save that topic for another day.
Note that in the case of PassArray, the formal parameter at the ABI level is not declared const T*
, even though the buffer is read-only.
Update: “Freed by” and “Allocator” rows added later.
The VB in this chart is slightly incorrect – it will work only with parentheses, not with square brackets, to denote the array elements.
So,
Sub M(value As T[])
should beSub M(value As T())
. OrSub M(value() As T)
– the () can go either after the variable name or after the type, but not both.Wait, no option for a read and write array? That’s going to be awkward, especially for people who want to limit the amount of copying being done.
You can pass a PassArray and a FillArray as separate parameters. No copying.
Is “Function” at the end of the Javascript row supposed to have a capital F?
You got the names “PassArray”, “FillArray”, and “ReceiveArray” from C++/CX, right? (e.g. they are used here: https://docs.microsoft.com/en-us/cpp/cppcx/array-and-writeonlyarray-c-cx?view=vs-2019 )
Does the API promise only to write to the write-only arrays, and not even read what it has written?
“I gave the JavaScript prototypes in TypeScript notation so I could annotate the data types.” And it’s the other way around: C++/CX got the names from the Windows Runtime. FillArrays are write-only. Not sure if they can be read-back before returning. (It shouldn’t matter, should it? Are you planning to pass device memory as a FillArray target?)
I could imagine a cross-process marshaler that temporarily maps most of the caller’s buffer to the callee process with write-only page permissions, and copies only any partial pages at the beginning and the end. Win32 does not have PAGE_WRITEONLY in Memory Protection Constants, though.
For the C++/CX parameter case:
void M(Array<T>& value);
error C3699: ‘&’: cannot use this indirection on type ‘Platform::Array’
It needs a hat too:
void M(Array<T>^& value);