Marshal.StringToHGlobalAnsi(String) 方法
定义
public:
static IntPtr StringToHGlobalAnsi(System::String ^ s);
[System.Security.SecurityCritical]
public static IntPtr StringToHGlobalAnsi (string s);
public static IntPtr StringToHGlobalAnsi (string? s);
public static IntPtr StringToHGlobalAnsi (string s);
[<System.Security.SecurityCritical>]
static member StringToHGlobalAnsi : string -> nativeint
static member StringToHGlobalAnsi : string -> nativeint
Public Shared Function StringToHGlobalAnsi (s As String) As IntPtr
参数
- s
- String
要复制的托管字符串。A managed string to be copied.
返回
非托管内存中将 s 复制到其中的地址;如果 s 为 null,则为 0。The address, in unmanaged memory, to where s was copied, or 0 if s is null.
- 属性
例外
没有足够的可用内存。There is insufficient memory available.
s 参数超过了操作系统所允许的最大长度。The s parameter exceeds the maximum length allowed by the operating system.
示例
下面的示例演示如何将托管类的内容转换 String 为非托管内存,并在完成后释放非托管内存。The following example demonstrates how to convert the contents of a managed String class to unmanaged memory and then dispose of the unmanaged memory when done.
using namespace System;
using namespace System::Runtime::InteropServices;
#include <iostream> // for printf
int main()
{
// Create a managed string.
String^ managedString = "Hello unmanaged world (from the managed world).";
// Marshal the managed string to unmanaged memory.
char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();
printf("stringPointer = %s\n", stringPointer);
// Always free the unmanaged string.
Marshal::FreeHGlobal(IntPtr(stringPointer));
return 0;
}
using System;
using System.Runtime.InteropServices;
class MainFunction
{
static void Main()
{
Console.WriteLine("\nStringToGlobalAnsi\n");
// Create a managed string.
String managedString = "I am a managed String";
Console.WriteLine("1) managedString = " + managedString );
// Marshal the managed string to unmanaged memory.
IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
Console.WriteLine("2) stringPointer = {0}", stringPointer );
// Get the string back from unmanaged memory
String RetrievedString = Marshal.PtrToStringAnsi( stringPointer);
Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString );
// Always free the unmanaged string.
Marshal.FreeHGlobal(stringPointer);
// IntPtr handle value is still the same:
Console.WriteLine("4) stringPointer = " + stringPointer );
// However, it contains no data after being freed:
String RetrievedString2 = Marshal.PtrToStringAnsi( stringPointer);
Console.WriteLine("5) RetrievedString2 = " + RetrievedString2 );
}
}
注解
StringToHGlobalAnsi 对于自定义封送或混合托管和非托管代码很有用。StringToHGlobalAnsi is useful for custom marshaling or when mixing managed and unmanaged code. 由于此方法会分配字符串所需的非托管内存,因此请始终通过调用来释放内存 FreeHGlobal 。Because this method allocates the unmanaged memory required for a string, always free the memory by calling FreeHGlobal. StringToHGlobalAnsi 提供的相反功能 Marshal.PtrToStringAnsi 。StringToHGlobalAnsi provides the opposite functionality of Marshal.PtrToStringAnsi.
此方法复制嵌入的 null 字符,并包含一个终止 null 字符。This method copies embedded null characters, and includes a terminating null character.