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. Marshal Deeply Nested Struct Problem!

Marshal Deeply Nested Struct Problem!

Scheduled Pinned Locked Moved C#
helpcsharpc++
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.
  • J Offline
    J Offline
    jinzhecheng
    wrote on last edited by
    #1

    // the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((

    J L 2 Replies Last reply
    0
    • J jinzhecheng

      // the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((

      J Offline
      J Offline
      jinzhecheng
      wrote on last edited by
      #2

      Sorry, just saw some post , currently , struct array as a member of struct is not supported. But use flat stuct , member like this: arr1, arr2.... sounds not a good idea, Who can give a good work around?

      R 1 Reply Last reply
      0
      • J jinzhecheng

        Sorry, just saw some post , currently , struct array as a member of struct is not supported. But use flat stuct , member like this: arr1, arr2.... sounds not a good idea, Who can give a good work around?

        R Offline
        R Offline
        rudy net
        wrote on last edited by
        #3

        Just to give you an idea, try changing the line in structB From: [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] To: [MarshalAs(UnmanagedType.ByValArray, ArraySubType=UnmanagedType.LPStruct, SizeConst=4 )] Basically you may need to specify the type of data that you have in the array. In this case I used LPStruct but you can try other types. I don't understand why we wouldn't be able to pass other structures after all, it's just a bunch of bytes that have to be marshaled into your managed structures.

        1 Reply Last reply
        0
        • J jinzhecheng

          // the original c++ has those struct: typedef struct { long id; } StructA; typedef struct { long price StructA[4] idArray }StructB typedef struct { long date; StructB priceArray }StructC // a function use those struct void foo(StructC* quote) //============================================= // c# correspoding type //============================================= [StructLayout(LayoutKind.Sequential)] struct StructA; { int id; } [StructLayout(LayoutKind.Sequential)] struct StructB { long price [MarshalAs(UnmanagedType.ByValArray,SizeConst=4 )] StructA[] idArray } [StructLayout(LayoutKind.Sequential)] struct StructC { long date; StructB priceArray } // c# function void foo(ref StructC quote) It always complain StructB doesnt have layout info... stuck for whole day , really need help.:((

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          long in C/C++ is int (Int32) in C#. Also the person who wrote the C code should be shot. You should be able to replace everything with the following:

          struct Quote
          {
          int date;
          int price;
          int id0;
          int id1;
          int id2;
          int id3;
          }

          void foo(ref Quote quote);

          To check do a sizeof(StructC) in C and Marshal.SizeOf(typeof(Quote)) in C#. Both should be 24 bytes. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          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