IntPtr.Zero Field


A read-only field that represents a pointer or handle that has been initialized to zero.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared ReadOnly Zero As IntPtr

Field Value

Type: System.IntPtr

The value of this field is not equivalent to null. Use this field to efficiently determine whether an instance of IntPtr has been set to a value other than zero.

For example, assume the variable, ip, is an instance of IntPtr. You can determine if it has been set by comparing it to the value returned by a constructor, for example: " if ip != new IntPtr(0)... ". However, invoking a constructor to get an unintialized pointer is inefficient. It is better to code either " if ip != IntPtr.Zero... ", or " if !IntPtr.Zero.Equals(ip)... ".

When calling the Windows API from managed code, you can pass IntPtr.Zero instead of null if an argument is expected to be either a pointer or a null. For example, the following call to the Windows CreateFile function supplies IntPtr.Zero for the pSecurityAttributes and hTemplateFile argument values.

Imports Microsoft.Win32.SafeHandles
Imports System
Imports System.Runtime.InteropServices

Module Example
   Private Const GENERIC_READ As UInteger = 2147483648
   Private Const OPEN_EXISTING As UInteger = 3 
   Private Const FILE_ATTRIBUTE_NORMAL As UInteger = 128
   Private Const FILE_FLAG_OVERLAPPED As UInteger = &h40000000

   Private Declare Auto Function CreateFile Lib "Kernel32" Alias "CreateFileW" (
            lpFileName As String, dwDesiredAccess As UInt32, 
            dwShareMode As UInt32, pSecurityAttributes As IntPtr, 
            dwCreationDisposition As UInt32, dwFlagsAndAttributes As UInt32, 
            hTemplateFile As IntPtr) As SafeFileHandle

   Public Sub Main()
      Dim hnd As SafeFileHandle = CreateFile("CallOfTheWild.txt", GENERIC_READ, 0, 
                                             IntPtr.Zero, OPEN_EXISTING,
                                             FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, 
      If hnd.IsInvalid Then
         Dim ex As Exception = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())
         Console.WriteLine("Attempt to open file failed:")
         Console.WriteLine("  {0}", ex.Message)
         Console.WriteLine("File successfully opened.")
      End If
   End Sub
End Module
' 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)

Although Zero is equivalent to null for Windows API functions with parameters or return values that can be either pointers or null, Zero is not equivalent to null. Passing null to the IntPtr.Zero.Equals method always returns false.

You can also test for a null return value from Windows API function calls that return either a pointer or a null by comparing the returned value with IntPtr.Zero. For example, the call to the GetWindow function in the following example tries to retrieve the handle of a non-existent window. If it were called from unmanaged code, the function would return null, but when it is called from managed code, it returns IntPtr.Zero.

Module Example
   Private Const GW_OWNER As Integer = 4

   Private Declare Function GetWindow Lib "user32" (hWnd As IntPtr, 
                            wFlag As Integer) As IntPtr 

   Public Sub Main()
      Dim hwnd = new IntPtr(3)
      Dim hOwner As IntPtr = GetWindow(hwnd, GW_OWNER)
      If hOwner = IntPtr.Zero Then
         Console.WriteLine("Window not found.")
      End If   
   End Sub
End Module
' The example displays the following output:
'       Window not found.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top