Marshal.GetIUnknownForObjectInContext(Object) Method

Definition

Returns an IUnknown interface from a managed object, if the caller is in the same context as that object.

C#
public static IntPtr GetIUnknownForObjectInContext(object o);
C#
[System.Security.SecurityCritical]
public static IntPtr GetIUnknownForObjectInContext(object o);

Parameters

o
Object

The object whose IUnknown interface is requested.

Returns

IntPtr

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.

C#
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();
    }
}

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.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also