Red Gate forums :: View topic - IntPtrs vs HandelRefs vs SafeHandles
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net forum

IntPtrs vs HandelRefs vs SafeHandles

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
Pete R

Joined: 18 Oct 2007
Posts: 1

PostPosted: Thu Oct 18, 2007 7:12 pm    Post subject: IntPtrs vs HandelRefs vs SafeHandles Reply with quote

Hi guys, I appreciate all the work done on this site, though things are still far from clear for me. It frankly boggles my mind that Microsoft doesn't thoroughly and officially document the correct P/Invoke signatures for the Win32 API, but so it goes. This appears to be the most definitive source(?) for the signatures, but I see some signature overloads using IntPtrs and others using HandleRefs and I'm not sure why there isn't consistency. And isn't it best to use SafeHandles if possible? For example the signature for OpenDesktop given here returns an IntPtr for the desktop handle. Wouldn't something like this be preferred:

class SafeDesktopHandle : SafeHandle
        private SafeDesktopHandle()
            : base(IntPtr.Zero, true)

        public override bool IsInvalid
            get { return handle == IntPtr.Zero; }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool CloseDesktop(IntPtr handle);

        protected override bool ReleaseHandle()
            return CloseDesktop(handle);

    [DllImport("user32.dll", SetLastError = true)]
    static extern SafeDesktopHandle OpenDesktop(string lpszDesktop, int dwFlags, bool fInherit, int dwDesiredAccess);


I use a lot of windows calls like EnumWindows and related, and I'm not sure whether to try to use HandleRefs, or create a SafeWindowHandle and try to figure when it should be used and when it shouldn't, etc, or use HandleRefs, or just toss GC.KeepAlives in every time I make an Interop call. Where can I find guidance on this? Thanks,

Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group