VirtualBox

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

Last change on this file since 28544 was 28544, checked in by vboxsync, 15 years ago

VbglR3: Added Windows storage path lookup for built in vendors.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1/* $Id: VBoxGuestR3LibAdditions.cpp 28544 2010-04-21 08:10:03Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Additions Info.
4 */
5
6/*
7 * Copyright (C) 2007-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/mem.h>
36#include <iprt/string.h>
37#include <VBox/log.h>
38#include <VBox/version.h>
39#include "VBGLR3Internal.h"
40
41
42/**
43 * Fallback for vbglR3GetAdditionsVersion.
44 */
45static int vbglR3GetAdditionsCompileTimeVersion(char **ppszVer, char **ppszRev)
46{
47 if (ppszVer)
48 {
49 *ppszVer = RTStrDup(VBOX_VERSION_STRING);
50 if (!*ppszVer)
51 return VERR_NO_STR_MEMORY;
52 }
53
54 if (ppszRev)
55 {
56 char szRev[64];
57 RTStrPrintf(szRev, sizeof(szRev), "%d", VBOX_SVN_REV);
58 *ppszRev = RTStrDup(szRev);
59 if (!*ppszRev)
60 {
61 if (ppszVer)
62 {
63 RTStrFree(*ppszVer);
64 *ppszVer = NULL;
65 }
66 return VERR_NO_STR_MEMORY;
67 }
68 }
69
70 return VINF_SUCCESS;
71}
72
73#ifdef RT_OS_WINDOWS
74/**
75 * Looks up the storage path handle (registry).
76 *
77 * @returns IPRT status value
78 * @param hKey Receives pointer of allocated version string. NULL is
79 * accepted. The returned pointer must be closed by
80 * vbglR3CloseAdditionsWinStoragePath().
81 */
82static int vbglR3GetAdditionsWinStoragePath(PHKEY phKey)
83{
84 /*
85 * Try get the *installed* version first.
86 */
87 LONG r;
88
89 /* Check the built in vendor path first. */
90 char szPath[255];
91 RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
92 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
93# ifdef RT_ARCH_AMD64
94 if (r != ERROR_SUCCESS)
95 {
96 /* Check Wow6432Node. */
97 RTStrPrintf(szPath, sizeof(szPath), "SOFTWARE\\Wow6432Node\\%s\\VirtualBox Guest Additions", VBOX_VENDOR_SHORT);
98 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, phKey);
99 }
100# endif
101
102 /* Check the "Sun" path first. */
103 if (r != ERROR_SUCCESS)
104 {
105 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
106# ifdef RT_ARCH_AMD64
107 if (r != ERROR_SUCCESS)
108 {
109 /* Check Wow6432Node (for new entries). */
110 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
111 }
112# endif
113 }
114
115 /* Still no luck? Then try the old "Sun xVM" paths ... */
116 if (r != ERROR_SUCCESS)
117 {
118 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
119# ifdef RT_ARCH_AMD64
120 if (r != ERROR_SUCCESS)
121 {
122 /* Check Wow6432Node (for new entries). */
123 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
124 }
125# endif
126 }
127 return RTErrConvertFromWin32(r);
128}
129
130
131/**
132 * Closes the storage path handle (registry).
133 *
134 * @returns IPRT status value
135 * @param hKey Handle to close, retrieved by
136 * vbglR3GetAdditionsWinStoragePath().
137 */
138static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
139{
140 return RTErrConvertFromWin32(RegCloseKey(hKey));
141}
142
143#endif /* RT_OS_WINDOWS */
144
145/**
146 * Retrieves the installed Guest Additions version and/or revision.
147 *
148 * @returns IPRT status value
149 * @param ppszVer Receives pointer of allocated version string. NULL is
150 * accepted. The returned pointer must be freed using
151 * RTStrFree().
152 * @param ppszRev Receives pointer of allocated revision string. NULL is
153 * accepted. The returned pointer must be freed using
154 * RTStrFree().
155 */
156VBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszRev)
157{
158#ifdef RT_OS_WINDOWS
159 HKEY hKey;
160 int rc = vbglR3GetAdditionsWinStoragePath(&hKey);
161 if (RT_SUCCESS(rc))
162 {
163 /* Version. */
164 LONG l;
165 DWORD dwType;
166 DWORD dwSize = 32;
167 char *pszTmp;
168 if (ppszVer)
169 {
170 pszTmp = (char*)RTMemAlloc(dwSize);
171 if (pszTmp)
172 {
173 l = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
174 if (l == ERROR_SUCCESS)
175 {
176 if (dwType == REG_SZ)
177 rc = RTStrDupEx(ppszVer, pszTmp);
178 else
179 rc = VERR_INVALID_PARAMETER;
180 }
181 else
182 {
183 rc = RTErrConvertFromWin32(l);
184 }
185 RTMemFree(pszTmp);
186 }
187 else
188 rc = VERR_NO_MEMORY;
189 }
190 /* Revision. */
191 if (ppszRev)
192 {
193 dwSize = 32; /* Reset */
194 pszTmp = (char*)RTMemAlloc(dwSize);
195 if (pszTmp)
196 {
197 l = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
198 if (l == ERROR_SUCCESS)
199 {
200 if (dwType == REG_SZ)
201 rc = RTStrDupEx(ppszRev, pszTmp);
202 else
203 rc = VERR_INVALID_PARAMETER;
204 }
205 else
206 {
207 rc = RTErrConvertFromWin32(l);
208 }
209 RTMemFree(pszTmp);
210 }
211 else
212 rc = VERR_NO_MEMORY;
213
214 if (RT_FAILURE(rc) && ppszVer)
215 {
216 RTStrFree(*ppszVer);
217 *ppszVer = NULL;
218 }
219 }
220 rc = vbglR3CloseAdditionsWinStoragePath(hKey);
221 }
222 else
223 {
224 /*
225 * No registry entries found, return the version string compiled
226 * into this binary.
227 */
228 rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
229 }
230 return rc;
231
232#else /* !RT_OS_WINDOWS */
233 /*
234 * On non-Windows platforms just return the compile-time version string.
235 */
236 return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
237#endif /* !RT_OS_WINDOWS */
238}
239
240
241/**
242 * Retrieves the installation path of Guest Additions.
243 *
244 * @returns IPRT status value
245 * @param ppszPath Receives pointer of allocated installation path string.
246 * The returned pointer must be freed using
247 * RTStrFree().
248 */
249VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
250{
251 int rc;
252#ifdef RT_OS_WINDOWS
253 HKEY hKey;
254 rc = vbglR3GetAdditionsWinStoragePath(&hKey);
255 if (RT_SUCCESS(rc))
256 {
257 /* Installation directory. */
258 DWORD dwType;
259 DWORD dwSize = _MAX_PATH * sizeof(char);
260 char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
261 if (pszTmp)
262 {
263 LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
264 if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
265 {
266 rc = RTErrConvertFromNtStatus(l);
267 }
268 else
269 {
270 if (dwType == REG_SZ)
271 rc = RTStrDupEx(ppszPath, pszTmp);
272 else
273 rc = VERR_INVALID_PARAMETER;
274 if (RT_SUCCESS(rc))
275 {
276 /* Flip slashes. */
277 for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
278 if (*pszTmp2 == '\\')
279 *pszTmp2 = '/';
280 }
281 }
282 RTMemFree(pszTmp);
283 }
284 else
285 rc = VERR_NO_MEMORY;
286 rc = vbglR3CloseAdditionsWinStoragePath(hKey);
287 }
288#else
289 /** @todo implement me */
290 rc = VERR_NOT_IMPLEMENTED;
291#endif
292 return rc;
293}
294
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