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. Marshalling MyStruct[] from C# to C++ API

Marshalling MyStruct[] from C# to C++ API

Scheduled Pinned Locked Moved C#
csharpc++commcpdata-structures
2 Posts 2 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.
  • G Offline
    G Offline
    Grimolfr
    wrote on last edited by
    #1

    I have an API I'm wrapping in C#, and I'm having trouble tweaking the DllImport. Here's the struct definition in C#:

    [
    MarshalAs(
    UnmanagedType.Struct,
    SizeConst = 512,
    CharSet = CharSet.Ansi
    )
    ]
    public struct MyStruct
    {
    [MarshalAs(UnmanagedType.I4)]public Int32 Size;
    [MarshalAs(UnmanagedType.LPStr, SizeConst = 508)]public String Name;
    }

    In C:

    public struct MyStruct
    {
    int size,
    char[508] name
    }

    And the C function declaration:

    EXPORT void ListFiles(void* buffer, DWORD count)
    {
    //Populate the buffer with /count/ records
    }

    The idea is to pass in a pointer to a buffer. The API function will fill that buffer with count records. Each record being byte data in the format of MyStruct. I can successfully retrieve the data by passing in a buffer of type byte[512 * count]. But then I have to manually parse the data with BitConverter calls in a for loop. What I'm trying to do is declare an array of MyStruct[count], and set up a DllImport that will properly marshal that array into the API, so that the API can fill the structures directly, with no need for me to parse data on return. I've had no success with this whatsoever using MarshalAs(UnmanagedType.LPArray) or trying to let the compiler do automatic marshalling. Everything I've tried results in an array of structures that all have size 0 and null names. Anyone have any pointers or suggestions to help me figure out how to marshal an array of structs from C# as a void* in C++?


    Grim

    (aka Toby)

    MCDBA, MCSD, MCP+SB

    Need a Second Life?[^]

    SELECT * FROM users WHERE clue IS NOT NULL GO

    (0 row(s) affected)

    D 1 Reply Last reply
    0
    • G Grimolfr

      I have an API I'm wrapping in C#, and I'm having trouble tweaking the DllImport. Here's the struct definition in C#:

      [
      MarshalAs(
      UnmanagedType.Struct,
      SizeConst = 512,
      CharSet = CharSet.Ansi
      )
      ]
      public struct MyStruct
      {
      [MarshalAs(UnmanagedType.I4)]public Int32 Size;
      [MarshalAs(UnmanagedType.LPStr, SizeConst = 508)]public String Name;
      }

      In C:

      public struct MyStruct
      {
      int size,
      char[508] name
      }

      And the C function declaration:

      EXPORT void ListFiles(void* buffer, DWORD count)
      {
      //Populate the buffer with /count/ records
      }

      The idea is to pass in a pointer to a buffer. The API function will fill that buffer with count records. Each record being byte data in the format of MyStruct. I can successfully retrieve the data by passing in a buffer of type byte[512 * count]. But then I have to manually parse the data with BitConverter calls in a for loop. What I'm trying to do is declare an array of MyStruct[count], and set up a DllImport that will properly marshal that array into the API, so that the API can fill the structures directly, with no need for me to parse data on return. I've had no success with this whatsoever using MarshalAs(UnmanagedType.LPArray) or trying to let the compiler do automatic marshalling. Everything I've tried results in an array of structures that all have size 0 and null names. Anyone have any pointers or suggestions to help me figure out how to marshal an array of structs from C# as a void* in C++?


      Grim

      (aka Toby)

      MCDBA, MCSD, MCP+SB

      Need a Second Life?[^]

      SELECT * FROM users WHERE clue IS NOT NULL GO

      (0 row(s) affected)

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Although I don't know the answer, I may have a tip to you: I learned that, for some more complicated cases, a managed C++ wrapper is the way to go. Interop coding that takes hours or days of coding in C# often takes only a few minutes in managed C++. Unless you have a strong reason to stay with "pure C#", I'd consider using managed C++. I see dead pixels Yes, even I am blogging now!

      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