_get_osfhandle

Retrieves the operating-system file handle that is associated with the specified file descriptor.

Syntax

intptr_t _get_osfhandle(
   int fd
);

Parameters

fd
An existing file descriptor.

Return Value

Returns an operating-system file handle if fd is valid. Otherwise, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, this function returns INVALID_HANDLE_VALUE (-1) and sets errno to EBADF, indicating an invalid file handle. To avoid a compiler warning when the result is used in routines that expect a Win32 file handle, cast it to a HANDLE type.

Remarks

To close a file whose operating system (OS) file handle is obtained by _get_osfhandle, call _close on the file descriptor fd. Do not call CloseHandle on the return value of this function. The underlying OS file handle is owned by the fd file descriptor, and is closed when _close is called on fd. If the file descriptor is owned by a FILE * stream, then calling fclose on that FILE * stream closes both the file descriptor and the underlying OS file handle. In this case, do not call _close on the file descriptor.

Requirements

Routine Required header
_get_osfhandle <io.h>

For more compatibility information, see Compatibility.

See also

File Handling
_close
_creat, _wcreat
_dup, _dup2
_open, _wopen