471,325 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Get IntPtr to Current Object?

I have a class that has an Explicit StructLayout. In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. Would there be a way to get an IntPtr
directly to the current object?

public EntityState(byte[] rawData)
{
IntPtr pData = Marshal.AllocHGlobal(rawData.Length);
Marshal.Copy(rawData, 0, pData, 144);
Marshal.PtrToStructure(pData, this);
Marshal.FreeHGlobal(pData);
}
Jul 11 '08 #1
2 7478
On Jul 11, 9:17 am, "O.B." <funkj...@bellsouth.netwrote:
I have a class that has an Explicit StructLayout. In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. Would there be a way to get an IntPtr
directly to the current object?

public EntityState(byte[] rawData)
{
IntPtr pData = Marshal.AllocHGlobal(rawData.Length);
Marshal.Copy(rawData, 0, pData, 144);
Marshal.PtrToStructure(pData, this);
Marshal.FreeHGlobal(pData);

}
Actually, I think this will work. Any gotchas that I should be aware
of?

public EntityState(byte[] rawData)
{
unsafe
{
fixed (byte* pData = rawData)
{
Marshal.PtrToStructure((IntPtr)pData, this);
}
}
}
Jul 11 '08 #2
On Jul 11, 6:17*pm, "O.B." <funkj...@bellsouth.netwrote:
I have a class that has an Explicit StructLayout. *In the constructor
below, I am copying data from a byte array onto this class so as to
initialize all the explicit attributes of the class. *What I don't
like is the overhead of allocating memory for a temporary variable
(pData) when I want to marshal the data in the rawData array directly
into the current object. *Would there be a way to get an IntPtr
directly to the current object?
You can't get a pointer to an object, but you can just get a plain
pointer to the first data member (using operator unary & as usual),
which, if you did the layout correctly, should be equivalent. Just
don't forget to pin it using "fixed". A pointer can then be cast to
IntPtr.
Jul 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nuno Esculcas | last post: by
3 posts views Thread by Ron Vecchi | last post: by
13 posts views Thread by Christian Westerlund | last post: by
5 posts views Thread by Sakharam Phapale | last post: by
3 posts views Thread by Richards | last post: by
reply views Thread by Jørn Jensen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.