VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTHandleGetStandard-win.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 22 months ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: RTHandleGetStandard-win.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - RTHandleGetStandard, Windows.
4 */
5
6/*
7 * Copyright (C) 2010-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/handle.h>
43
44#include <iprt/file.h>
45#include <iprt/pipe.h>
46#include <iprt/assert.h>
47#include <iprt/errcore.h>
48#include <iprt/log.h>
49
50#include <iprt/win/windows.h>
51
52#include "internal/socket.h" /* (Needs Windows.h.) */
53
54
55RTDECL(int) RTHandleGetStandard(RTHANDLESTD enmStdHandle, bool fLeaveOpen, PRTHANDLE ph)
56{
57 /*
58 * Validate and convert input.
59 */
60 AssertPtrReturn(ph, VERR_INVALID_POINTER);
61 DWORD dwStdHandle;
62 switch (enmStdHandle)
63 {
64 case RTHANDLESTD_INPUT: dwStdHandle = STD_INPUT_HANDLE; break;
65 case RTHANDLESTD_OUTPUT: dwStdHandle = STD_OUTPUT_HANDLE; break;
66 case RTHANDLESTD_ERROR: dwStdHandle = STD_ERROR_HANDLE; break;
67 default:
68 AssertFailedReturn(VERR_INVALID_PARAMETER);
69 }
70
71 /*
72 * Is the requested descriptor valid and which IPRT handle type does it
73 * best map on to?
74 */
75 HANDLE hNative = GetStdHandle(dwStdHandle);
76 if (hNative == INVALID_HANDLE_VALUE)
77 return RTErrConvertFromWin32(GetLastError());
78
79 DWORD dwInfo;
80 if (!GetHandleInformation(hNative, &dwInfo))
81 return RTErrConvertFromWin32(GetLastError());
82 bool const fInherit = RT_BOOL(dwInfo & HANDLE_FLAG_INHERIT);
83
84 RTHANDLE h;
85 DWORD dwType = GetFileType(hNative);
86 switch (dwType & ~FILE_TYPE_REMOTE)
87 {
88 default:
89 case FILE_TYPE_UNKNOWN:
90 case FILE_TYPE_CHAR:
91 case FILE_TYPE_DISK:
92 h.enmType = RTHANDLETYPE_FILE;
93 break;
94
95 case FILE_TYPE_PIPE:
96 {
97 DWORD cMaxInstances;
98 DWORD fInfo;
99 if (!GetNamedPipeInfo(hNative, &fInfo, NULL, NULL, &cMaxInstances))
100 h.enmType = RTHANDLETYPE_SOCKET;
101 else
102 h.enmType = RTHANDLETYPE_PIPE;
103 break;
104 }
105 }
106
107 /*
108 * Create the IPRT handle.
109 */
110 int rc;
111 switch (h.enmType)
112 {
113 case RTHANDLETYPE_FILE:
114 /** @todo fLeaveOpen */
115 rc = RTFileFromNative(&h.u.hFile, (RTHCUINTPTR)hNative);
116 break;
117
118 case RTHANDLETYPE_PIPE:
119 rc = RTPipeFromNative(&h.u.hPipe, (RTHCUINTPTR)hNative,
120 (enmStdHandle == RTHANDLESTD_INPUT ? RTPIPE_N_READ : RTPIPE_N_WRITE)
121 | (fInherit ? RTPIPE_N_INHERIT : 0)
122 | (fLeaveOpen ? RTPIPE_N_LEAVE_OPEN : 0));
123 break;
124
125 case RTHANDLETYPE_SOCKET:
126 rc = rtSocketCreateForNative(&h.u.hSocket, (RTHCUINTPTR)hNative, fLeaveOpen);
127 break;
128
129 default: /* shut up gcc */
130 return VERR_INTERNAL_ERROR;
131 }
132
133 if (RT_SUCCESS(rc))
134 *ph = h;
135
136 return rc;
137}
138
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use