OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_WIN_GET_FUNCTION_H_ |
| 16 #define CRASHPAD_UTIL_WIN_GET_FUNCTION_H_ |
| 17 |
| 18 #include <windows.h> |
| 19 |
| 20 //! \file |
| 21 |
| 22 namespace crashpad { |
| 23 namespace internal { |
| 24 |
| 25 //! \brief Returns a function pointer to a named function in a library. |
| 26 //! |
| 27 //! Do not call this directly, use the GET_FUNCTION() or GET_FUNCTION_REQUIRED() |
| 28 //! macros instead. |
| 29 //! |
| 30 //! This accesses \a library by calling `LoadLibrary()` and is subject to the |
| 31 //! same restrictions as that function. Notably, it can’t be used from a |
| 32 //! `DllMain()` entry point. |
| 33 //! |
| 34 //! \param[in] library The library to search in. |
| 35 //! \param[in] function The function to search for. If a leading `::` is |
| 36 //! present, it will be stripped. |
| 37 //! \param[in] required If `true`, require the function to resolve by `DCHECK`. |
| 38 //! |
| 39 //! \return A pointer to the requested function on success. If \a required is |
| 40 //! `true`, triggers a `DCHECK` assertion on failure, otherwise, `nullptr` |
| 41 //! on failure. |
| 42 FARPROC GetFunctionInternal( |
| 43 const wchar_t* library, const char* function, bool required); |
| 44 |
| 45 //! \copydoc GetFunctionInternal |
| 46 template <typename FunctionType> |
| 47 FunctionType* GetFunction( |
| 48 const wchar_t* library, const char* function, bool required) { |
| 49 return reinterpret_cast<FunctionType*>( |
| 50 internal::GetFunctionInternal(library, function, required)); |
| 51 } |
| 52 |
| 53 } // namespace internal |
| 54 } // namespace crashpad |
| 55 |
| 56 //! \brief Returns a function pointer to a named function in a library without |
| 57 //! requiring that it be found. |
| 58 //! |
| 59 //! If the library or function cannot be found, this will return `nullptr`. This |
| 60 //! macro is intended to be used to access functions that may not be available |
| 61 //! at runtime. |
| 62 //! |
| 63 //! This macro returns a properly-typed function pointer. It is expected to be |
| 64 //! used in this way: |
| 65 //! \code |
| 66 //! static const auto get_named_pipe_client_process_id = |
| 67 //! GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId); |
| 68 //! if (get_named_pipe_client_process_id) { |
| 69 //! BOOL rv = get_named_pipe_client_process_id(pipe, &client_process_id); |
| 70 //! } |
| 71 //! \endcode |
| 72 //! |
| 73 //! This accesses \a library by calling `LoadLibrary()` and is subject to the |
| 74 //! same restrictions as that function. Notably, it can’t be used from a |
| 75 //! `DllMain()` entry point. |
| 76 //! |
| 77 //! \param[in] library The library to search in. |
| 78 //! \param[in] function The function to search for. A leading `::` is |
| 79 //! recommended when a wrapper function of the same name is present. |
| 80 //! |
| 81 //! \return A pointer to the requested function on success, or `nullptr` on |
| 82 //! failure. |
| 83 //! |
| 84 //! \sa GET_FUNCTION_REQUIRED |
| 85 #define GET_FUNCTION(library, function) \ |
| 86 crashpad::internal::GetFunction<decltype(function)>( \ |
| 87 library, #function, false) |
| 88 |
| 89 //! \brief Returns a function pointer to a named function in a library, |
| 90 //! requiring that it be found. |
| 91 //! |
| 92 //! If the library or function cannot be found, this will trigger a `DCHECK` |
| 93 //! assertion. This macro is intended to be used to access functions that are |
| 94 //! always expected to be available at runtime but which are not present in any |
| 95 //! import library. |
| 96 //! |
| 97 //! This macro returns a properly-typed function pointer. It is expected to be |
| 98 //! used in this way: |
| 99 //! \code |
| 100 //! static const auto nt_query_object = |
| 101 //! GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtQueryObject); |
| 102 //! NTSTATUS status = |
| 103 //! nt_query_object(handle, type, &info, info_length, &return_length); |
| 104 //! \endcode |
| 105 //! |
| 106 //! This accesses \a library by calling `LoadLibrary()` and is subject to the |
| 107 //! same restrictions as that function. Notably, it can’t be used from a |
| 108 //! `DllMain()` entry point. |
| 109 //! |
| 110 //! \param[in] library The library to search in. |
| 111 //! \param[in] function The function to search for. A leading `::` is |
| 112 //! recommended when a wrapper function of the same name is present. |
| 113 //! |
| 114 //! \return A pointer to the requested function. |
| 115 //! |
| 116 //! \sa GET_FUNCTION |
| 117 #define GET_FUNCTION_REQUIRED(library, function) \ |
| 118 crashpad::internal::GetFunction<decltype(function)>( \ |
| 119 library, #function, true) |
| 120 |
| 121 #endif // CRASHPAD_UTIL_WIN_GET_FUNCTION_H_ |
OLD | NEW |