VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibAdditions.cpp@ 37256

Last change on this file since 37256 was 37256, checked in by vboxsync, 14 years ago

VBoxGuestR3LibAdditions: VersionEx -> VersionExt, refacatored code.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1/* $Id: VBoxGuestR3LibAdditions.cpp 37256 2011-05-30 12:39:03Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Additions Info.
4 */
5
6/*
7 * Copyright (C) 2007-2011 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/mem.h>
32#include <iprt/string.h>
33#include <VBox/log.h>
34#include <VBox/version.h>
35#include "VBGLR3Internal.h"
36
37
38/**
39 * Fallback for VbglR3GetAdditionsVersion.
40 */
41static int vbglR3GetAdditionsCompileTimeVersion(char **ppszVer, char **ppszVerEx, char **ppszRev)
42{
43 int rc = VINF_SUCCESS;
44 if (ppszVer)
45 rc = RTStrDupEx(ppszVer, VBOX_VERSION_STRING_RAW);
46 if (RT_SUCCESS(rc))
47 {
48 if (ppszVerEx)
49 rc = RTStrDupEx(ppszVerEx, VBOX_VERSION_STRING);
50 if (RT_SUCCESS(rc))
51 {
52 if (ppszRev)
53 {
54#if 0
55 char szRev[64];
56 RTStrPrintf(szRev, sizeof(szRev), "%d", VBOX_SVN_REV);
57 rc = RTStrDupEx(ppszRev, szRev);
58#else
59 rc = RTStrDupEx(ppszRev, RT_XSTR(VBOX_SVN_REV));
60#endif
61 }
62 if (RT_SUCCESS(rc))
63 return VINF_SUCCESS;
64
65 /* bail out: */
66 }
67 if (ppszVerEx)
68 {
69 RTStrFree(*ppszVerEx);
70 *ppszVerEx = NULL;
71 }
72 }
73 if (ppszVer)
74 {
75 RTStrFree(*ppszVer);
76 *ppszVer = NULL;
77 }
78 return rc;
79}
80
81#ifdef RT_OS_WINDOWS
82
83/**
84 * Looks up the storage path handle (registry).
85 *
86 * @returns IPRT status value
87 * @param hKey Receives pointer of allocated version string. NULL is
88 * accepted. The returned pointer must be closed by
89 * vbglR3CloseAdditionsWinStoragePath().
90 */
91static int vbglR3QueryAdditionsWinStoragePath(PHKEY phKey)
92{
93 /*
94 * Try get the *installed* version first.
95 */
96 LONG r;
97
98 /* Check the built in vendor path first. */
99 char szPath[255];
100 RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
101 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
102# ifdef RT_ARCH_AMD64
103 if (r != ERROR_SUCCESS)
104 {
105 /* Check Wow6432Node. */
106 RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\Wow6432Node\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
107 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
108 }
109# endif
110
111 /* Check the "Sun" path first. */
112 if (r != ERROR_SUCCESS)
113 {
114 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
115# ifdef RT_ARCH_AMD64
116 if (r != ERROR_SUCCESS)
117 {
118 /* Check Wow6432Node (for new entries). */
119 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
120 }
121# endif
122 }
123
124 /* Still no luck? Then try the old "Sun xVM" paths ... */
125 if (r != ERROR_SUCCESS)
126 {
127 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
128# ifdef RT_ARCH_AMD64
129 if (r != ERROR_SUCCESS)
130 {
131 /* Check Wow6432Node (for new entries). */
132 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
133 }
134# endif
135 }
136 return RTErrConvertFromWin32(r);
137}
138
139
140/**
141 * Closes the storage path handle (registry).
142 *
143 * @returns IPRT status value
144 * @param hKey Handle to close, retrieved by
145 * vbglR3QueryAdditionsWinStoragePath().
146 */
147static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
148{
149 return RTErrConvertFromWin32(RegCloseKey(hKey));
150}
151
152#endif /* RT_OS_WINDOWS */
153
154/**
155 * Reports the Guest Additions status of a certain facility to the host.
156 *
157 * @returns IPRT status value
158 * @param enmFacility The facility to report the status on.
159 * @param enmStatus The new status of the facility.
160 * @param fReserved Reserved for future use (what?).
161 */
162VBGLR3DECL(int) VbglR3ReportAdditionsStatus(VBoxGuestFacilityType enmFacility,
163 VBoxGuestFacilityStatus enmStatusCurrent,
164 uint32_t fReserved)
165{
166 VMMDevReportGuestStatus Report;
167 RT_ZERO(Report);
168 int rc = vmmdevInitRequest((VMMDevRequestHeader*)&Report, VMMDevReq_ReportGuestStatus);
169 if (RT_SUCCESS(rc))
170 {
171 Report.guestStatus.facility = enmFacility;
172 Report.guestStatus.status = enmStatusCurrent;
173 Report.guestStatus.flags = fReserved;
174
175 rc = vbglR3GRPerform(&Report.header);
176 }
177 return rc;
178}
179
180
181int vbglR3QueryRegistryString(HKEY hKey, const char *pszValName, char *pszBuffer, size_t cchBuffer)
182{
183 AssertReturn(pszValName, VERR_INVALID_PARAMETER);
184 AssertReturn(pszBuffer, VERR_INVALID_POINTER);
185 AssertReturn(cchBuffer, VERR_INVALID_PARAMETER);
186
187 int rc = VINF_SUCCESS;
188
189 DWORD dwType;
190 DWORD dwSize = (DWORD)cchBuffer;
191 LONG lRet = RegQueryValueEx(hKey, pszValName, NULL, &dwType, (BYTE*)(LPCTSTR)pszBuffer, &dwSize);
192 if (lRet == ERROR_SUCCESS)
193 {
194 rc = dwType == REG_SZ
195 ? VINF_SUCCESS : VERR_INVALID_PARAMETER;
196 }
197 else
198 rc = RTErrConvertFromWin32(lRet);
199 return rc;
200}
201
202
203/**
204 * Retrieves the installed Guest Additions version and/or revision.
205 *
206 * @returns IPRT status value
207 * @param ppszVer Receives pointer of allocated raw version string
208 * (major.minor.build). NULL is accepted. The returned
209 * pointer must be freed using RTStrFree().*
210 * @param ppszVerExt Receives pointer of allocated full version string
211 * (raw version + vendor suffix(es)). NULL is
212 * accepted. The returned pointer must be freed using
213 * RTStrFree().
214 * @param ppszRev Receives pointer of allocated revision string. NULL is
215 * accepted. The returned pointer must be freed using
216 * RTStrFree().
217 */
218VBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszVerExt, char **ppszRev)
219{
220 /*
221 * Zap the return value up front.
222 */
223 if (ppszVer)
224 *ppszVer = NULL;
225 if (ppszVerExt)
226 *ppszVerExt = NULL;
227 if (ppszRev)
228 *ppszRev = NULL;
229
230
231#ifdef RT_OS_WINDOWS
232 HKEY hKey;
233 int rc = vbglR3QueryAdditionsWinStoragePath(&hKey);
234 if (RT_SUCCESS(rc))
235 {
236 /*
237 * Version.
238 */
239 char szTemp[32];
240 if (ppszVer)
241 {
242 rc = vbglR3QueryRegistryString(hKey, "Version", szTemp, sizeof(szTemp));
243 if (RT_SUCCESS(rc))
244 rc = RTStrDupEx(ppszVer, szTemp);
245 }
246
247 if ( RT_SUCCESS(rc)
248 && ppszVerExt)
249 {
250 rc = vbglR3QueryRegistryString(hKey, "VersionExt", szTemp, sizeof(szTemp));
251 if (RT_SUCCESS(rc))
252 rc = RTStrDupEx(ppszVerExt, szTemp);
253 }
254
255 /*
256 * Revision.
257 */
258 if ( RT_SUCCESS(rc)
259 && ppszRev)
260 {
261 rc = vbglR3QueryRegistryString(hKey, "Revision", szTemp, sizeof(szTemp));
262 if (RT_SUCCESS(rc))
263 rc = RTStrDupEx(ppszRev, szTemp);
264 }
265
266 int rc2 = vbglR3CloseAdditionsWinStoragePath(hKey);
267 if (RT_SUCCESS(rc))
268 rc = rc2;
269 else
270 {
271 /* Clean up allocated strings on error. */
272 if (*ppszVer)
273 RTStrFree(*ppszVer);
274 if (*ppszVerExt)
275 RTStrFree(*ppszVerExt);
276 if (*ppszRev)
277 RTStrFree(*ppszRev);
278 }
279 }
280 else
281 {
282 /*
283 * No registry entries found, return the version string compiled
284 * into this binary.
285 */
286 rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszVerExt, ppszRev);
287 }
288 return rc;
289
290#else /* !RT_OS_WINDOWS */
291 /*
292 * On non-Windows platforms just return the compile-time version string.
293 */
294 return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszVerExt, ppszRev);
295#endif /* !RT_OS_WINDOWS */
296}
297
298
299/**
300 * Retrieves the installation path of Guest Additions.
301 *
302 * @returns IPRT status value
303 * @param ppszPath Receives pointer of allocated installation path string.
304 * The returned pointer must be freed using
305 * RTStrFree().
306 */
307VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
308{
309 int rc;
310#ifdef RT_OS_WINDOWS
311 HKEY hKey;
312 rc = vbglR3QueryAdditionsWinStoragePath(&hKey);
313 if (RT_SUCCESS(rc))
314 {
315 /* Installation directory. */
316 DWORD dwType;
317 DWORD dwSize = _MAX_PATH * sizeof(char);
318 char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
319 if (pszTmp)
320 {
321 LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
322 if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
323 {
324 rc = RTErrConvertFromNtStatus(l);
325 }
326 else
327 {
328 if (dwType == REG_SZ)
329 rc = RTStrDupEx(ppszPath, pszTmp);
330 else
331 rc = VERR_INVALID_PARAMETER;
332 if (RT_SUCCESS(rc))
333 {
334 /* Flip slashes. */
335 for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
336 if (*pszTmp2 == '\\')
337 *pszTmp2 = '/';
338 }
339 }
340 RTMemFree(pszTmp);
341 }
342 else
343 rc = VERR_NO_MEMORY;
344 rc = vbglR3CloseAdditionsWinStoragePath(hKey);
345 }
346#else
347 /** @todo implement me */
348 rc = VERR_NOT_IMPLEMENTED;
349#endif
350 return rc;
351}
352
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette