After posting a question yesterday I thought I had this cleared up but I’m still having problems, I have a C++/CLI wrapper for a C++ class, some functions of the C++ class take buffers for recv as parameters, the packet structures are defined as C++ structs and that is what is taken as a parameter.
In C# I have replicated these C++ structs using structlayout so that I have equivalent structs in C# which are laid out the same in memory as my C++ structs. In my C++/CLI code I attempted the following
UINT GetValues(value class^ JPVals) // value class, as C# structs are value types
{
IntPtr ptr;
Marshal::StructureToPtr(JPVals,ptr,false);
return m_pComms->GetValues(ptr,0); // m_pComms is a wrapped unmanaged class
//GetValues takes a pointer to a C++ struct
}
The error I get is cannot convert parameter 1 from ‘System::IntPtr’ to ‘SJPVal *’, why is it not possible to Marshall from value class to C++ struct pointer? And in this case what should I be passing in and how should I be marshalling it?
You didn’t get the serialization process:
EDIT: shown how to copy back the value.
As you are using a C#
structyou need to pass it by reference to make sure the changes are copied back. Alternatively, the code will work the same with a C#class. The first step (StructureToPtr) is probably useless, now, since you probably don’t care about what was in there before your call toGetValues.By the way your naming convention is a bit bad. You should NOT start variable names by a capital letter in C++.