VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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