Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. [StructLayout(LayoutKind.Sequential)]

[StructLayout(LayoutKind.Sequential)]

Scheduled Pinned Locked Moved C#
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3bood ghzawi
    wrote on last edited by
    #1

    Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

    L 0 D L D 5 Replies Last reply
    0
    • 3 3bood ghzawi

      Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Maybe this[^] helps. If not, ask more specific questions, with context and purpose. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.


      1 Reply Last reply
      0
      • 3 3bood ghzawi

        Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        Normally the compiler pads structures out so that it can use optimised four-byte transfer operations. But different compilers do that in different ways. LayoutKind.Sequential lays the structure out exactly as you specify, not how the compiler thinks will run well. It's commonly used in PInvoke, because C# and C compilers are likely to pad things out differently.

        OSDev :)

        1 Reply Last reply
        0
        • 3 3bood ghzawi

          Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Believe it or not, MSDN is your friend. Search it.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          1 Reply Last reply
          0
          • 3 3bood ghzawi

            Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            If you don't specify it, the compiler would be free to sort the fields by size so as to minimize padding.

            1 Reply Last reply
            0
            • 3 3bood ghzawi

              Hi all, i counter this statment at some code, but i don't undestand what means, so, i need helping of what it means in detail... [StructLayout(LayoutKind.Sequential)] Thanks......

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              It's commonly used when using external C/C++ dlls (PInvoke). When a structure is being passed as a parameter to an external function then the called C/C++ function needs to know exactly where to find the relavent parts of the structure in memory. It does this using byte offsets from the start address (pointer) of the struct. Imagine you had this structure

              public struct MyStruct
              {
              public byte a;
              public int b;
              }

              Without specifying the LayoutKind, this could end up being layed out in memory with b first. This would be catastrophic for the external function as it would assume the fields to be layed out in sequence - so with a first. As a result the data retrieved from memory would be incorrect. To make sure that the sequence of fields is preserved, we use LayoutKind.Sequential. Another common one is LayoutKind.Explicit (most commonly when used with C/C++ unions). Here you explicitly state the offset for each field using the FieldOffset attribute.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups