Marshals data from a managed object to an unmanaged block of memory. This method copies the contents of structure to the block of memory pointed to by ptr. If fDeleteOld is true, the buffer originally pointed to by ptr will be deleted. This is accomplished by calling the appropriate 'delete' API on the embedded pointer. StructureToPtr does this tidy-up for every reference field specified in the 'mirror' managed class.
[Visual Basic] Public Shared Sub StructureToPtr( _ ByVal structure As Object, _ ByVal ptr As Integer, _ ByVal fDeleteOld As Boolean _ ) [C#] public static void StructureToPtr( object structure, int ptr, bool fDeleteOld ); [C++] public: static void StructureToPtr( Object* structure, int ptr, bool fDeleteOld ); [JScript] public static function StructureToPtr( structure : Object, ptr : int, fDeleteOld : Boolean );
Suppose your unmanaged block of memory is pointed to by Ptr. [its layout is described by a corresponding managed class, called Structure]. StructureToPtr will marshall field values from Structure to Ptr.
Suppose your Ptr block includes a reference field, pointing to a string buffer, and currently holding "abc". Suppose the corresponding field on the managed side is a string holding "vwxyz". If you don't tell it otherwise, StructureToPtr will allocate a new unmanaged buffer to hold "vwxyz", and hook it up to the Ptr block. But it will cast the old buffer "abc" adrift, without freeing it back to the unmanaged heap; so you end up with an orphan buffer that represents a memory leak in your code. However, if you set the fDeleteOld bit true, then StructureToPtr frees the buffer holding "abc" before going on to allocate a new buffer for "vwxyz". So, no heap leak!
Marshal Class | Marshal Members | System.Runtime.InteropServices Namespace