I'm experience a bit of a small problem. I have implemented a render system with a dll in C++, where the user injects data (model/view/projection matricse, colors, meshes and so on) through void pointers. It is quite handy, as the graphic API's usually don't care about the data type, just the structure and size of the data.
I have been using it as a part of a game that we are developing, and it works out quite good. But now I am in the mood of developing a editor to the render system in C#, and am quite stumped.
When i link an XNA matrix as a pointer to the render dll, the pointer stays intact in teh transfer to the dll, where it is kept for future references when updated in the editor. But, the data that the pointer points at, is not. Here is some code:
Editor (C#):
unsafe
{
[...]
Matrix unsafeWorldMatrix = worldMatrix;
void* worldMatrixPointer = (void*)(&unsafeWorldMatrix.M11);
mRenderInterface.AddData("world", worldMatrixPointer);
[...]
}
While the pointer is in the editor code, the world matrix pointer is able to convert back to the matrix in question, with the data intact.
// The data is intact
Matrix worldMatrix = *(Matrix*)worldMatrixPointer;
But as soon as I send it to the render system, using the dll, it looses the data. The pointer is the same, but teh first value just points to 0, and the rest is invalid.
// Sending the data to the render system
// The data is still intact here, and lets say that the pointer value is 0x0c52af32
[DllImport(DllName, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern unsafe void SetSharedData(System.String dataID, void* data);
public unsafe void AddData(String id, void* dataPtr)
{
SetSharedData(id, dataPtr);
}
In the dll (c++)
// Here the data is incorrect
// the pointer is the same as before, but with CAPS letter (should not matter though)(0x0C52AF32)
void hs::renderer::SetSharedData(const char* id, void* data)
{
AssertFunctionAndThrow(DirectXManager::GetInstance()->GetContent()->AddSharedData(id, data));
}
Both systems are compiled using x86 / Win32 settings, and both are in debug.
What is the deal here? I am aware that it is lack of knowledge, as I´m very unfamiliar with the correct procedures here, but as far as i know, the data should not change, as long as the pointer is intact...
BTW, when returning back from saving the pointer in the system, returning to the C# editor, the data in the pointer is intact.... WTF?
So, anyone who can point out my error? And as the rendersystem works as intended in the actual game, I know it is nothing wrong in the dll. And, teh only thing I do is storing the pointer in a map with a string identifier, so no interaction with hte actual pointer,
Any ideas are welcome.
// CAPE