You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/c-runtime-library/reference/get-osfhandle.md
+8-2
Original file line number
Diff line number
Diff line change
@@ -27,11 +27,16 @@ An existing file descriptor.
27
27
28
28
## Return Value
29
29
30
-
Returns an operating-system file handle if *fd* is valid. Otherwise, the invalid parameter handler is invoked, as described in [Parameter Validation](../../c-runtime-library/parameter-validation.md). 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.
30
+
Returns an operating-system file handle if *fd* is valid. Otherwise, the invalid parameter handler is invoked, as described in [Parameter Validation](../../c-runtime-library/parameter-validation.md). If execution is allowed to continue, it returns **INVALID_HANDLE_VALUE** (-1). It also sets **errno** to **EBADF**, indicating an invalid file handle. To avoid a warning when the result is used as a Win32 file handle, cast it to a **HANDLE** type.
31
+
32
+
> [!NOTE]
33
+
> When **stdin**, **stdout**, and **stderr** aren't associated with a stream (for example, in a Windows application without a console window), the file descriptor values for these streams are returned from [_fileno](fileno.md) as the special value -2. Similarly, if you use a 0, 1, or 2 as the file descriptor parameter instead of the result of a call to **_fileno**, **_get_osfhandle** also returns the special value -2 when the file descriptor is not associated with a stream, and does not set **errno**. However, this is not a valid file handle value, and subsequent calls that attempt to use it are likely to fail.
34
+
35
+
For more information about **EBADF** and other error codes, see [_doserrno, errno, _sys_errlist, and _sys_nerr](../../c-runtime-library/errno-doserrno-sys-errlist-and-sys-nerr.md).
31
36
32
37
## Remarks
33
38
34
-
To close a file whose operating system (OS) file handle is obtained by **_get_osfhandle**, call [_close](close.md) 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](close.md) is called on *fd*. If the file descriptor is owned by a `FILE *` stream, then calling [fclose](fclose-fcloseall.md) on that `FILE *` stream closes both the file descriptor and the underlying OS file handle. In this case, do not call [_close](close.md) on the file descriptor.
39
+
To close a file whose operating system (OS) file handle is obtained by **_get_osfhandle**, call [_close](close.md) on the file descriptor *fd*. Never 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](close.md) is called on *fd*. If the file descriptor is owned by a `FILE *` stream, then calling [fclose](fclose-fcloseall.md) on that `FILE *` stream closes both the file descriptor and the underlying OS file handle. In this case, don't call [_close](close.md) on the file descriptor.
35
40
36
41
## Requirements
37
42
@@ -48,3 +53,4 @@ For more compatibility information, see [Compatibility](../../c-runtime-library/
Associates a C run-time file descriptor with an existing operating-system file handle.
13
+
Associates a C run-time file descriptor with an existing operatingsystem file handle.
14
14
15
15
## Syntax
16
16
@@ -24,7 +24,7 @@ int _open_osfhandle (
24
24
### Parameters
25
25
26
26
*osfhandle*<br/>
27
-
Operating-system file handle.
27
+
Operatingsystem file handle.
28
28
29
29
*flags*<br/>
30
30
Types of operations allowed.
@@ -35,7 +35,7 @@ If successful, **_open_osfhandle** returns a C run-time file descriptor. Otherwi
35
35
36
36
## Remarks
37
37
38
-
The **_open_osfhandle** function allocates a C run-time file descriptor and associates it with the operating-system file handle specified by *osfhandle*. To avoid a compiler warning, cast the *osfhandle* argument from **HANDLE** to **intptr_t**. The *flags* argument is an integer expression formed from one or more of the manifest constants defined in \<fcntl.h>. When two or more manifest constants are used to form the *flags* argument, the constants are combined with the bitwise-OR operator ( **|** ).
38
+
The **_open_osfhandle** function allocates a C run-time file descriptor. It associates this file descriptor with the operatingsystem file handle specified by *osfhandle*. To avoid a compiler warning, cast the *osfhandle* argument from **HANDLE** to **intptr_t**. The *flags* argument is an integer expression formed from one or more of the manifest constants defined in \<fcntl.h>. You can use the bitwise-OR operator ( **|** ) to combine two or more manifest constants to form the *flags* argument.
39
39
40
40
These manifest constants are defined in \<fcntl.h>:
41
41
@@ -46,7 +46,7 @@ These manifest constants are defined in \<fcntl.h>:
46
46
|**\_O\_TEXT**| Opens the file in text (translated) mode. |
47
47
|**\_O\_WTEXT**| Opens the file in Unicode (translated UTF-16) mode. |
48
48
49
-
The **_open_osfhandle** call transfers ownership of the Win32 file handle to the file descriptor. To close a file opened by using **_open_osfhandle**, call [\_close](close.md). The underlying OS file handle is also closed by a call to **_close**. Don't call the Win32 function **CloseHandle** on the original handle. If the file descriptor is owned by a **FILE *** stream, then a call to [fclose](fclose-fcloseall.md)on that **FILE *** stream closes both the file descriptor and the underlying handle. In this case, don't call **_close** on the file descriptor or **CloseHandle** on the original handle.
49
+
The **_open_osfhandle** call transfers ownership of the Win32 file handle to the file descriptor. To close a file opened by using **_open_osfhandle**, call [\_close](close.md). The underlying OS file handle is also closed by a call to **_close**. Don't call the Win32 function **CloseHandle** on the original handle. If the file descriptor is owned by a **FILE *** stream, then a call to [fclose](fclose-fcloseall.md) closes both the file descriptor and the underlying handle. In this case, don't call **_close** on the file descriptor or **CloseHandle** on the original handle.
50
50
51
51
## Requirements
52
52
@@ -59,3 +59,4 @@ For more compatibility information, see [Compatibility](../../c-runtime-library/
0 commit comments