In C#, I defined a struct:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MyObject
{
[MarshalAs(UnmanagedType.LPWStr)]
public string var1;
[MarshalAs(UnmanagedType.LPWStr)]
public string var2;
};
I have this struct in C++:
public value struct MyObject
{
LPWSTR var1;
LPWSTR var2;
};
And in the method of C++ which is a public class to be called from C#:
TestingObject(MyObject^ configObject)
{
// convert configObject from managed to unmanaged.
}
The object is debugged correctly that I can see two strings var1 and var2. However, the problem now is that I need to marshal the object: configObject
into an unmanaged object.
What I think of is to do something like this:
TestingObject(MyObject^ configObject)
{
// convert configObject from managed to unmanaged.
MyObject unmanagedObj = (MyObject)Marshal::PtrToStructure(configObject, MyObject);
}
That is what I can think of but off course, I got this error:
Error 2 error C2275: 'MyObject' : illegal use of this type as an expression
Is that right to convert the managed object into unmanaged object? If so, how can I can that Marshal::PtrToStructure
correctly? If no, how can I do it?