1 : // Copyright 2013 Google Inc. 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 : // Declares a utility class to get DLL load/unload notifications on supporting
16 : // systems - Vista and up.
17 :
18 : #ifndef SYZYGY_AGENT_COMMON_DLL_NOTIFICATIONS_H_
19 : #define SYZYGY_AGENT_COMMON_DLL_NOTIFICATIONS_H_
20 :
21 : #include <windows.h>
22 :
23 : #include "base/callback.h"
24 : #include "base/strings/string_piece.h"
25 :
26 : // Forward decl.
27 m : union _LDR_DLL_NOTIFICATION_DATA;
28 :
29 m : namespace agent {
30 m : namespace common {
31 :
32 : // A wrapper class that assists with getting DLL load and unload notifications.
33 m : class DllNotificationWatcher {
34 m : public:
35 m : enum EventType {
36 m : kDllLoaded,
37 m : kDllUnloaded,
38 m : };
39 m : typedef base::Callback<void(EventType type,
40 m : HMODULE module,
41 m : size_t module_size,
42 m : const base::StringPiece16& dll_path,
43 m : const base::StringPiece16& dll_base_name)>
44 m : CallbackType;
45 :
46 m : DllNotificationWatcher();
47 m : ~DllNotificationWatcher();
48 :
49 : // Initialize for notifications to @p callback.
50 : // @returns true on success, false on failure.
51 : // @note this will return false on systems that don't implement this
52 : // mechanism, Windows XP and earlier.
53 m : bool Init(const CallbackType& callback);
54 :
55 : // Uninitialize and unregister from further callbacks.
56 : // @note From observation, the registration and unregistration are done under
57 : // loader's lock, so there's no danger of callbacks after this function
58 : // returns.
59 m : void Reset();
60 :
61 m : private:
62 m : static void CALLBACK NotificationFunction(
63 m : ULONG reason,
64 m : const union _LDR_DLL_NOTIFICATION_DATA* data,
65 m : void* context);
66 :
67 m : CallbackType callback_;
68 m : void* cookie_;
69 m : };
70 :
71 m : } // namespace common
72 m : } // namespace agent
73 :
74 : #endif // SYZYGY_AGENT_COMMON_DLL_NOTIFICATIONS_H_
|