convert struct handle from managed into unmanaged

2019-03-16 20:00发布

问题:

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?

回答1:

Marshal::PtrToStructure does the opposite of what you want, it converts an unmanaged pointer to a managed object. You want Marshal::StructureToPtr.

Also, you would need to define an unmanaged class, because MyObject is a Managed Type. Assuming you have done that, you could do it like this (just converted this from the C# sample):

IntPtr pnt = Marshal::AllocHGlobal(Marshal::SizeOf(configObject)); 
Marshal.StructureToPtr(configObject, pnt, false);

You then have a pointer to the data, which you can memcpy or whatever into your native struct.

But MyObject is and will stay a managed type. If you want a truly unmanaged type, you have to define one that matches the managed struct.

Just wondering, why are you using unmanaged LPWSTR in a managed struct?



回答2:

You probably mean:

struct MyUnmanagedStruct {
    LPWSTR var1, var2;
};

Then you can use Marshal.StructureToPtras suggested by Botz3000. Otherwise C#'s

public struct MyObject {
   public String var1;
   public String var2;
}

and C++/CLI's

public struct value MyObject {
   public String^ var1;
   public String^ var2;
}

are completely equivalent, assuming your're you're using the same System.String on both sides.