Leggere in inglese

Condividi tramite


IntPtr.Zero Campo

Definizione

Campo di sola lettura che rappresenta un intero firmato inizializzato su zero.

public static readonly IntPtr Zero;

Valore del campo

IntPtr

Commenti

Il valore di questo campo non equivale a null. Usare questo campo per determinare in modo efficiente se un'istanza di IntPtr è stata impostata su un valore diverso da zero.

Si supponga, ad esempio, che la variabile, ip, sia un'istanza di IntPtr. È possibile determinare se è stato impostato confrontandolo con il valore restituito da un costruttore, ad esempio : " if ip != new IntPtr(0)... ". Tuttavia, richiamando un costruttore per ottenere un puntatore non inizializzato è inefficiente. È meglio codificare " if ip != IntPtr.Zero... o " if !IntPtr.Zero.Equals(ip)... ".

Quando si chiama l'API di Windows dal codice gestito, è possibile passare IntPtr.Zero anziché null se si prevede che un argomento sia un puntatore o null. Ad esempio, la seguente chiamata alla funzione CreateFile di Windows fornisce IntPtr.Zero per i valori degli argomenti pSecurityAttributes e hTemplateFile.

using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

public class Example
{
   private const uint GENERIC_READ = 0x80000000;
   private const uint OPEN_EXISTING = 3;
   private const uint FILE_ATTRIBUTE_NORMAL = 128;
   private const uint FILE_FLAG_OVERLAPPED = 0x40000000;

   [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
   private static extern Microsoft.Win32.SafeHandles.SafeFileHandle CreateFile(
            string lpFileName, System.UInt32 dwDesiredAccess, System.UInt32 dwShareMode,
            IntPtr pSecurityAttributes, System.UInt32 dwCreationDisposition,
            System.UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);

   public static void Main()
   {
      SafeFileHandle hnd = CreateFile("CallOfTheWild.txt", GENERIC_READ, 0,
                                      IntPtr.Zero, OPEN_EXISTING,
                                      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                                      IntPtr.Zero);
      if (hnd.IsInvalid) {
            Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            Console.WriteLine("Attempt to open file failed:");
            Console.WriteLine("  {0}", ex.Message);
            return;
      }
      else {
         Console.WriteLine("File successfully opened.");
         hnd.Close();
      }
   }
}
// If the file cannot be found, the example displays the following output:
//    Attempt to open file failed:
//      The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

Nota

Anche se Zero è equivalente a null per le funzioni API Windows con parametri o valori restituiti che possono essere puntatori o null, Zero non è equivalente a null. Il passaggio di null al metodo IntPtr.Zero.Equals restituisce sempre false.

È inoltre possibile verificare se viene restituito un valore null dalle chiamate alle funzioni API di Windows che restituiscono un puntatore o null confrontando il valore restituito con IntPtr.Zero. Ad esempio, nell'esempio seguente la chiamata alla funzione GetWindow tenta di recuperare il riferimento di una finestra inesistente. Se la funzione è stata chiamata dal codice non gestito, restituisce null, ma quando viene chiamata dal codice gestito, restituisce IntPtr.Zero.

using System;
using System.Runtime.InteropServices;

public class Example
{
   private const int GW_OWNER = 4;

   [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
   public static extern IntPtr GetWindow(IntPtr hwnd, int wFlag);

   public static void Main()
   {
      IntPtr hwnd = new IntPtr(3);
      IntPtr hOwner = GetWindow(hwnd, GW_OWNER);
      if (hOwner == IntPtr.Zero)
         Console.WriteLine("Window not found.");
   }
}
// The example displays the following output:
//        Window not found.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0