O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

HandleRef

Synopsis

When you pass a managed object into unmanaged code using PInvoke, there is a chance that the garbage collector will finalize the object before the unmanaged code is finished with it. This can only happen when your managed code does not reference the object after the PInvoke call. Because the garbage collector’s reach does not extend into unmanaged code, this fools the garbage collector into thinking that you are finished with it.

This class is used to wrap your managed object before passing it into unmanaged code, and you are guaranteed that the garbage collector will not touch it until the PInvoke call returns.

public struct HandleRef {
// Public Constructors
   public method HandleRef(object wrapper, IntPtr handle);  
// Public Instance Properties
   public field IntPtr Handle{get; } 
   public field object Wrapper{get; } 
// Public Static Methods
   public static explicit operator method IntPtr(
        HandleRef value);  
}

Hierarchy

System.ObjectSystem.ValueTypeHandleRef

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required