VirtualBox

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

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

VBoxGuestLib: split up VBoxGuestR3LibMisc.cpp.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/* $Id: VBoxGuestR3LibAdditions.cpp 27083 2010-03-05 13:05:51Z 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 new path first. */
90 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
91# ifdef RT_ARCH_AMD64
92 if (r != ERROR_SUCCESS)
93 {
94 /* Check Wow6432Node (for new entries). */
95 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\VirtualBox Guest Additions", 0, KEY_READ, phKey);
96 }
97# endif
98
99 /* Still no luck? Then try the old xVM paths ... */
100 if (r != ERROR_SUCCESS)
101 {
102 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
103# ifdef RT_ARCH_AMD64
104 if (r != ERROR_SUCCESS)
105 {
106 /* Check Wow6432Node (for new entries). */
107 r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Wow6432Node\\Sun\\xVM VirtualBox Guest Additions", 0, KEY_READ, phKey);
108 }
109# endif
110 }
111 return RTErrConvertFromWin32(r);
112}
113
114
115/**
116 * Closes the storage path handle (registry).
117 *
118 * @returns IPRT status value
119 * @param hKey Handle to close, retrieved by
120 * vbglR3GetAdditionsWinStoragePath().
121 */
122static int vbglR3CloseAdditionsWinStoragePath(HKEY hKey)
123{
124 return RTErrConvertFromWin32(RegCloseKey(hKey));
125}
126
127#endif /* RT_OS_WINDOWS */
128
129/**
130 * Retrieves the installed Guest Additions version and/or revision.
131 *
132 * @returns IPRT status value
133 * @param ppszVer Receives pointer of allocated version string. NULL is
134 * accepted. The returned pointer must be freed using
135 * RTStrFree().
136 * @param ppszRev Receives pointer of allocated revision string. NULL is
137 * accepted. The returned pointer must be freed using
138 * RTStrFree().
139 */
140VBGLR3DECL(int) VbglR3GetAdditionsVersion(char **ppszVer, char **ppszRev)
141{
142#ifdef RT_OS_WINDOWS
143 HKEY hKey;
144 int rc = vbglR3GetAdditionsWinStoragePath(&hKey);
145 if (RT_SUCCESS(rc))
146 {
147 /* Version. */
148 LONG l;
149 DWORD dwType;
150 DWORD dwSize = 32;
151 char *pszTmp;
152 if (ppszVer)
153 {
154 pszTmp = (char*)RTMemAlloc(dwSize);
155 if (pszTmp)
156 {
157 l = RegQueryValueEx(hKey, "Version", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
158 if (l == ERROR_SUCCESS)
159 {
160 if (dwType == REG_SZ)
161 rc = RTStrDupEx(ppszVer, pszTmp);
162 else
163 rc = VERR_INVALID_PARAMETER;
164 }
165 else
166 {
167 rc = RTErrConvertFromWin32(l);
168 }
169 RTMemFree(pszTmp);
170 }
171 else
172 rc = VERR_NO_MEMORY;
173 }
174 /* Revision. */
175 if (ppszRev)
176 {
177 dwSize = 32; /* Reset */
178 pszTmp = (char*)RTMemAlloc(dwSize);
179 if (pszTmp)
180 {
181 l = RegQueryValueEx(hKey, "Revision", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
182 if (l == ERROR_SUCCESS)
183 {
184 if (dwType == REG_SZ)
185 rc = RTStrDupEx(ppszRev, pszTmp);
186 else
187 rc = VERR_INVALID_PARAMETER;
188 }
189 else
190 {
191 rc = RTErrConvertFromWin32(l);
192 }
193 RTMemFree(pszTmp);
194 }
195 else
196 rc = VERR_NO_MEMORY;
197
198 if (RT_FAILURE(rc) && ppszVer)
199 {
200 RTStrFree(*ppszVer);
201 *ppszVer = NULL;
202 }
203 }
204 rc = vbglR3CloseAdditionsWinStoragePath(hKey);
205 }
206 else
207 {
208 /*
209 * No registry entries found, return the version string compiled
210 * into this binary.
211 */
212 rc = vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
213 }
214 return rc;
215
216#else /* !RT_OS_WINDOWS */
217 /*
218 * On non-Windows platforms just return the compile-time version string.
219 */
220 return vbglR3GetAdditionsCompileTimeVersion(ppszVer, ppszRev);
221#endif /* !RT_OS_WINDOWS */
222}
223
224
225/**
226 * Retrieves the installation path of Guest Additions.
227 *
228 * @returns IPRT status value
229 * @param ppszPath Receives pointer of allocated installation path string.
230 * The returned pointer must be freed using
231 * RTStrFree().
232 */
233VBGLR3DECL(int) VbglR3GetAdditionsInstallationPath(char **ppszPath)
234{
235 int rc;
236#ifdef RT_OS_WINDOWS
237 HKEY hKey;
238 rc = vbglR3GetAdditionsWinStoragePath(&hKey);
239 if (RT_SUCCESS(rc))
240 {
241 /* Installation directory. */
242 DWORD dwType;
243 DWORD dwSize = _MAX_PATH * sizeof(char);
244 char *pszTmp = (char*)RTMemAlloc(dwSize + 1);
245 if (pszTmp)
246 {
247 LONG l = RegQueryValueEx(hKey, "InstallDir", NULL, &dwType, (BYTE*)(LPCTSTR)pszTmp, &dwSize);
248 if ((l != ERROR_SUCCESS) && (l != ERROR_FILE_NOT_FOUND))
249 {
250 rc = RTErrConvertFromNtStatus(l);
251 }
252 else
253 {
254 if (dwType == REG_SZ)
255 rc = RTStrDupEx(ppszPath, pszTmp);
256 else
257 rc = VERR_INVALID_PARAMETER;
258 if (RT_SUCCESS(rc))
259 {
260 /* Flip slashes. */
261 for (char *pszTmp2 = ppszPath[0]; *pszTmp2; ++pszTmp2)
262 if (*pszTmp2 == '\\')
263 *pszTmp2 = '/';
264 }
265 }
266 RTMemFree(pszTmp);
267 }
268 else
269 rc = VERR_NO_MEMORY;
270 rc = vbglR3CloseAdditionsWinStoragePath(hKey);
271 }
272#else
273 /** @todo implement me */
274 rc = VERR_NOT_IMPLEMENTED;
275#endif
276 return rc;
277}
278
Note: See TracBrowser for help on using the repository browser.

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