Marshal.GetIUnknownForObjectInContext(Object) Method     
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an IUnknown interface from a managed object, if the caller is in the same context as that object.
public:
 static IntPtr GetIUnknownForObjectInContext(System::Object ^ o);public static IntPtr GetIUnknownForObjectInContext(object o);[System.Security.SecurityCritical]
public static IntPtr GetIUnknownForObjectInContext(object o);static member GetIUnknownForObjectInContext : obj -> nativeint[<System.Security.SecurityCritical>]
static member GetIUnknownForObjectInContext : obj -> nativeintPublic Shared Function GetIUnknownForObjectInContext (o As Object) As IntPtrParameters
- o
- Object
The object whose IUnknown interface is requested.
Returns
nativeint
The IUnknown pointer for the specified object, or null if the caller is not in the same context as the specified object.
- Attributes
Examples
The following example demonstrates how to retrieve an IUnknown interface for a managed object using the GetIUnknownForObjectInContext method.
using System;
using System.Runtime.InteropServices;
class Program
{
    static void Run()
    {
        // Create an int object
        int obj = 1;
        Console.WriteLine("Calling Marshal.GetIUnknownForObjectInContext...");
        // Get the IUnKnown pointer for the Integer object
        IntPtr pointer = Marshal.GetIUnknownForObjectInContext(obj);
        Console.WriteLine("Calling Marshal.Release...");
        // Always call Marshal.Release to decrement the reference count.
        Marshal.Release(pointer);
    }
    static void Main(string[] args)
    {
        Run();
    }
}
Imports System.Runtime.InteropServices
Module Program
    Sub Run()
        ' Dim an Integer object
        Dim IntegerObject As Integer = 1
        ' Dim a pointer
        Dim pointer As IntPtr
        Console.WriteLine("Calling Marshal.GetIUnknownForObjectInContext...")
        ' Get the IUnKnown pointer for the Integer object
        pointer = Marshal.GetIUnknownForObjectInContext(IntegerObject)
        Console.WriteLine("Calling Marshal.Release...")
        ' Always call Marshal.Release to decrement the reference count.
        Marshal.Release(pointer)
    End Sub
    Sub Main(ByVal args() As String)
        Run()
    End Sub
End Module
Remarks
This method is the same as GetIUnknownForObject except that it returns null if the caller is not in the same context as the object.