1 | /** @file
|
---|
2 | *
|
---|
3 | * VBoxHook -- Global windows hook dll
|
---|
4 | *
|
---|
5 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
6 | *
|
---|
7 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
8 | * available from http://www.virtualbox.org. This file is free software;
|
---|
9 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
10 | * General Public License (GPL) as published by the Free Software
|
---|
11 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
12 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
13 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
14 | *
|
---|
15 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
16 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
17 | * additional information or have any questions.
|
---|
18 | */
|
---|
19 | #include <windows.h>
|
---|
20 | #include <VBoxHook.h>
|
---|
21 | #include <stdio.h>
|
---|
22 |
|
---|
23 | #pragma data_seg("SHARED")
|
---|
24 | static HWINEVENTHOOK hEventHook[2] = {0};
|
---|
25 | #pragma data_seg()
|
---|
26 | #pragma comment(linker, "/section:SHARED,RWS")
|
---|
27 |
|
---|
28 | static HANDLE hNotifyEvent = 0;
|
---|
29 |
|
---|
30 | #ifdef DEBUG
|
---|
31 | void WriteLog(char *String, ...);
|
---|
32 | #define dprintf(a) do { WriteLog a; } while (0)
|
---|
33 | #else
|
---|
34 | #define dprintf(a) do {} while (0)
|
---|
35 | #endif /* DEBUG */
|
---|
36 |
|
---|
37 |
|
---|
38 | void CALLBACK VBoxHandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
|
---|
39 | LONG idObject, LONG idChild,
|
---|
40 | DWORD dwEventThread, DWORD dwmsEventTime)
|
---|
41 | {
|
---|
42 | DWORD dwStyle;
|
---|
43 | if ( idObject != OBJID_WINDOW
|
---|
44 | || !hwnd)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | dwStyle = GetWindowLong(hwnd, GWL_STYLE);
|
---|
48 | if (dwStyle & WS_CHILD)
|
---|
49 | return;
|
---|
50 |
|
---|
51 | switch(event)
|
---|
52 | {
|
---|
53 | case EVENT_OBJECT_LOCATIONCHANGE:
|
---|
54 | if (!(dwStyle & WS_VISIBLE))
|
---|
55 | return;
|
---|
56 |
|
---|
57 | case EVENT_OBJECT_CREATE:
|
---|
58 | case EVENT_OBJECT_DESTROY:
|
---|
59 | case EVENT_OBJECT_HIDE:
|
---|
60 | case EVENT_OBJECT_SHOW:
|
---|
61 | #ifdef DEBUG
|
---|
62 | switch(event)
|
---|
63 | {
|
---|
64 | case EVENT_OBJECT_LOCATIONCHANGE:
|
---|
65 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_LOCATIONCHANGE for window %x\n", hwnd));
|
---|
66 | break;
|
---|
67 | case EVENT_OBJECT_CREATE:
|
---|
68 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_CREATE for window %x\n", hwnd));
|
---|
69 | break;
|
---|
70 | case EVENT_OBJECT_HIDE:
|
---|
71 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_HIDE for window %x\n", hwnd));
|
---|
72 | break;
|
---|
73 | case EVENT_OBJECT_SHOW:
|
---|
74 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_SHOW for window %x\n", hwnd));
|
---|
75 | break;
|
---|
76 | case EVENT_OBJECT_DESTROY:
|
---|
77 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_DESTROY for window %x\n", hwnd));
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 | if (!hNotifyEvent)
|
---|
82 | {
|
---|
83 | hNotifyEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
|
---|
84 | dprintf(("OpenEvent returned %x (last err=%x)\n", hNotifyEvent, GetLastError()));
|
---|
85 | }
|
---|
86 | BOOL ret = SetEvent(hNotifyEvent);
|
---|
87 | dprintf(("SetEvent %x returned %d (last error %x)\n", hNotifyEvent, ret, GetLastError()));
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /* Install the global message hook */
|
---|
94 | BOOL VBoxInstallHook(HMODULE hDll)
|
---|
95 | {
|
---|
96 | if (hEventHook[0] || hEventHook[1])
|
---|
97 | return TRUE;
|
---|
98 |
|
---|
99 | CoInitialize(NULL);
|
---|
100 | hEventHook[0] = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE,
|
---|
101 | hDll,
|
---|
102 | VBoxHandleWinEvent,
|
---|
103 | 0, 0,
|
---|
104 | WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
|
---|
105 |
|
---|
106 | hEventHook[1] = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE,
|
---|
107 | hDll,
|
---|
108 | VBoxHandleWinEvent,
|
---|
109 | 0, 0,
|
---|
110 | WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
|
---|
111 | return !!hEventHook[0];
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Remove the global message hook */
|
---|
115 | BOOL VBoxRemoveHook()
|
---|
116 | {
|
---|
117 | if (hEventHook[0] && hEventHook[1])
|
---|
118 | {
|
---|
119 | UnhookWinEvent(hEventHook[0]);
|
---|
120 | UnhookWinEvent(hEventHook[1]);
|
---|
121 | CoUninitialize();
|
---|
122 | }
|
---|
123 | hEventHook[0] = hEventHook[1] = 0;
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | #ifdef DEBUG
|
---|
129 | #include <VBox/VBoxGuest.h>
|
---|
130 |
|
---|
131 | static char LogBuffer[1024];
|
---|
132 | static HANDLE gVBoxDriver = INVALID_HANDLE_VALUE;
|
---|
133 |
|
---|
134 | VBGLR3DECL(int) VbglR3GRPerform(VMMDevRequestHeader *pReq)
|
---|
135 | {
|
---|
136 | DWORD cbReturned;
|
---|
137 | DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(pReq->size), pReq, pReq->size,
|
---|
138 | pReq, pReq->size, &cbReturned, NULL);
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void WriteLog(char *pszStr, ...)
|
---|
143 | {
|
---|
144 | VMMDevReqLogString *pReq = (VMMDevReqLogString *)LogBuffer;
|
---|
145 | int rc;
|
---|
146 |
|
---|
147 | /* open VBox guest driver */
|
---|
148 | if (gVBoxDriver == INVALID_HANDLE_VALUE)
|
---|
149 | gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
|
---|
150 | GENERIC_READ | GENERIC_WRITE,
|
---|
151 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
152 | NULL,
|
---|
153 | OPEN_EXISTING,
|
---|
154 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
---|
155 | NULL);
|
---|
156 |
|
---|
157 | if (gVBoxDriver == INVALID_HANDLE_VALUE)
|
---|
158 | return;
|
---|
159 |
|
---|
160 | va_list va;
|
---|
161 |
|
---|
162 | va_start(va, pszStr);
|
---|
163 |
|
---|
164 | vmmdevInitRequest(&pReq->header, VMMDevReq_LogString);
|
---|
165 | vsprintf(pReq->szString, pszStr, va);
|
---|
166 | pReq->header.size += strlen(pReq->szString);
|
---|
167 | rc = VbglR3GRPerform(&pReq->header);
|
---|
168 |
|
---|
169 | va_end (va);
|
---|
170 | return;
|
---|
171 | }
|
---|
172 |
|
---|
173 | #endif
|
---|