Marshal.PtrToStringAuto 方法

定义

分配托管 String 字符串,并将非托管字符串的所有或部分复制到其中。

重载

名称 说明
PtrToStringAuto(IntPtr)

分配托管 String 字符,并将存储在非托管内存中的字符串中的所有字符复制到其中,并将所有字符复制到其中。

PtrToStringAuto(IntPtr, Int32)

分配托管 String 字符,并将存储在非托管内存中的字符串中的指定字符数复制到其中。

PtrToStringAuto(IntPtr)

Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs

分配托管 String 字符,并将存储在非托管内存中的字符串中的所有字符复制到其中,并将所有字符复制到其中。

public:
 static System::String ^ PtrToStringAuto(IntPtr ptr);
public static string? PtrToStringAuto(IntPtr ptr);
public static string PtrToStringAuto(IntPtr ptr);
[System.Security.SecurityCritical]
public static string PtrToStringAuto(IntPtr ptr);
static member PtrToStringAuto : nativeint -> string
[<System.Security.SecurityCritical>]
static member PtrToStringAuto : nativeint -> string
Public Shared Function PtrToStringAuto (ptr As IntPtr) As String

参数

ptr
IntPtr

nativeint

对于 Unicode 平台,第一个 Unicode 字符的地址。

-或-

对于 ANSI 平台,第一个 ANSI 字符的地址。

返回

如果参数的值不是ptr,则保存非托管字符串的副本的null托管字符串;否则,此方法返回null

属性

注解

如果当前平台为 Unicode,则会将每个 ANSI 字符扩大为 Unicode 字符,并且此方法调用 PtrToStringUni。 否则,此方法调用 PtrToStringAnsi

PtrToStringAuto 对于自定义封送处理或在混合托管和非托管代码时非常有用。 由于此方法创建非托管字符串内容的副本,因此必须根据需要释放原始字符串。 PtrToStringAuto提供与方法相反的功能Marshal.StringToCoTaskMemAutoMarshal.StringToHGlobalAuto

另请参阅

适用于

PtrToStringAuto(IntPtr, Int32)

Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs

分配托管 String 字符,并将存储在非托管内存中的字符串中的指定字符数复制到其中。

public:
 static System::String ^ PtrToStringAuto(IntPtr ptr, int len);
public static string? PtrToStringAuto(IntPtr ptr, int len);
public static string PtrToStringAuto(IntPtr ptr, int len);
[System.Security.SecurityCritical]
public static string PtrToStringAuto(IntPtr ptr, int len);
static member PtrToStringAuto : nativeint * int -> string
[<System.Security.SecurityCritical>]
static member PtrToStringAuto : nativeint * int -> string
Public Shared Function PtrToStringAuto (ptr As IntPtr, len As Integer) As String

参数

ptr
IntPtr

nativeint

对于 Unicode 平台,第一个 Unicode 字符的地址。

-或-

对于 ANSI 平台,第一个 ANSI 字符的地址。

len
Int32

要复制的字符数。

返回

一个托管字符串,如果参数的值 ptr 不是 null,则保存本机字符串的副本;否则,此方法返回 null

属性

例外

len 小于零。

注解

在 Unicode 平台上,此方法调用 PtrToStringUni;在 ANSI 平台上,调用此方法 PtrToStringAnsi。 在调用这些方法之前,不会执行任何转换。

PtrToStringAuto 对于自定义封送处理或在混合托管和非托管代码时非常有用。 由于此方法创建非托管字符串内容的副本,因此必须根据需要释放原始字符串。 PtrToStringAuto提供和 Marshal.StringToCoTaskMemAuto. 的Marshal.StringToHGlobalAuto相反功能。

另请参阅

适用于