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