Home All Groups Group Topic Archive Search About

Pinning a managed structure

Author
29 Jun 2006 12:43 PM
Ulrich Sprick
Hi all,

is there a simple possibility to pin down a C# class member (a structure) so
that I can use it in an unmanaged DLL?
I only found the C# fixed() statement, but the structure is used throughout
the whole application lifetime, so fixed() does not seem to be the method of
choice...

Any ideas?

Thanks in advance,
ulrich

Author
29 Jun 2006 3:31 PM
William Sullivan
I believe you have to grab a GC handle for the object and pass the
constructor an enum saying that you want it to be pinned.  From an example I
snagged:

GCHandle dontMoveMe = GCHandle.Alloc( thePinnedObject, GCHandleType.Pinned );

Show quote
"Ulrich Sprick" wrote:

> Hi all,
>
> is there a simple possibility to pin down a C# class member (a structure) so
> that I can use it in an unmanaged DLL?
> I only found the C# fixed() statement, but the structure is used throughout
> the whole application lifetime, so fixed() does not seem to be the method of
> choice...
>
> Any ideas?
>
> Thanks in advance,
> ulrich
>
>
>
Author
30 Jun 2006 5:30 PM
Mattias Sjögren
Ulrich,

>I only found the C# fixed() statement, but the structure is used throughout
>the whole application lifetime, so fixed() does not seem to be the method of
>choice...

Then pinning isn't the right choice at all, it's only meant for short
periods of time. Have you considered allocating and sharing an
unmanaged memory buffer between your managed and native code? On the
C# side you can read and write a struct to/from that buffer.


Mattias

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

AddThis Social Bookmark Button