470,613 Members | 1,742 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

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.AllocHGlobal(abc.names* Marshal.SizeOf(typeof(Detail)));
Marshal.StructureToPtr(obj, (IntPtr)(abc.names.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.


int size = Marshal.SizeOf(typeof(Name));
IntPtr data = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(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(data, 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.PtrToStructure(proxyCtx, 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.
Sep 18 '08 #1
0 1905

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by William Stacey | last post: by
2 posts views Thread by Stefan | last post: by
6 posts views Thread by william.thorpe.b | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.