I am doing some manual marshaling for interop from C#/.NET to unmanaged DLLs.
Consider the following struct:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct LockInfo
{
ushort lockVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lockName;
}
I marshal this to unmanaged memory:
var lockInfo = new LockInfo();
var lockInfoPtr = Marshal.AllocHGlobal(Marshal.SizeOf(lockInfo));
Marshal.StructureToPtr(lockInfo, lockInfoPtr, false);
Once I'm done with it, do I need to call Marshal.DestroyStructure on lockInfoPtr?
I am aware of the need to call Marshal.FreeHGlobal, but prior to that, is Marshal.DestroyStructure actually required in this case?
I have found it very difficult to understand Microsoft's documentation around this. Google searching hasn't helped, possibly because I just don't quite understand marshalling properly yet (but I am learning).
Similar questions...
I have reviewed the similar question "Marshal.DestroyStructure vs Marshal.FreeHGlobal in .Net" but this question does not address the issue of the content a struct should contain that would require the use of DestroyStructure. My limited understanding is that DestroyStructure does not always need to be called, only when the structure contains certain kinds of fields. In my case I am unsure if a string, being marshalled as ByValTStr, requires the use of DestroyStructure.