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. Platform Interop - Structures and Arrays

Platform Interop - Structures and Arrays

Scheduled Pinned Locked Moved C#
csharpc++comdata-structuresperformance
1 Posts 1 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.
  • M Offline
    M Offline
    Mikko Puonti
    wrote on last edited by
    #1

    Hi! I have following structures: [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )] struct ISEG { public short soffset; // segment position (offset) public short slength; // segment length public short segmode; // segment mode } [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )] struct IIDX { public short inumseg; // number of segments public IntPtr seg; // (ISEG*) segment information // + some other data } So IIDX structure contains pointer to array of ISEG structures, inumseg defines how many structures there is in array. And I have following DLL function that uses IIDX structure: // short (__cdecl * Open)( pIIDX indexPtr ); [DllImport( "mydll.dll")] private static extern short Open( ref IIDX indexPtr ); In unmanaged C/C++ you would use structure following way: IIDX udtIndex; ISEG udtBseg[2]; udtIndex.inumseg = 2; udtIndex.seg = udtBseg; udtBseg[0].soffset = 1; udtBseg[0].slength = 2; udtBseg[0].segmode = 1; udtBseg[1].soffset = 3; udtBseg[1].slength = 2; udtBseg[1].segmode = 1; Open( &udtIndex ); So, my problem is how I marshal ISEG array pointer to IIDX structure in C#? Should I first allocate memory for two ISEG structures, and then marshal each ISEG structures to right positions in the allocated memory? Or is there easier way to do marshaling (like function or attribute)? Could I use unsafe blocks to get pointer to the array? Any samples? Thank you!

    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