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 <VBox/VBoxGuestLib.h>
|
---|
22 | #include <stdio.h>
|
---|
23 |
|
---|
24 | #pragma data_seg("SHARED")
|
---|
25 | static HWINEVENTHOOK hEventHook[2] = {0};
|
---|
26 | #pragma data_seg()
|
---|
27 | #pragma comment(linker, "/section:SHARED,RWS")
|
---|
28 |
|
---|
29 | static HANDLE hNotifyEvent = 0;
|
---|
30 |
|
---|
31 | #ifdef DEBUG
|
---|
32 | void WriteLog(char *String, ...);
|
---|
33 | #define dprintf(a) do { WriteLog a; } while (0)
|
---|
34 | #else
|
---|
35 | #define dprintf(a) do {} while (0)
|
---|
36 | #endif /* DEBUG */
|
---|
37 |
|
---|
38 |
|
---|
39 | void CALLBACK VBoxHandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
|
---|
40 | LONG idObject, LONG idChild,
|
---|
41 | DWORD dwEventThread, DWORD dwmsEventTime)
|
---|
42 | {
|
---|
43 | DWORD dwStyle;
|
---|
44 | if ( idObject != OBJID_WINDOW
|
---|
45 | || !hwnd)
|
---|
46 | return;
|
---|
47 |
|
---|
48 | dwStyle = GetWindowLong(hwnd, GWL_STYLE);
|
---|
49 | if (dwStyle & WS_CHILD)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | switch(event)
|
---|
53 | {
|
---|
54 | case EVENT_OBJECT_LOCATIONCHANGE:
|
---|
55 | if (!(dwStyle & WS_VISIBLE))
|
---|
56 | return;
|
---|
57 |
|
---|
58 | case EVENT_OBJECT_CREATE:
|
---|
59 | case EVENT_OBJECT_DESTROY:
|
---|
60 | case EVENT_OBJECT_HIDE:
|
---|
61 | case EVENT_OBJECT_SHOW:
|
---|
62 | #ifdef DEBUG
|
---|
63 | switch(event)
|
---|
64 | {
|
---|
65 | case EVENT_OBJECT_LOCATIONCHANGE:
|
---|
66 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_LOCATIONCHANGE for window %x\n", hwnd));
|
---|
67 | break;
|
---|
68 | case EVENT_OBJECT_CREATE:
|
---|
69 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_CREATE for window %x\n", hwnd));
|
---|
70 | break;
|
---|
71 | case EVENT_OBJECT_HIDE:
|
---|
72 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_HIDE for window %x\n", hwnd));
|
---|
73 | break;
|
---|
74 | case EVENT_OBJECT_SHOW:
|
---|
75 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_SHOW for window %x\n", hwnd));
|
---|
76 | break;
|
---|
77 | case EVENT_OBJECT_DESTROY:
|
---|
78 | dprintf(("VBoxHandleWinEvent EVENT_OBJECT_DESTROY for window %x\n", hwnd));
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | #endif
|
---|
82 | if (!hNotifyEvent)
|
---|
83 | {
|
---|
84 | hNotifyEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
|
---|
85 | dprintf(("OpenEvent returned %x (last err=%x)\n", hNotifyEvent, GetLastError()));
|
---|
86 | }
|
---|
87 | BOOL ret = SetEvent(hNotifyEvent);
|
---|
88 | dprintf(("SetEvent %x returned %d (last error %x)\n", hNotifyEvent, ret, GetLastError()));
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /* Install the global message hook */
|
---|
95 | BOOL VBoxInstallHook(HMODULE hDll)
|
---|
96 | {
|
---|
97 | if (hEventHook[0] || hEventHook[1])
|
---|
98 | return TRUE;
|
---|
99 |
|
---|
100 | CoInitialize(NULL);
|
---|
101 | hEventHook[0] = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE,
|
---|
102 | hDll,
|
---|
103 | VBoxHandleWinEvent,
|
---|
104 | 0, 0,
|
---|
105 | WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
|
---|
106 |
|
---|
107 | hEventHook[1] = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE,
|
---|
108 | hDll,
|
---|
109 | VBoxHandleWinEvent,
|
---|
110 | 0, 0,
|
---|
111 | WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
|
---|
112 | return !!hEventHook[0];
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* Remove the global message hook */
|
---|
116 | BOOL VBoxRemoveHook()
|
---|
117 | {
|
---|
118 | if (hEventHook[0] && hEventHook[1])
|
---|
119 | {
|
---|
120 | UnhookWinEvent(hEventHook[0]);
|
---|
121 | UnhookWinEvent(hEventHook[1]);
|
---|
122 | CoUninitialize();
|
---|
123 | }
|
---|
124 | hEventHook[0] = hEventHook[1] = 0;
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | #ifdef DEBUG
|
---|
130 | #include <VBox/VBoxGuest.h>
|
---|
131 | #include <VBox/VMMDev.h>
|
---|
132 |
|
---|
133 | static char LogBuffer[1024];
|
---|
134 | static HANDLE gVBoxDriver = INVALID_HANDLE_VALUE;
|
---|
135 |
|
---|
136 | VBGLR3DECL(int) VbglR3GRPerform(VMMDevRequestHeader *pReq)
|
---|
137 | {
|
---|
138 | DWORD cbReturned;
|
---|
139 | DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(pReq->size), pReq, pReq->size,
|
---|
140 | pReq, pReq->size, &cbReturned, NULL);
|
---|
141 | return VINF_SUCCESS;
|
---|
142 | }
|
---|
143 |
|
---|
144 | void WriteLog(char *pszStr, ...)
|
---|
145 | {
|
---|
146 | VMMDevReqLogString *pReq = (VMMDevReqLogString *)LogBuffer;
|
---|
147 | int rc;
|
---|
148 |
|
---|
149 | /* open VBox guest driver */
|
---|
150 | if (gVBoxDriver == INVALID_HANDLE_VALUE)
|
---|
151 | gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
|
---|
152 | GENERIC_READ | GENERIC_WRITE,
|
---|
153 | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
---|
154 | NULL,
|
---|
155 | OPEN_EXISTING,
|
---|
156 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
---|
157 | NULL);
|
---|
158 |
|
---|
159 | if (gVBoxDriver == INVALID_HANDLE_VALUE)
|
---|
160 | return;
|
---|
161 |
|
---|
162 | va_list va;
|
---|
163 |
|
---|
164 | va_start(va, pszStr);
|
---|
165 |
|
---|
166 | vmmdevInitRequest(&pReq->header, VMMDevReq_LogString);
|
---|
167 | vsprintf(pReq->szString, pszStr, va);
|
---|
168 | pReq->header.size += strlen(pReq->szString);
|
---|
169 | rc = VbglR3GRPerform(&pReq->header);
|
---|
170 |
|
---|
171 | va_end (va);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | #endif
|
---|