In a dll, I’m doing
std::vector<foo*>* v = new std::vector<foo*>();
foo* f = new foo();
v->push_back(f);
// size of v is 1
process->Result = static_cast<void*>(v); // Result is of type void*
and in a exe that uses the dll, I do
if (process->Result != NULL)
{
std::vector<foo*>* v = static_cast<std::vector<foo*>*>(process->Result);
int size = v->size(); // <-- size is garbage (221232 or something like that)
}
Result has to be a void*. Does anyone know why I’m not able to pass back the vector properly ? Am I doing something wrong with the casting?
Thanks
Because the standard library data structures such as
std::vectorare 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 astd::vectorin this way.Assuming
foois the exact same data layout between components, you could return an array of rawfoo*pointers that has been allocated withHeapAlloc. Do not usemalloc()because again, your components are using different runtime libraries and you can’tfree()something that has been allocated with a different allocator. By usingHeapAllocandHeapFree, you are using the Win32 standard allocator which is shared across your whole process.By way of example, you could do something like:
and then in your calling module: