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 structure in C ++:
public value struct MyObject { LPWSTR var1; LPWSTR var2; };
And in the C ++ method, which is a public class that is called from C #:
TestingObject(MyObject^ configObject) { // convert configObject from managed to unmanaged. }
The object is being debugged correctly, that I see two lines var1 and var2. However, now the problem is that I need to marshal the object: configObject into an unmanaged object.
What I think of doing something like this:
TestingObject(MyObject^ configObject) { // convert configObject from managed to unmanaged. MyObject unmanagedObj = (MyObject)Marshal::PtrToStructure(configObject, MyObject); }
This is something I can think of, but not in the know, I got this error:
Error 2 error C2275: "MyObject": illegal use of this type as an expression
Is it right to convert a managed object to an unmanaged object? If so, how can I do this Marshal::PtrToStructure correctly? If not, how can I do this?
source share