CloseHandle (kernel32)
Last changed: -186.136.223.176

.
Summary

C# Signature:

[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);

VB .NET Signature:

<DllImport("kernel32.dll", SetLastError:=True)> _

Public Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

End Function

Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal hObject As IntPtr) As Boolean
Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer

User-Defined Types:

None.

Notes:

For .NET 2.0, consider using Microsoft.Win32.SafeHandles.SafeFileHandle instead. It can be used where IntPtr is used.

Can someone explain why "[return: MarshalAs(UnmanagedType.Bool)]" is part of the signature?

External values of type 'bool' could represent a 1-byte (e.g. C++ bool), 2-byte (e.g. COM VARIANT_BOOL) or 4-byte (e.g. Windows BOOL) value. The distinction is less important for parameters, but for the return, the value would be stored in either (on x86) AL (1 byte), AX (2 bytes) or EAX (4 bytes). By telling interop of the native return type, it knows which parts of EAX to interpret.

Tips & Tricks:

Please add some!

Sample Code:

hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 4096, "mIRC")
CloseHandle(hMapFile)

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
CloseHandle on MSDN