Converting Unmanaged array Struct to Managed One

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Charming12
    New Member
    • Sep 2008
    • 18

    Converting Unmanaged array Struct to Managed One

    Hi All,

    The question is regarding Unmanaged and Managed code conversion in C# for structures.

    I have two structures like:
    Code:
    public struct Detail
    {
    public int age;
    public string address;
    
    public Detail(int _age, string _address)
    {
    age = _age;
    address = _address;
    }
    }
    
    public struct Name
    {
    public Detail[] names;
    }
    While passing the code to StructureToPtr function:
    Code:
    Name some = new Name();
    
    some.names[0].age = 23;
    some.names[0].address = "123,ABC Farms";
    
    int Size = Marshal.SizeOf(some);
    IntPtr data = Marchal.AllocHGlobal(Size);
    Marshal.StructureToPtr(some, data, true);
    This code is showing error as Parameters are not correct, and if i tried to pass the same code without using array, it worked perfectly.

    Can anyone please suggest me what should i try to resolve the problem.
    Last edited by Frinavale; Sep 17 '08, 01:36 PM. Reason: added code tags
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    Check this thread. You will have to use IntPtr and then copy each struct in your array manually.

    Comment

    • Charming12
      New Member
      • Sep 2008
      • 18

      #3
      Thanks for the prompt reply but still i am not sure how to use IntPtr to pass into struct ..

      Comment

      Working...