Nota: este método es nuevo en la versión 2.0 de .NET Framework.
Libera un puntero a una cadena no administrada que se ha asignado con el método
SecureStringToGlobalAllocAnsi.
Espacio de nombres: System.Runtime.InteropServices
Ensamblado: mscorlib (en mscorlib.dll)

Sintaxis
Visual Basic (Declaración)
Public Shared Sub ZeroFreeGlobalAllocAnsi ( _
s As IntPtr _
)
Dim s As IntPtr
Marshal.ZeroFreeGlobalAllocAnsi(s)
public static void ZeroFreeGlobalAllocAnsi (
IntPtr s
)
public:
static void ZeroFreeGlobalAllocAnsi (
IntPtr s
)
public static void ZeroFreeGlobalAllocAnsi (
IntPtr s
)
public static function ZeroFreeGlobalAllocAnsi (
s : IntPtr
)
Parámetros
- s
La dirección de la cadena no administrada que se va a liberar.

Comentarios
El método ZeroFreeGlobalAllocAnsi primero hace que el resultado sea cero y luego libera memoria no administrada que se ha asignado con el método SecureStringToGlobalAllocAnsi.

Ejemplo
En el siguiente ejemplo de código se utiliza el método SecureStringToGlobalAllocAnsi para calcular referencias y descifrar el contenido de un objeto SecureString en un bloque de memoria no administrada. A continuación se utiliza el método ZeroFreeGlobalAllocAnsi para hacer que el resultado sea cero y desechar el bloque no administrado.
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Security.Principal
Imports System.Text
Module MarshalExample
Sub Main(ByVal args() As String)
Dim unmanagedRef As IntPtr
Try
' Ask the user for a password.
Console.Write("Please enter your password:")
Dim passWord As SecureString = GetPassword()
Console.WriteLine("Copying and decrypting the string to unmanaged memory...")
' Copy the Secure string to unmanaged memory (and decrypt it).
unmanagedRef = Marshal.SecureStringToGlobalAllocAnsi(passWord)
Catch e As Exception
Console.WriteLine(e.Message)
Finally
If unmanagedRef <> IntPtr.Zero Then
Console.WriteLine("Zeroing out unmanaged memory...")
Marshal.ZeroFreeGlobalAllocAnsi(unmanagedRef)
End If
End Try
Console.WriteLine("Done.")
Console.ReadLine()
End Sub
Function GetPassword() As SecureString
Dim password As New SecureString()
' get the first character of the password
Dim nextKey As ConsoleKeyInfo = Console.ReadKey(True)
While nextKey.Key <> ConsoleKey.Enter
If nextKey.Key = ConsoleKey.BackSpace Then
If password.Length > 0 Then
password.RemoveAt(password.Length - 1)
' erase the last * as well
Console.Write(nextKey.KeyChar)
Console.Write(" ")
Console.Write(nextKey.KeyChar)
End If
Else
password.AppendChar(nextKey.KeyChar)
Console.Write("*")
End If
nextKey = Console.ReadKey(True)
End While
Console.WriteLine()
' lock the password down
password.MakeReadOnly()
Return password
End Function
End Module
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Text;
namespace SecureStringExample
{
class MarshalExample
{
static void Main(string[] args)
{
IntPtr unmanagedRef = IntPtr.Zero;
try
{
// Ask the user for a password.
Console.Write("Please enter your password:");
SecureString passWord = GetPassword();
Console.WriteLine("Copying and decrypting the string to unmanaged memory...");
// Copy the Secure string to unmanaged memory (and decrypt it).
unmanagedRef = Marshal.SecureStringToGlobalAllocAnsi(passWord);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (unmanagedRef != IntPtr.Zero)
{
Console.WriteLine("Zeroing out unmanaged memory...");
Marshal.ZeroFreeGlobalAllocAnsi(unmanagedRef);
}
}
Console.WriteLine("Done.");
Console.ReadLine();
}
public static SecureString GetPassword()
{
SecureString password = new SecureString();
// get the first character of the password
ConsoleKeyInfo nextKey = Console.ReadKey(true);
while (nextKey.Key != ConsoleKey.Enter)
{
if (nextKey.Key == ConsoleKey.Backspace)
{
if (password.Length > 0)
{
password.RemoveAt(password.Length - 1);
// erase the last * as well
Console.Write(nextKey.KeyChar);
Console.Write(" ");
Console.Write(nextKey.KeyChar);
}
}
else
{
password.AppendChar(nextKey.KeyChar);
Console.Write("*");
}
nextKey = Console.ReadKey(true);
}
Console.WriteLine();
// lock the password down
password.MakeReadOnly();
return password;
}
}
}

Seguridad de .NET Framework

Plataformas
Windows 98, Windows 2000 SP4, Windows Millennium, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

Información de versión
.NET Framework
Compatible con: 2.0

Vea también