Compartir a través de


Método Marshal.GetIUnknownForObject (Object)

 

Publicado: octubre de 2016

Devuelve un IUnknown interfaz desde un objeto administrado.

Espacio de nombres:   System.Runtime.InteropServices
Ensamblado:  mscorlib (en mscorlib.dll)

Sintaxis

[SecurityCriticalAttribute]
public static IntPtr GetIUnknownForObject(
    object o
)
public:
[SecurityCriticalAttribute]
static IntPtr GetIUnknownForObject(
    Object^ o
)
[<SecurityCriticalAttribute>]
static member GetIUnknownForObject : 
        o:Object -> nativeint
<SecurityCriticalAttribute>
Public Shared Function GetIUnknownForObject (
    o As Object
) As IntPtr

Parámetros

  • o
    Type: System.Object

    Objeto cuya interfaz IUnknown se solicita.

Valor devuelto

Type: System.IntPtr

Puntero IUnknown para el parámetro o.

Comentarios

En código administrado, rara vez se trabaja directamente con el IUnknown interfaz. Sin embargo, GetIUnknownForObject es útil cuando se llama a un método que expone un parámetro de objeto COM como un IntPtr tipo, o con el cálculo de referencias personalizado. Llamar a un objeto con este método hace que el recuento de referencia aumenta en el puntero de interfaz antes de que se devuelva el puntero. Utilice siempre Marshal.Release para disminuir el recuento de referencias cuando haya terminado con el puntero. Este método proporciona la funcionalidad opuesta a la de la Marshal.GetObjectForIUnknown (método).

También puede utilizar este método en un objeto administrado para obtener un puntero de interfaz a la COM Callable Wrapper para el objeto.

Ejemplos

En el ejemplo siguiente se muestra cómo recuperar un IUnknown interfaz para un objeto administrado mediante el GetIUnknownForObject método.

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

Seguridad

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Información de versión

Plataforma universal de Windows
Disponible desde 8
.NET Framework
Disponible desde 1.1
Biblioteca de clases portable
Se admite en: plataformas portátiles de .NET
Windows Phone Silverlight
Disponible desde 8.0
Windows Phone
Disponible desde 8.1

Ver también

Release
GetObjectForIUnknown
Clase Marshal
Espacio de nombres System.Runtime.InteropServices

Volver al principio