1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SANDBOX_SANDBOX_POC_POCDLL_UTILS_H__
6 #define SANDBOX_SANDBOX_POC_POCDLL_UTILS_H__
10 #include "base/basictypes.h"
12 // Class to convert a HANDLE to a FILE *. The FILE * is closed when the
13 // object goes out of scope
20 // Note: c_file_handle_ does not need to be closed because fclose does it.
28 // Translates a HANDLE (handle) to a FILE * opened with the mode "mode".
29 // The return value is the FILE * or NULL if there is an error.
30 FILE* Translate(HANDLE handle
, const char *mode
) {
36 BOOL result
= ::DuplicateHandle(::GetCurrentProcess(),
38 ::GetCurrentProcess(),
40 0, // Don't ask for a specific
42 FALSE
, // Not inheritable.
43 DUPLICATE_SAME_ACCESS
);
49 int c_file_handle
= _open_osfhandle(reinterpret_cast<LONG_PTR
>(new_handle
),
51 if (-1 == c_file_handle
) {
55 file_
= _fdopen(c_file_handle
, mode
);
59 // the FILE* returned. We need to closed it at the end.
62 DISALLOW_COPY_AND_ASSIGN(HandleToFile
);
65 #endif // SANDBOX_SANDBOX_POC_POCDLL_UTILS_H__