int vs IntPtr when you have a handle?

First a background question:

In general, what is the difference between int and IntPtr? My guess is that it is an actual object rather than a value like an int or byte is. Assuming that is true:

So they are not the same. Yet I see handles represented as both.

  1. IntPtr: Control.Handle
  2. int (or uint): A PInvoke can be setup to return an int and it works just fine:

      [DllImport ("coredll.dll", SetLastError = true)]
    общедоступный статический extern int GetForegroundWindow ();
    частная строка GetActiveWindow ()
    {
     const int nChars = 256;
     int handle = 0;
     StringBuilder Buff = новый StringBuilder (nChars);
    
     handle = CoreDLL.GetForegroundWindow ();
    
     если (CoreDLL.GetWindowText (дескриптор, Buff, nChars)> 0)
     {
     вернуть Buff.ToString ();
     }
    
     возвращение "";
    }
    

Итак, int vs IntPtr ? Имеет ли значение для ручек? Можете ли вы использовать то или иное?

14
задан Community 23 December 2015 в 23:39
поделиться