Libor Theimer

Libor Theimer

  • NA
  • 32
  • 7.3k

HandleRef or IntPtr. When and Why?

Jun 11 2014 7:51 PM
In what cases is it better to use HandleRef instead IntPtr in PInvoke. Is there something like best practices ? I need use it in Win32 functions like GetWindow, GetWindowText, SetWindowText etc. User can close any window anytime so hwnd in not valid anymore.
 
For example:
1. get hwnd by function GetWindow  for Notepad window
2. user close Notepad window so my hwnd is invalid
3. using this hwnd in SetWindowText will crashed
 
Is it possible solve this problem by HandleRef? I think no, but I am beginner in .NET.
 
 Thanks to all :o)
 
 

Answers (13)