Marshal.GetIUnknownForObjectInContext(Object) 方法

定义

如果调用方与托管对象在同一上下文中,则从该对象返回一个 IUnknown 接口。

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 -> nativeint
Public Shared Function GetIUnknownForObjectInContext (o As Object) As IntPtr

参数

o
Object

IUnknown 接口被请求的对象。

返回

IntPtr

nativeint

指定对象的 IUnknown 指针;如果调用方与指定对象不在同一上下文中,则为 null

属性

示例

以下示例演示如何使用 GetIUnknownForObjectInContext 方法检索IUnknown托管对象的接口。

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

注解

此方法与 相同 GetIUnknownForObject ,只是当调用方与 对象不在同一上下文中时,此方法返回 null

适用于

另请参阅