I have a DLL which needs to access data stored in STL containers in the host application. Because C++ has no standard ABI, and I want to support different compilers, the interface between the application and DLL basically has to remain plain-old-data.
For vectors this is relatively straightforward. You can simply return the memory block of the vector, because it is guaranteed to be contigious:
// To return vector<int> data
virtual void GetVectorData(const int*& ptr, size_t& count) const
{
if (!vec.empty())
ptr = &(vec.front());
count = vec.size();
}
Now the DLL can have safe read-only access to the vector's data via that interface. The DLL can also wrap this to copy the contents in to a vector for itself as well.
What about STL lists (and deques) though? Is there another straightforward way to allow access via a DLL boundary? Or will I have to resort to some kind of GetFirst()/GetNext() interface? I might need to do this for a lot of lists, so it'd be nice to have a solution as simple as vector's.