Marshal.GetIUnknownForObject(Object) 方法

定義

從 Managed 物件傳回 IUnknown 介面。

public:
 static IntPtr GetIUnknownForObject(System::Object ^ o);
[System.Security.SecurityCritical]
public static IntPtr GetIUnknownForObject (object o);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static IntPtr GetIUnknownForObject (object o);
public static IntPtr GetIUnknownForObject (object o);
[<System.Security.SecurityCritical>]
static member GetIUnknownForObject : obj -> nativeint
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetIUnknownForObject : obj -> nativeint
static member GetIUnknownForObject : obj -> nativeint
Public Shared Function GetIUnknownForObject (o As Object) As IntPtr

參數

o
Object

IUnknown 介面受要求的物件。

傳回

IntPtr

nativeint

o 參數的 IUnknown 指標。

屬性

範例

下列範例示範如何使用 方法來擷取 Managed 物件的 GetIUnknownForObjectIUnknown介面。

using System;
using System.Runtime.InteropServices;

class Program
{

    static void Run()
    {

        // Create an int object
        int obj = 1;

        Console.WriteLine("Calling Marshal.GetIUnknownForObject...");

        // Get the IUnKnown pointer for the Integer object
        IntPtr pointer = Marshal.GetIUnknownForObject(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.GetIUnknownForObject...")

        ' Get the IUnKnown pointer for the Integer object
        pointer = Marshal.GetIUnknownForObject(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

備註

在 Managed 程式碼中,您很少會直接使用 IUnknown 介面。 不過, GetIUnknownForObject 呼叫將 COM 物件參數公開為 IntPtr 型別或自訂封送處理的方法時,會很有用。 使用這個方法呼叫 物件會導致傳回指標之前,介面指標上的參考計數遞增。 當您完成指標之後,一律使用 Marshal.Release 來遞減參考計數。 這個方法提供 方法的 Marshal.GetObjectForIUnknown 相反功能。

您也可以在 Managed 物件上使用這個方法,以取得物件的 COM 可呼叫包裝函 式的介面指標。

適用於

另請參閱