IntPtr.Zero IntPtr.Zero IntPtr.Zero IntPtr.Zero Field

定義

0 に初期化されたポインターまたはハンドルを表す読み取り専用フィールド。 A read-only field that represents a pointer or handle that has been initialized to zero.

public: static initonly IntPtr Zero;
public static readonly IntPtr Zero;
 staticval mutable Zero : nativeint
Public Shared ReadOnly Zero As IntPtr 

フィールド値

注釈

このフィールドの値が等しくないnullします。The value of this field is not equivalent to null. このフィールドのインスタンスかどうかを効率的に判断を使用してIntPtr0 以外の値に設定されています。Use this field to efficiently determine whether an instance of IntPtr has been set to a value other than zero.

たとえば、ip では、変数のインスタンスであるIntPtrします。For example, assume the variable, ip, is an instance of IntPtr. たとえば、コンス トラクターによって返される値と比較して設定されているかどうかを指定できます:" if ip != new IntPtr(0)... "。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. いずれかをコーディングすることをお勧め" if ip != IntPtr.Zero... "、または" if !IntPtr.Zero.Equals(ip)... "。It is better to code either " if ip != IntPtr.Zero... ", or " if !IntPtr.Zero.Equals(ip)... ".

マネージ コードから Windows API を呼び出すときに渡すことができますIntPtr.Zeroの代わりにnull引数のいずれかのポインターであるが予想される場合、またはnullします。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. Windows の次の呼び出しなどCreateFile関数装置IntPtr.ZeropSecurityAttributeshTemplateFile引数の値。For example, the following call to the Windows CreateFile function supplies IntPtr.Zero for the pSecurityAttributes and hTemplateFile argument values.

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)
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, 
                                             IntPtr.Zero)
      If hnd.IsInvalid Then
         Dim ex As Exception = 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()     
      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)

注意

Zeroと等価nullパラメーターまたはいずれかのポインターであることが戻り値を持つ Windows API 関数またはnullZeroと等価でないnullします。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. 渡すnullIntPtr.Zero.Equalsメソッドは常に返しますfalseします。Passing null to the IntPtr.Zero.Equals method always returns false.

テストすることも、nullをいずれかにポインターを返す Windows API 関数呼び出しからの戻り値またはnullで返される値を比較することによってIntPtr.Zeroします。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. 呼び出しなど、GetWindowで次の例では、関数が存在しないウィンドウのハンドルの取得を試みます。For example, the call to the GetWindow function in the following example tries to retrieve the handle of a non-existent window. かどうかは、アンマネージ コードから呼び出された、関数が返すはnull、マネージ コードからが呼び出されるを返しますが、IntPtr.Zeroします。If it were called from unmanaged code, the function would return null, but when it is called from managed code, it returns 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.
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.

適用対象