summaryrefslogtreecommitdiff
path: root/src/VBox/Additions/WINNT/VBoxHook
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2014-03-26 19:21:20 +0000
committer <>2014-05-08 15:03:54 +0000
commitfb123f93f9f5ce42c8e5785d2f8e0edaf951740e (patch)
treec2103d76aec5f1f10892cd1d3a38e24f665ae5db /src/VBox/Additions/WINNT/VBoxHook
parent58ed4748338f9466599adfc8a9171280ed99e23f (diff)
downloadVirtualBox-master.tar.gz
Imported from /home/lorry/working-area/delta_VirtualBox/VirtualBox-4.3.10.tar.bz2.HEADVirtualBox-4.3.10master
Diffstat (limited to 'src/VBox/Additions/WINNT/VBoxHook')
-rw-r--r--src/VBox/Additions/WINNT/VBoxHook/VBoxHook.cpp205
-rw-r--r--src/VBox/Additions/WINNT/VBoxHook/VBoxHook.def20
-rw-r--r--src/VBox/Additions/WINNT/VBoxHook/dllmain.cpp36
-rw-r--r--src/VBox/Additions/WINNT/VBoxHook/testcase/tstHook.cpp8
4 files changed, 163 insertions, 106 deletions
diff --git a/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.cpp b/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.cpp
index 553aae32..291a9d7c 100644
--- a/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.cpp
+++ b/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.cpp
@@ -1,8 +1,10 @@
+/* $Id: VBoxHook.cpp $ */
/** @file
- *
* VBoxHook -- Global windows hook dll
- *
- * Copyright (C) 2006-2007 Oracle Corporation
+ */
+
+/*
+ * Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
@@ -12,29 +14,32 @@
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
+
#include <Windows.h>
#include <VBoxHook.h>
#include <VBox/VBoxGuestLib.h>
#include <stdio.h>
#pragma data_seg("SHARED")
-static HWINEVENTHOOK hEventHook[2] = {0};
+static HWINEVENTHOOK hWinEventHook[2] = {0};
+static HWINEVENTHOOK hDesktopEventHook = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")
-static HANDLE hNotifyEvent = 0;
+static HANDLE hWinNotifyEvent = 0;
+static HANDLE hDesktopNotifyEvent = 0;
#ifdef DEBUG
-void WriteLog(char *String, ...);
-#define dprintf(a) do { WriteLog a; } while (0)
+static void WriteLog(const char *pszFormat, ...);
+# define dprintf(a) do { WriteLog a; } while (0)
#else
-#define dprintf(a) do {} while (0)
-#endif /* DEBUG */
+# define dprintf(a) do {} while (0)
+#endif /* !DEBUG */
-void CALLBACK VBoxHandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
- LONG idObject, LONG idChild,
- DWORD dwEventThread, DWORD dwmsEventTime)
+static void CALLBACK VBoxHandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
+ LONG idObject, LONG idChild,
+ DWORD dwEventThread, DWORD dwmsEventTime)
{
DWORD dwStyle;
if ( idObject != OBJID_WINDOW
@@ -75,97 +80,147 @@ void CALLBACK VBoxHandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
break;
}
#endif
- if (!hNotifyEvent)
+ if (!hWinNotifyEvent)
{
- hNotifyEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
- dprintf(("OpenEvent returned %x (last err=%x)\n", hNotifyEvent, GetLastError()));
+ hWinNotifyEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, VBOXHOOK_GLOBAL_WT_EVENT_NAME);
+ dprintf(("OpenEvent returned %x (last err=%x)\n", hWinNotifyEvent, GetLastError()));
}
- BOOL ret = SetEvent(hNotifyEvent);
- dprintf(("SetEvent %x returned %d (last error %x)\n", hNotifyEvent, ret, GetLastError()));
+ BOOL ret = SetEvent(hWinNotifyEvent);
+ dprintf(("SetEvent %x returned %d (last error %x)\n", hWinNotifyEvent, ret, GetLastError()));
break;
}
}
+static void CALLBACK VBoxHandleDesktopEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
+ LONG idObject, LONG idChild,
+ DWORD dwEventThread, DWORD dwmsEventTime)
+{
+ if (!hDesktopNotifyEvent)
+ {
+ hDesktopNotifyEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, VBOXHOOK_GLOBAL_DT_EVENT_NAME);
+ dprintf(("OpenEvent returned %x (last err=%x)\n", hDesktopNotifyEvent, GetLastError()));
+ }
+ BOOL ret = SetEvent(hDesktopNotifyEvent);
+ dprintf(("SetEvent %x returned %d (last error %x)\n", hDesktopNotifyEvent, ret, GetLastError()));
+}
-/* Install the global message hook */
-BOOL VBoxInstallHook(HMODULE hDll)
+BOOL VBoxHookInstallActiveDesktopTracker(HMODULE hDll)
{
- if (hEventHook[0] || hEventHook[1])
+ if (hDesktopEventHook)
return TRUE;
CoInitialize(NULL);
- hEventHook[0] = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE,
- hDll,
- VBoxHandleWinEvent,
- 0, 0,
- WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
-
- hEventHook[1] = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE,
- hDll,
- VBoxHandleWinEvent,
- 0, 0,
- WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
- return !!hEventHook[0];
+ hDesktopEventHook = SetWinEventHook(EVENT_SYSTEM_DESKTOPSWITCH, EVENT_SYSTEM_DESKTOPSWITCH,
+ hDll,
+ VBoxHandleDesktopEvent,
+ 0, 0,
+ 0);
+
+ return !!hDesktopEventHook;
+
}
-/* Remove the global message hook */
-BOOL VBoxRemoveHook()
+BOOL VBoxHookRemoveActiveDesktopTracker()
{
- if (hEventHook[0] && hEventHook[1])
+ if (hDesktopEventHook)
{
- UnhookWinEvent(hEventHook[0]);
- UnhookWinEvent(hEventHook[1]);
+ UnhookWinEvent(hDesktopEventHook);
CoUninitialize();
}
- hEventHook[0] = hEventHook[1] = 0;
- return true;
+ hDesktopEventHook = 0;
+ return TRUE;
}
+/** Install the global message hook */
+BOOL VBoxHookInstallWindowTracker(HMODULE hDll)
+{
+ if (hWinEventHook[0] || hWinEventHook[1])
+ return TRUE;
-#ifdef DEBUG
-#include <VBox/VBoxGuest.h>
-#include <VBox/VMMDev.h>
-
-static char LogBuffer[1024];
-static HANDLE gVBoxDriver = INVALID_HANDLE_VALUE;
+ CoInitialize(NULL);
+ hWinEventHook[0] = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE,
+ hDll,
+ VBoxHandleWinEvent,
+ 0, 0,
+ WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
+
+ hWinEventHook[1] = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_HIDE,
+ hDll,
+ VBoxHandleWinEvent,
+ 0, 0,
+ WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
+ return !!hWinEventHook[0];
+}
-VBGLR3DECL(int) VbglR3GRPerform(VMMDevRequestHeader *pReq)
+/** Remove the global message hook */
+BOOL VBoxHookRemoveWindowTracker()
{
- DWORD cbReturned;
- DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_VMMREQUEST(pReq->size), pReq, pReq->size,
- pReq, pReq->size, &cbReturned, NULL);
- return VINF_SUCCESS;
+ if (hWinEventHook[0] && hWinEventHook[1])
+ {
+ UnhookWinEvent(hWinEventHook[0]);
+ UnhookWinEvent(hWinEventHook[1]);
+ CoUninitialize();
+ }
+ hWinEventHook[0] = hWinEventHook[1] = 0;
+ return TRUE;
}
-void WriteLog(char *pszStr, ...)
+
+#ifdef DEBUG
+# include <VBox/VBoxGuest.h>
+# include <VBox/VMMDev.h>
+
+/**
+ * dprintf worker using VBoxGuest.sys and VMMDevReq_LogString.
+ */
+static void WriteLog(const char *pszFormat, ...)
{
- VMMDevReqLogString *pReq = (VMMDevReqLogString *)LogBuffer;
- int rc;
-
- /* open VBox guest driver */
- if (gVBoxDriver == INVALID_HANDLE_VALUE)
- gVBoxDriver = CreateFile(VBOXGUEST_DEVICE_NAME,
- GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
- NULL);
-
- if (gVBoxDriver == INVALID_HANDLE_VALUE)
- return;
+ /*
+ * Open VBox guest driver once.
+ */
+ static HANDLE s_hVBoxGuest = INVALID_HANDLE_VALUE;
+ HANDLE hVBoxGuest = s_hVBoxGuest;
+ if (hVBoxGuest == INVALID_HANDLE_VALUE)
+ {
+ hVBoxGuest = CreateFile(VBOXGUEST_DEVICE_NAME,
+ GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
+ NULL);
+ if (hVBoxGuest == INVALID_HANDLE_VALUE)
+ return;
+ s_hVBoxGuest = hVBoxGuest;
+ }
- va_list va;
+ /*
+ * We're apparently afraid of using stack here, so we use a static buffer
+ * instead and pray we won't be here at the same time on two threads...
+ */
+ static union
+ {
+ VMMDevReqLogString Req;
+ uint8_t abBuf[1024];
+ } s_uBuf;
- va_start(va, pszStr);
+ vmmdevInitRequest(&s_uBuf.Req.header, VMMDevReq_LogString);
+
+ va_list va;
+ va_start(va, pszFormat);
+ size_t cch = vsprintf(s_uBuf.Req.szString, pszFormat, va);
+ va_end(va);
- vmmdevInitRequest(&pReq->header, VMMDevReq_LogString);
- vsprintf(pReq->szString, pszStr, va);
- pReq->header.size += strlen(pReq->szString);
- rc = VbglR3GRPerform(&pReq->header);
+ s_uBuf.Req.header.size += (uint32_t)cch;
+ if (s_uBuf.Req.header.size > sizeof(s_uBuf))
+ __debugbreak();
- va_end (va);
- return;
+ DWORD cbReturned;
+ DeviceIoControl(hVBoxGuest, VBOXGUEST_IOCTL_VMMREQUEST(s_uBuf.Req.size),
+ &s_uBuf.Req, s_uBuf.Req.header.size,
+ &s_uBuf.Req, s_uBuf.Req.header.size,
+ &cbReturned, NULL);
}
-#endif
+#endif /* DEBUG */
+
diff --git a/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.def b/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.def
index 65466ffc..49b5e32b 100644
--- a/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.def
+++ b/src/VBox/Additions/WINNT/VBoxHook/VBoxHook.def
@@ -1,8 +1,10 @@
-;/** @file
-; *
-; * VBoxHook -- Global windows hook dll
-; *
-; Copyright (C) 2006-2007 Oracle Corporation
+; $Id: VBoxHook.def $
+;; @file
+; VBoxHook -- Global windows hook dll
+;
+
+;
+; Copyright (C) 2006-2010 Oracle Corporation
;
; This file is part of VirtualBox Open Source Edition (OSE), as
; available from http://www.virtualbox.org. This file is free software;
@@ -12,11 +14,11 @@
; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
;
-; */
LIBRARY VBoxHook
-
EXPORTS
- VBoxInstallHook
- VBoxRemoveHook \ No newline at end of file
+ VBoxHookInstallActiveDesktopTracker
+ VBoxHookRemoveActiveDesktopTracker
+ VBoxHookInstallWindowTracker
+ VBoxHookRemoveWindowTracker
diff --git a/src/VBox/Additions/WINNT/VBoxHook/dllmain.cpp b/src/VBox/Additions/WINNT/VBoxHook/dllmain.cpp
index 8d3928d3..286a3a47 100644
--- a/src/VBox/Additions/WINNT/VBoxHook/dllmain.cpp
+++ b/src/VBox/Additions/WINNT/VBoxHook/dllmain.cpp
@@ -1,8 +1,10 @@
+/* $Id: dllmain.cpp $ */
/** @file
- *
* VBoxHook -- Global windows hook dll
- *
- * Copyright (C) 2006-2007 Oracle Corporation
+ */
+
+/*
+ * Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
@@ -12,7 +14,9 @@
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
-#include <windows.h>
+
+
+#include <Windows.h>
/**
@@ -25,26 +29,22 @@
*/
BOOL WINAPI DllMain(HINSTANCE hDLLInst, DWORD fdwReason, LPVOID lpvReserved)
{
- BOOL bStatus = TRUE;
-
switch (fdwReason)
{
- case DLL_PROCESS_ATTACH:
- return TRUE;
+ case DLL_PROCESS_ATTACH:
+ return TRUE;
- case DLL_PROCESS_DETACH:
- return TRUE;
+ case DLL_PROCESS_DETACH:
+ return TRUE;
- case DLL_THREAD_ATTACH:
- break;
+ case DLL_THREAD_ATTACH:
+ return TRUE;
- case DLL_THREAD_DETACH:
- return TRUE;
+ case DLL_THREAD_DETACH:
+ return TRUE;
- default:
- break;
+ default:
+ return TRUE;
}
-
- return bStatus;
}
diff --git a/src/VBox/Additions/WINNT/VBoxHook/testcase/tstHook.cpp b/src/VBox/Additions/WINNT/VBoxHook/testcase/tstHook.cpp
index f86be148..6f6f1a42 100644
--- a/src/VBox/Additions/WINNT/VBoxHook/testcase/tstHook.cpp
+++ b/src/VBox/Additions/WINNT/VBoxHook/testcase/tstHook.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2006-2007 Oracle Corporation
+ * Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
@@ -21,12 +21,12 @@ int main(int argc, char **argv)
{
printf("Enabling global hook\n");
- HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, VBOXHOOK_GLOBAL_EVENT_NAME);
+ HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, VBOXHOOK_GLOBAL_WT_EVENT_NAME);
- VBoxInstallHook(GetModuleHandle("VBoxHook.dll"));
+ VBoxHookInstallWindowTracker(GetModuleHandle("VBoxHook.dll"));
getchar();
printf("Disabling global hook\n");
- VBoxRemoveHook();
+ VBoxHookRemoveWindowTracker();
return 0;
}