c# - Which is better/safer to use: HandleRef or IntPtr (newer source code from Microsoft no longer uses HandleRef) -


for example, in old .net framework 2.0 source code (windows forms, visual studio 2005 - whidbey), getclientrect function defined using handleref:

    [dllimport(externdll.user32, exactspelling=true, charset=charset.auto)]     public static extern bool getclientrect(handleref hwnd, [in, out] ref nativemethods.rect rect);  

in new windows api code pack (from microsoft, 2009/2010) same function defined using intptr:

    [dllimport("user32.dll")]     [return: marshalas(unmanagedtype.bool)]     internal static extern bool getclientrect(intptr hwnd, ref corenativemethods.rect rect); 

actually handleref not used in of windows api code pack source files, while heavily used in native methods signatures in old .net framework source files.

it's bit fishy. handleref not needed when handle values stored in safehandle derived object. code pack declares, zeroinvalidhandle, several derived ones safewindowhandle.

however, doesn't use of these safehandle classes anywhere. not sure if has to, lot of vista , win7 extensions com interfaces. not traditional handle based c api. kept alive through reference counts , not subject kind of garbage collector mishap.

personally never worry this. getting class object collected while api call executing bug. can happen microsecond after api call completed. still bug, not 1 makes api call fail. not sure i'd want not fail, i'd prefer exception when got bug in code. microsoft needs protect this, don't want blame exception. do.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -