Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

closehandle (kernel32)
 
.
Summary

C# Signature:

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

VB .NET Signature:

<DllImport("kernel32.dll", SetLastError:=True)> _
<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)> _
<SuppressUnmanagedCodeSecurity> _
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

Boo Signature:

[DllImport("kernel32", SetLastError : true)]
def CloseHandle(hObject as IntPtr) as bool:
     pass

User-Defined Types:

None.

Notes:

For .NET 2.0, consider using Microsoft.Win32.SafeHandles.SafeFileHandle instead. It can be used where IntPtr is used. If you use a SafeFileHandle, do not call CloseHandle as the CLR will close it for you (even if it's already closed).

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.

I understand the conversion between 1 to 4 bytes for the different types of bools. However, isn't [MarshalAs(UnmanagedType.Bool)] the default for marshalling a .NET bool? Therefore making it unnecessary to specify any marshal on the return value. Please let me know if I'm wrong about this.

Tips & Tricks:

Please add some more!

Unsafe Code refers to C# code with Pointers in it.

"The use of pointers is rarely required in C#, but there are some situations that require them. Using an 'unsafe' context to allow pointers is warranted [... for use of] Advanced COM or Platform Invoke scenarios that involve structures with pointers in them" e.g.

[DllImport("kernel32", SetLastError=true)]

      static extern unsafe bool CloseHandle(
        IntPtr hObject   // handle to object
        );

http://msdn.microsoft.com/en-us/library/aa288474(v=VS.71).aspx

"You cannot return the same exact pointer from a native function that you took as a parameter. If a native function returns the pointer that has been marshaled to it by PInvoke[c or c++ or DllImport in c#], memory corruption and exceptions may ensue." http://msdn.microsoft.com/en-us/library/ms235282.aspx

Unsafe code C# language specification: http://msdn.microsoft.com/en-us/library/aa664769(v=vs.71).aspx

In order for C# to compile unsafe code, the application must be compiled with /unsafe. http://msdn.microsoft.com/en-us/library/ct597kb0.aspx

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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions