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 / June 2005

Tip: Looking for answers? Try searching our database.

do you need to pin on passing byte[] to native?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adam - 03 Jun 2005 21:12 GMT
In browsing some of the framework code with reflector, I see a char buffer
being pinned to be passed to native code. A byte array however is not. Does a
byte array need to be pinned before being passed to native code?
System.Web.Hosting.IsapiWorkerRequestInProc.GetServerVariableCore
uses a byte buffer that is never pinned. Is this an error or do byte buffers
not need to be pinned?
William DePalo [MVP VC++] - 04 Jun 2005 00:06 GMT
> In browsing some of the framework code with reflector, I see a char buffer
> being pinned to be passed to native code. A byte array however is not.
[quoted text clipped - 4 lines]
> buffers
> not need to be pinned?

In the case of no pinning, is the object allocated on the stack or inline
within an object on the managed heap?

A object that is on the stack is not subject to being collected or moved.

Regards,
Will
Adam - 04 Jun 2005 01:39 GMT
Heres what Im seeing:
A framework object developed in c# by microsoft declares a byte array (not
using the unsafe keyword) just a regular byte array and passes it into a
native code method that uses it just like it would a char[]. The native code
fills it in and passes it back to the managed library. Seems to me the byte
array should be pinned via GCHandle.Alloc, but Im not seeing this. It should
be pinned, no?
Also, what about the case with a managed array containing strings - the
array is pinned, does every string in the array need to be pinned as well, or
is the runtime smart enough to realize this situation?
Thanks
Adam

> > In browsing some of the framework code with reflector, I see a char buffer
> > being pinned to be passed to native code. A byte array however is not.
[quoted text clipped - 12 lines]
> Regards,
> Will
Mattias Sjögren - 04 Jun 2005 10:36 GMT
>Does a byte array need to be pinned before being passed to native code?

The CLR pins arguments passed to native code for the duration of the
call. So as long as it's a synchronous call (the native code doesn't
store the pointer and use it after the function has returned) you
don't have to explicitly pin the array.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Adam - 06 Jun 2005 19:05 GMT
ah... that makes sense then as to why you don't see buffers in regular win32
api calls pinned.
So then why would there be an explicit pin for reading in a char array (for
unicode) and no pin for a bytearray when passing both items to synchronous
methods in the Framework System.Web.Hosting.IsapiWorkerRequestInProc class?
See GetServerVariableCore and the unicode on was well using reflector.

> >Does a byte array need to be pinned before being passed to native code?
>
[quoted text clipped - 4 lines]
>
> Mattias
Mattias Sjögren - 06 Jun 2005 22:41 GMT
>So then why would there be an explicit pin for reading in a char array (for
>unicode) and no pin for a bytearray when passing both items to synchronous
>methods in the Framework System.Web.Hosting.IsapiWorkerRequestInProc class?
>See GetServerVariableCore and the unicode on was well using reflector.

I don't know, I can only speculate. It seems like they are reusing the
same buffer, presumably because it will be promoted to an older GC
heap generation after a while, and it probably hurts less to pin
things on gen2 than gen0 for example.

As to why they are manually pinning, perhaps it's cheaper to
explicitly pin once and pass the same IntPtr (opaque to the marshaler)
to native code, than to rely on the marshaler to pin/unpin for every
call.

I don't know why they aren't doing the same thing for the byte buffer,
but then I haven't really studied the usage pattern. Perhaps it's the
fact that the strings have to be decoded from UTF8 anyway so such an
optimization might not make sense in this case. Hopefully the ASP.NET
team have a good reason for doing it the way they are.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

"Peter Huang" [MSFT] - 08 Jun 2005 08:01 GMT
Hi

Commonly we need to pin the memory, but if we can ensure the memory will
not be Collected during the lifetime of the bytearray, it can be omitted.
Also since the detailed implement is not public and the reflector may not
display the "real" of our implement, it may cause some confusion.

So for your concern, I think you may follow the common rule to pin the
memory if you can not guarantee the byte array will be collected.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Adam - 08 Jun 2005 16:11 GMT
Thanks for the help guys.
So as Mattias mention, if this is a known synchronous call to native code
from c#, will interop services automatically pin this for us so it doesn't
need to be done explicitly?

> Hi
>
[quoted text clipped - 13 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Peter Huang" [MSFT] - 09 Jun 2005 03:19 GMT
Hi

Based on my understanding, for synchronous call, the GC will have no chance
to collected it.
e.g.
byte[] bts = new byte[3];
int rt = UnmanagedPInvoke(bts);
//until the rt is return the bts is still alive, the bts will not be
collected.

But for asynchronous scenario, it is better to pin the memory to ensure the
byte array will not be removed in case the byte array var is out of
lifetime.

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


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.