13
Answers

HandleRef or IntPtr. When and Why?

Libor Theimer

Libor Theimer

10y
2.8k
1
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)