When accessing the winapi method CloseHandle() via .net P/Invoke, should the argument be IntPtr or HandleRef, and why?
When accessing the winapi method CloseHandle() via .net P/Invoke, should the argument be IntPtr
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s really your choice, depending on how you obtain the handles. If the handles are originally embedded in some managed object, you should use HandleRef. If you got the handles from other PInvoke functions, and .NET “knows” nothing about them, use IntPtr.
The advantage of HandleRef is that .NET promises to keep the container object alive as long as the HandleRef exists, but then marshals only the handle to the API function. In the specific case of CloseHandle, this doesn’t really matter, so IntPtr should be fine.