Because the standard library data structures such as std::vector
are implemented in header files, the actual data representation can be different between versions of the compiler. If you have some code compiled with VS2005 and other code with VS2010, you cannot reliably return a std::vector
in this way.
Assuming foo
is the exact same data layout between components, you could return an array of raw foo*
pointers that has been allocated with HeapAlloc
. Do not use malloc()
because again, your components are using different runtime libraries and you can't free()
something that has been allocated with a different allocator. By using HeapAlloc
and HeapFree
, you are using the Win32 standard allocator which is shared across your whole process.
By way of example, you could do something like:
v->push_back(f);
HANDLE heap = GetProcessHeap();
foo **r = (foo **)HeapAlloc(heap, 0, (v->size() + 1) * sizeof(foo *));
std::copy(v.begin(), v.end(), r);
r[v->size()] = NULL; // add a sentinel so you know where the end of the array is
process->Result = r;
and then in your calling module:
foo **v = static_cast<foo**>(process->Result);
for (int i = 0; v[i] != NULL; i++) {
printf("index %d value %p\n", i, v[i]);
}
HANDLE heap = GetProcessHeap();
HeapFree(heap, 0, v);