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