List Info

Thread: turn IntPtr into byte[] w/o copying/reallocating data?




turn IntPtr into byte[] w/o copying/reallocating data?
user name
2006-05-15 07:12:29
Oh, sorry, didn't notice you didn't want to copy the data.

I assume you want the data in "byte[]" to
perform managed tasks
(otherwise, you could treat the pinned pointer as an array
in C-style),
however, then, what you desire is impossible, because to
obtain a
managed reference you will have to marshal/copy the data
from unmanaged
to managed, 
e.g., by Marshal.PtrToStructure (notice, however, that
byte[] doesn't
have parameterless ctor), or by way of example in the
previous post.

Igal Yoffe
Software Engineer and Architect
mPrest Systems Ltd. 
972-3-9291515 (ext. 2720)
 

> -----Original Message-----
> From: Discussion of development on the .NET platform
using 
> any managed language [mailtoOTNET-CL
RDISCUSS.DEVELOP.COM] 
> On Behalf Of Igal Yoffe
> Sent: Monday, May 15, 2006 9:09 AM
> To: DOTNET-CLRDISCUSS.DEVELOP.COM
> Subject: Re: [DOTNET-CLR] turn IntPtr into byte[] w/o 
> copying/reallocating data?
> 
> How about:
> 
> byte[] dataBuf;
> 
> ...
> GCHandle bufHandle = GCHandle.Alloc(dataBuf, 
> GCHandleType.Pinned); IntPtr ptr = 
> bufHandle.AddrOfPinnedObject(); 
>
<Read>((byte*)ptr.ToPointer(),<required_length>)
;
> ....
> 
> Where <Read> would be based on: 
>
[System.Runtime.InteropServices.DllImport("kernel32.dl
l")]
> private static extern void CopyMemory(byte *pDest,byte 
> *pSrc,int pSize);
> 
> 
> Igal Yoffe
> Software Engineer and Architect
> mPrest Systems Ltd. 
> 972-3-9291515 (ext. 2720)
>  
> 
> > -----Original Message-----
> > From: Discussion of development on the .NET
platform using 
> any managed 
> > language [mailtoOTNET-CL
RDISCUSS.DEVELOP.COM]
> > On Behalf Of Kirk Marple
> > Sent: Sunday, May 14, 2006 1:59 AM
> > To: DOTNET-CLRDISCUSS.DEVELOP.COM
> > Subject: [DOTNET-CLR] turn IntPtr into byte[] w/o 
> copying/reallocating 
> > data?
> > 
> > anyone know how to take an IntPtr and turn it into
a byte[] w/o 
> > requiring any data copies or reallocation of
memory?
> > 
> > (the IntPtr is coming from BitmapData.Scan0, so it
references an 
> > unmanaged image buffer.)
> > 
> > i've been looking at GCHandles, and other things,
and don't see an 
> > obvious way to do this.
> > 
> > any thoughts?  is this even possible?
> > 
> > thanks!
> > 
> > -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
> > Kirk Marple
> > Chief Software Architect, VP of Engineering
Agnostic Media, Inc.
> > e: kirkagnostic-media.com
> > w: www.agnostic-media.com <http://www.agnosti
c-media.com/>
> > 
> > 
> > ===================================
> > This list is hosted by DevelopMentor(r)  http://www.develop.com
> > 
> > View archives and manage your subscription(s) at 
> > http://discuss.develop.com

> > 
> 
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
> 
> View archives and manage your subscription(s) at 
> http://discuss.develop.com

> 

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )