SafeBuffer.AcquirePointer(Byte*) 方法

定义

重要

此 API 不符合 CLS。

SafeBuffer 内存块的对象获取指针。

public:
 void AcquirePointer(System::Byte* % pointer);
[System.CLSCompliant(false)]
public void AcquirePointer(ref byte* pointer);
[<System.CLSCompliant(false)>]
member this.AcquirePointer : Byte* -> unit

参数

pointer
Byte*

由引用传递的字节指针,用于从对象内部 SafeBuffer 接收指针。 在调用此方法之前,必须设置此指针 null

属性

例外

Initialize尚未调用该方法。

注解

返回时 AcquirePointer ,应通过验证 pointer 参数是否为 null执行边界检查。 如果不是 null,则必须在受约束的执行区域中调用 SafeBuffer.ReleasePointer 该方法(CER)。

AcquirePointer SafeHandle.DangerousAddRef调用该方法并公开指针。

以下示例演示如何使用 AcquirePointer 该方法:

byte* pointer = null;
RuntimeHelpers.PrepareConstrainedRegions();
try {
    MySafeBuffer.AcquirePointer(ref pointer);
    // Use pointer here, with your own bounds checking.
    }
finally {
    if (pointer != null)
        MySafeBuffer.ReleasePointer();
    }

如果将指针(即指向字节的指针)强制转换为 pointer 指向其他类型的指针(T*),则可能存在指针对齐问题。

必须负责使用此指针检查的所有边界。

适用于