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
  1. Home
  2. General Programming
  3. C#
  4. sizeof(struct) in C#.net

sizeof(struct) in C#.net

Scheduled Pinned Locked Moved C#
csharpjsontutorialquestion
4 Posts 3 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.
  • E Offline
    E Offline
    eyeshield21
    wrote on last edited by
    #1

    hi, I want to ask how to get size of struct that I made? ex: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MT_EVENT { public Int64 TimeStamp; // In FILETIME format //public LARGE_INTEGER TimeStamp; // In FILETIME format public uint UserStatus; // Not used by API public uint EventCode; // Event Code public uint SubReason; // Event sub-reason public uint XtraInfo; // Extra information, e.g. termination public uint FuncCode; // Function active when this event occurred public uint Board; // Board ID public uint Channel; // Global Channel ID (GCI) public IntPtr ptrBuffer; // Related play/record buffer pointer public uint DataLength; // Byte length of data accessed (played/recorded) public IntPtr ptrXtraBuffer; // Pointer to xtra buffer public uint XtraBufferLength; // Length of buffer pointed by ptrXtraBuffer public uint XtraDataLength; // Length of data in buffer pointed by ptrXtraBuffer public uint EventFlag; // Falgs of the following: // bit 0x00000001: 1 - Appl created the event // 0 - NTi DLL created the event // bit 0x00000002: 1 - Appl allocated ptrtraBuffer // 0 - NTi DLL allocated ptrXtraBuffer }

    E 1 Reply Last reply
    0
    • E eyeshield21

      hi, I want to ask how to get size of struct that I made? ex: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct MT_EVENT { public Int64 TimeStamp; // In FILETIME format //public LARGE_INTEGER TimeStamp; // In FILETIME format public uint UserStatus; // Not used by API public uint EventCode; // Event Code public uint SubReason; // Event sub-reason public uint XtraInfo; // Extra information, e.g. termination public uint FuncCode; // Function active when this event occurred public uint Board; // Board ID public uint Channel; // Global Channel ID (GCI) public IntPtr ptrBuffer; // Related play/record buffer pointer public uint DataLength; // Byte length of data accessed (played/recorded) public IntPtr ptrXtraBuffer; // Pointer to xtra buffer public uint XtraBufferLength; // Length of buffer pointed by ptrXtraBuffer public uint XtraDataLength; // Length of data in buffer pointed by ptrXtraBuffer public uint EventFlag; // Falgs of the following: // bit 0x00000001: 1 - Appl created the event // 0 - NTi DLL created the event // bit 0x00000002: 1 - Appl allocated ptrtraBuffer // 0 - NTi DLL allocated ptrXtraBuffer }

      E Offline
      E Offline
      eyeshield21
      wrote on last edited by
      #2

      can I use this: MT_EVENT evt = new MT_EVENT(); uint length= (uint) Marshal.SizeOf(evt);

      L L 2 Replies Last reply
      0
      • E eyeshield21

        can I use this: MT_EVENT evt = new MT_EVENT(); uint length= (uint) Marshal.SizeOf(evt);

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

        Yes, Marshal.SizeOf() Is equlant to sizeof() in c++ and c. It is used to call WinAPI. But this needs to be before defining class:[StructLayout(LayoutKind.Sequential)] Example: [StructLayout(LayoutKind.Sequential)] public class SomeClass { int i; }

        1 Reply Last reply
        0
        • E eyeshield21

          can I use this: MT_EVENT evt = new MT_EVENT(); uint length= (uint) Marshal.SizeOf(evt);

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

          Hi, if you are going to pass such a struct to the native world, that would be the way to go, yes. BTW: the value will vary, depending on Win32/Win64 as an IntPtr is elastic. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          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