How to Marshal an IntPtr containing one more IntPtr

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

    How to Marshal an IntPtr containing one more IntPtr

    Hi All,
    I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out.

    I have an structure something like:

    public struct Detail
    {
    public int age;

    public Detail(int _age)
    {
    age = _age;
    }
    }

    public struct Name
    {
    public IntPtr names;
    }


    While trying to Marshal the above code I am marshalling the Object of Detail struct using IntPtr Names: i.e,

    Name abc = new Name();

    Detail obj = new Detail(2);
    int sizeDetailobj = Marshal.SizeOf( typeof(Detail)) ;

    abc.names= Marshal.AllocHG lobal(abc.names * Marshal.SizeOf( typeof(Detail)) );
    Marshal.Structu reToPtr(obj, (IntPtr)(abc.na mes.ToInt32() + sizeDetailobj), false);



    Furthurmore I need to pass the object of struct Name which contains the IntPtr to struct Detail along with some other fields.

    as,

    int size = Marshal.SizeOf( typeof(Name));
    IntPtr data = Marshal.AllocHG lobal(size);
    Marshal.Structu reToPtr(abc, data, true);


    Here marshalling happens well without any problem,

    For sending the message over Network i am using BEEP Protocol, which converts data into byte[]

    as byte[] dataBytes = new byte[size];
    Marshal.Copy(da ta, dataBytes, 0, size);


    And at the receiving end while unmarshalling,
    I am unmarshalling As;

    IntPtr proxyCtx ; //which gets the databyte value

    Name namebuf = (Name)Marshal.P trToStructure(p roxyCtx, typeof(Name)); // At this step i got the desired result as IntPtr abc.names

    but the IntPtr received itself contains an IntPtr to struct Detail, while unmarshalling;

    Detail obj1 = new Detail();
    int structSize = Marshal.SizeOf( typeof(Detail)) ;
    obj1 = (Detail)Marshal .PtrToStructure (namebuf.names, typeof(Detail)) ;


    At this point i was suppose to get the value of age in obj1.age field but instead i am getting some garbage values.

    Plz Suggest what i am suppose to do to achieve required value.

    Thanks in Advance,
    Eric Haas.
Working...