Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping unmanaged data to a managed structure in .NET
    primarykey
    data
    text
    <p>I have spent many hours working with unmanaged code, and platform invoke in .NET. The code below illustrates something that is puzzling me regarding how unmanaged data is mapped to a managed object in .NET.</p> <p>For this example I am going to use the <a href="http://msdn.microsoft.com/en-gb/library/windows/desktop/dd162897%28v=vs.85%29.aspx">RECT</a> structure:</p> <p><strong>C++ RECT implementation (unmanaged Win32 API)</strong></p> <pre><code>typedef struct _RECT { LONG left; LONG top; LONG right; LONG bottom; } RECT, *PRECT; </code></pre> <p><strong>C# RECT implementation (managed .NET/C#)</strong></p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct RECT { public int left, top, right, bottom; } </code></pre> <p>Okay, so my C# equivalent should work, right? I mean, all of the variables are in the same order as the C++ struct, and it's using the same variable names.</p> <p>My assumption of <code>LayoutKind.Sequential</code> means that the unmanaged data is mapped to the managed object in the same sequence it appears in the C++ structure. i.e. the data will be mapped, starting with left, then top, then right, then bottom.</p> <p>On this basis I should be able to modify my C# structure...</p> <p><strong>C# RECT implementation (a bit cleaner)</strong></p> <pre><code>[StructLayout(LayoutKind.Sequential)] public struct Rect //I've started by giving it a .NET compliant name { private int _left, _top, _right, _bottom; // variables are no longer directly accessible. /* I can now access the coordinates via properties */ public Int32 Left { get { return _left; } set { this._left = value; } } public Int32 Top { get { return _top; } set { this._top = value; } } public Int32 Right { get { return _right; } set { this._right = value; } } public Int32 Bottom { get { return _bottom; } set { this._bottom = value; } } } </code></pre> <p>So what happens if the variables are declared in the wrong order? Presumably this screws up the coordinates because they will no longer be mapped to the correct thing?</p> <pre><code>public struct RECT { public int top, right, bottom, left; } </code></pre> <p>At a guess, this would map like so:</p> <blockquote> <p>top = left </p> <p>right = top </p> <p>bottom = right </p> <p>left = bottom</p> </blockquote> <p>So my question is simply, Am I correct in my assumption that, I can modify the managed structure in terms of the access specifier of each variable, and even the variable name, but I cannot alter the order of the variables?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload