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 //! \param[in] library The library to search in. | |
31 //! \param[in] function The function to search for. If a leading `::` is | |
32 //! present, it will be stripped. | |
33 //! \param[in] required If `true`, require the function to resolve by `DCHECK`. | |
34 //! | |
35 //! \return A pointer to the requested function on success. If \a required is | |
36 //! `true`, triggers a `DCHECK` assertion on failure, otherwise, `nullptr` | |
37 //! on failure. | |
38 FARPROC GetFunctionInternal( | |
39 const wchar_t* library, const char* function, bool required); | |
40 | |
41 //! \copydoc GetFunctionInternal | |
42 template <typename FunctionType> | |
43 FunctionType* GetFunction( | |
44 const wchar_t* library, const char* function, bool required) { | |
45 return reinterpret_cast<FunctionType*>( | |
46 internal::GetFunctionInternal(library, function, required)); | |
47 } | |
48 | |
49 } // namespace internal | |
50 } // namespace crashpad | |
51 | |
52 //! \brief Returns a function pointer to a named function in a library without | |
53 //! requiring that it be found. | |
54 //! | |
55 //! If the library or function cannot be found, this will return `nullptr`. This | |
56 //! macro is intended to be used to access functions that may not be available | |
57 //! at runtime. | |
58 //! | |
59 //! This macro returns a properly-typed function pointer. It is expected to be | |
60 //! used in this way: | |
61 //! \code | |
62 //! static const auto get_named_pipe_client_process_id = | |
63 //! GET_FUNCTION(L"kernel32.dll", ::GetNamedPipeClientProcessId); | |
64 //! if (get_named_pipe_client_process_id) { | |
65 //! BOOL rv = get_named_pipe_client_process_id(pipe, &client_process_id); | |
66 //! } | |
67 //! \endcode | |
scottmg
2015/10/19 17:28:06
Just note here that they use ::LoadLibrary(), as i
| |
68 //! | |
69 //! \param[in] library The library to search in. | |
70 //! \param[in] function The function to search for. A leading `::` is | |
71 //! recommended when a wrapper function of the same name is present. | |
72 //! | |
73 //! \return A pointer to the requested function on success, or `nullptr` on | |
74 //! failure. | |
75 //! | |
76 //! \sa GET_FUNCTION_REQUIRED | |
77 #define GET_FUNCTION(library, function) \ | |
78 crashpad::internal::GetFunction<decltype(function)>( \ | |
79 library, #function, false) | |
80 | |
81 //! \brief Returns a function pointer to a named function in a library, | |
82 //! requiring that it be found. | |
83 //! | |
84 //! If the library or function cannot be found, this will trigger a `DCHECK` | |
85 //! assertion. This macro is intended to be used to access functions that are | |
86 //! always expected to be available at runtime but which are not present in any | |
87 //! import library. | |
88 //! | |
89 //! This macro returns a properly-typed function pointer. It is expected to be | |
90 //! used in this way: | |
91 //! \code | |
92 //! static const auto nt_query_object = | |
93 //! GET_FUNCTION_REQUIRED(L"ntdll.dll", ::NtQueryObject); | |
94 //! NTSTATUS status = | |
95 //! nt_query_object(handle, type, &info, info_length, &return_length); | |
96 //! \endcode | |
97 //! | |
98 //! \param[in] library The library to search in. | |
99 //! \param[in] function The function to search for. A leading `::` is | |
100 //! recommended when a wrapper function of the same name is present. | |
101 //! | |
102 //! \return A pointer to the requested function. | |
103 //! | |
104 //! \sa GET_FUNCTION | |
105 #define GET_FUNCTION_REQUIRED(library, function) \ | |
106 crashpad::internal::GetFunction<decltype(function)>( \ | |
107 library, #function, true) | |
108 | |
109 #endif // CRASHPAD_UTIL_WIN_GET_FUNCTION_H_ | |
OLD | NEW |