Hello,
In a C# project, I need to pass object parameters by putting references in a structure. i.e. I have a structure passed to a dispatcher
struct SOMESTRUCT
{
public int lpObject;
}
Where lpObject holds a pointer to a custom object like
class SomeClass
{
private string foo;
}
And the SOMESTRUCT structure is passed from method to method to finally reach my code. I cannot modify the execution flow nor the strange SOMSTRUCT system, so I guessed the only solution was to cast my object to a pointer like this :
var myObject = new SomeClass();
GCHandle GC = GCHandle.Alloc(myObject, GCHandleType.Pinned);
int myRef = GC.AddrOfPinnedObject().ToInt32();
GC.Free();
SOMESTRUCT struct;
struct.lpObject = myRef;
someMethod(struct);
However, I can't figure out how to retrieve the myObject members from the lpObject fields. Something like this:
SomeClass myObject = CastPointerToObject(struct.myRef) as SomeClass;
Is there a way to do it, or is it impossible ? How can I tell the garbage collector to handle the object ? Should I create a new Garbage-collected object and copy the data field by field ?
TYIA,