1 | /* $Id: startmmr.cpp 44864 2013-02-28 12:18:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxMMR - Multimedia Redirection
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <tchar.h>
|
---|
20 | #include <windows.h>
|
---|
21 |
|
---|
22 | #include <iprt/initterm.h>
|
---|
23 |
|
---|
24 | #include <VBox/Log.h>
|
---|
25 | #include <VBox/VBoxGuestLib.h>
|
---|
26 |
|
---|
27 | const char *g_pszMMRDLL = "VBoxMMRHook";
|
---|
28 | const char *g_pszMMRPROC = "CBTProc";
|
---|
29 | const WCHAR *g_pwszMMRFlags = L"VBoxMMR";
|
---|
30 |
|
---|
31 | const WCHAR *g_pwszMMRAdditions =
|
---|
32 | L"SOFTWARE\\Oracle\\VirtualBox Guest Additions";
|
---|
33 |
|
---|
34 | const DWORD g_dwMMREnabled = 0x00000001;
|
---|
35 |
|
---|
36 | HANDLE g_hCtrlEvent;
|
---|
37 |
|
---|
38 | BOOL MMRIsEnabled()
|
---|
39 | {
|
---|
40 | LONG lResult;
|
---|
41 | HKEY hKey;
|
---|
42 | DWORD dwType = 0;
|
---|
43 | DWORD dwValue = 0;
|
---|
44 | DWORD dwSize = sizeof(dwValue);
|
---|
45 |
|
---|
46 | BOOL fEnabled = TRUE;
|
---|
47 |
|
---|
48 | lResult = RegOpenKeyExW(
|
---|
49 | HKEY_LOCAL_MACHINE, g_pwszMMRAdditions, 0, KEY_QUERY_VALUE, &hKey);
|
---|
50 |
|
---|
51 | if (lResult == ERROR_SUCCESS)
|
---|
52 | {
|
---|
53 | lResult = RegQueryValueExW(
|
---|
54 | hKey, g_pwszMMRFlags, NULL, &dwType, (BYTE *) &dwValue, &dwSize);
|
---|
55 |
|
---|
56 | RegCloseKey(hKey);
|
---|
57 |
|
---|
58 | if (lResult == ERROR_SUCCESS &&
|
---|
59 | dwSize == sizeof(dwValue) &&
|
---|
60 | dwType == REG_DWORD)
|
---|
61 | {
|
---|
62 | fEnabled = g_dwMMREnabled & dwValue;
|
---|
63 | LogRel(("VBoxMMR: Registry setting: %d\n", dwValue));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | return fEnabled;
|
---|
68 | }
|
---|
69 |
|
---|
70 | BOOL CtrlHandler(DWORD type)
|
---|
71 | {
|
---|
72 | SetEvent(g_hCtrlEvent);
|
---|
73 | return TRUE;
|
---|
74 | }
|
---|
75 |
|
---|
76 | int APIENTRY WinMain(
|
---|
77 | HINSTANCE hInstance,
|
---|
78 | HINSTANCE hPrevInstance,
|
---|
79 | LPSTR lpCmdLine,
|
---|
80 | int nCmdShow)
|
---|
81 | {
|
---|
82 | HINSTANCE hMod = NULL;
|
---|
83 | HHOOK hHook = NULL;
|
---|
84 | HOOKPROC pHook = NULL;
|
---|
85 |
|
---|
86 | int rc = RTR3InitExeNoArguments(0);
|
---|
87 | if (RT_FAILURE(rc))
|
---|
88 | {
|
---|
89 | LogRel(("VBoxMMR: Error initializing RT runtime: %d\n", rc));
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | rc = VbglR3Init();
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | {
|
---|
96 | LogRel(("VBoxMMR: Error initializing VbglR3 runtime: %d\n", rc));
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (MMRIsEnabled())
|
---|
101 | {
|
---|
102 | hMod = LoadLibraryA(g_pszMMRDLL);
|
---|
103 | if (hMod == NULL)
|
---|
104 | {
|
---|
105 | LogRel(("VBoxMMR: Hooking library not found\n"));
|
---|
106 | return VERR_NOT_FOUND;
|
---|
107 | }
|
---|
108 |
|
---|
109 | pHook = (HOOKPROC) GetProcAddress(hMod, g_pszMMRPROC);
|
---|
110 | if (pHook == NULL)
|
---|
111 | {
|
---|
112 | LogRel(("VBoxMMR: Hooking proc not found\n"));
|
---|
113 | FreeLibrary(hMod);
|
---|
114 | return VERR_NOT_FOUND;
|
---|
115 | }
|
---|
116 |
|
---|
117 | hHook = SetWindowsHookEx(WH_CBT, pHook, hMod, 0);
|
---|
118 | if (hHook == NULL)
|
---|
119 | {
|
---|
120 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
121 | LogRel(("VBoxMMR: Error installing hooking proc: %d\n", rc));
|
---|
122 | FreeLibrary(hMod);
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | g_hCtrlEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
---|
127 |
|
---|
128 | if (SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE))
|
---|
129 | {
|
---|
130 | WaitForSingleObject(g_hCtrlEvent, INFINITE);
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | int rc = RTErrConvertFromWin32(GetLastError());
|
---|
135 | LogRel(("VBoxMMR: Error installing ctrl handler: %d\n", rc));
|
---|
136 | }
|
---|
137 |
|
---|
138 | CloseHandle(g_hCtrlEvent);
|
---|
139 |
|
---|
140 | UnhookWindowsHookEx(hHook);
|
---|
141 | FreeLibrary(hMod);
|
---|
142 | }
|
---|
143 |
|
---|
144 | VbglR3Term();
|
---|
145 |
|
---|
146 | return 0;
|
---|
147 | }
|
---|