What I decided to do is to simply make the plugins accept a simple interface of:
DWORD func ( LPARAM pBuf, DWORD size );
This would allow the user to specify their parameters in the XML and then define a structure which they expect me to pass them, e.g.
typedef struct
{
int a;
float b;
double c;
wchar_t * d;
} test1;
When they get this message from me, they can check the size before using the buffer to ensure that the XML and the structure match.
As I parse the XML, I use this class with template methods to dynamically construct the object:
class DynamicStructure
{
public:
template <typename T>
void addField(const T & field)
{
m_mapPtrSize.push_back(std::make_pair(reinterpret_cast<const LPARAM>(&field), sizeof(T)));
}
DWORD getSize()
{
//
// Work out the combined size of all the fields
DWORD sSize = 0;
for ( auto it = m_mapPtrSize.cbegin(); it != m_mapPtrSize.cend(); it++ )
{
sSize += it->second;
}
return sSize;
}
LPARAM getBuffer()
{
// Create a buffer big enough for all the fields
//
LPARAM pBuf = reinterpret_cast<LPARAM> (new (std::nothrow) BYTE[getSize()]);
if (pBuf == NULL)
return NULL;
DWORD offset = 0;
for ( auto it = m_mapPtrSize.cbegin(); it != m_mapPtrSize.cend(); it++ )
{
// Copy the fields one at a time, offsetting into the buffer
//
memcpy( (void*) (pBuf + offset), (const void*) it->first, it->second);
offset += it->second;
}
return pBuf;
}
protected:
private:
std::vector<std::pair<const LPARAM, DWORD>> m_mapPtrSize;
};
This allows me to perform the following kind of operation as I parse the XML:
DynamicStructure dynStruct;
int a = 1;
float b = 2.3f;
double c = 3.5;
wchar_t * d = L"bob";
dynStruct.addField(a);
dynStruct.addField(b);
dynStruct.addField(c);
dynStruct.addField(d);
// Test - does the dymanic structure match the user's structure?
LPARAM pBuf = dynStruct.getBuffer();
test1 * pTest1 = (test1 *) pBuf;
std::wcout << pTest1->a << " " << pTest1->b << " " << pTest1->c << " " << pTest1->d << std::endl;
It's not perfect, and it's a bit old school, but at least it's simple and provides a reasonable level of safety.