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 / New Users / June 2006

Tip: Looking for answers? Try searching our database.

Pinning a managed structure

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ulrich Sprick - 29 Jun 2006 13:43 GMT
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
William Sullivan - 29 Jun 2006 16:31 GMT
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 );

> Hi all,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> ulrich
Mattias Sjögren - 30 Jun 2006 18:30 GMT
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

Signature

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


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.