Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2006

Tip: Looking for answers? Try searching our database.

Get length of IntPtr in bytes?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris  Ashley - 18 Sep 2006 10:21 GMT
I am using Interop with a C++ function which returns a pointer to an
array of bytes. I'm using an IntPtr and Marshal.Copy to populate a
managed array of bytes in C#, however I don't know the size I need to
allocate to the array. Is there any way of calculating the size of the
memory IntPtr is pointing to in bytes? If not, is there an alternative
way of accomplishing this? Should I be using IntPtr at all in this
scenario?
TDC - 18 Sep 2006 14:50 GMT
It's impossible to know without you providing us the function prototype
getting called.

It's *probably* a ptr to a null-terminated char array.

An API that returns data without providing a size in some way is not
usable, so I'm sure it's there somewhere.

Post more info and we can help!

Tom

> I am using Interop with a C++ function which returns a pointer to an
> array of bytes. I'm using an IntPtr and Marshal.Copy to populate a
[quoted text clipped - 3 lines]
> way of accomplishing this? Should I be using IntPtr at all in this
> scenario?
fredrik.claesson@gmail.com - 19 Sep 2006 21:11 GMT
Hello,

This post has an interesting connection to a problem I am dealing with
currently.
I send an unmanaged array (unsigned chars) från unmanaged C++ to C#
using P/Invoke and a callback/delegate.

I use Marshal.Copy to copy the data to a managed array. For performance
reasons I would perfer not to copy the data (large images) at all.
However as I have control of the allocation of the unmanaged array and
would like to create a managed "wrapper" array (which is considered to
be pinned by the GC) which points to the unmanaged data.

I have not been able to do this and and am therefore wondering if there
are reasons for not doing this at all? As the binary layout of the
managed array is just like a normal unmanaged array it would not seem
impossible to do this.

A solution which does not require me to copy the data is to allocated a
managed array, pin the data (using a GCHandle) and pass it to the
unmanaged code. Is this the only "no-copy" transfer of arrays from
unmanaged to managed code possible?

/Fredrik

> It's impossible to know without you providing us the function prototype
> getting called.
[quoted text clipped - 15 lines]
> > way of accomplishing this? Should I be using IntPtr at all in this
> > scenario?
fredrik.claesson@gmail.com - 19 Sep 2006 21:45 GMT
Hej!

Seem like I find the answer when I review older post. In the post
"Is Marshal.Copy() really necessary to pass an array to/from
managed/unmanaged code?" Mattias Sjögren answers a similar question.
So it appear I could try using
the unsafe keyword to access to data directly.

/Fredrik

> Hello,
>
[quoted text clipped - 20 lines]
>
> /Fredrik

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.