1 | /* $Id: VBoxServiceUtils.cpp 23655 2009-10-09 15:51:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceUtils - Some utility functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #ifdef RT_OS_WINDOWS
|
---|
27 | # include <Windows.h>
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 |
|
---|
34 | #include <VBox/VBoxGuestLib.h>
|
---|
35 | #include "VBoxServiceInternal.h"
|
---|
36 |
|
---|
37 |
|
---|
38 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
39 | /**
|
---|
40 | * Wrapper around VbglR3GuestPropWriteValue that does value formatting and
|
---|
41 | * logging.
|
---|
42 | *
|
---|
43 | * @returns VBox status code. Errors will be logged.
|
---|
44 | *
|
---|
45 | * @param u32ClientId The HGCM client ID for the guest property session.
|
---|
46 | * @param pszName The property name.
|
---|
47 | * @param pszValueFormat The property format string. If this is NULL then
|
---|
48 | * the property will be removed.
|
---|
49 | * @param ... Format arguments.
|
---|
50 | */
|
---|
51 | int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
|
---|
52 | {
|
---|
53 | int rc;
|
---|
54 | if (pszValueFormat != NULL)
|
---|
55 | {
|
---|
56 | /** @todo Log the value as well? just copy the guts of
|
---|
57 | * VbglR3GuestPropWriteValueV. */
|
---|
58 | VBoxServiceVerbose(3, "Writing guest property \"%s\"\n", pszName);
|
---|
59 | va_list va;
|
---|
60 | va_start(va, pszValueFormat);
|
---|
61 | rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
|
---|
62 | va_end(va);
|
---|
63 | if (RT_FAILURE(rc))
|
---|
64 | VBoxServiceError("Error writing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
|
---|
69 | if (RT_FAILURE(rc))
|
---|
70 | VBoxServiceError("Error removing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
|
---|
71 | }
|
---|
72 | return rc;
|
---|
73 | }
|
---|
74 | #endif /* VBOX_WITH_GUEST_PROPS */
|
---|
75 |
|
---|
76 |
|
---|
77 | #ifdef RT_OS_WINDOWS
|
---|
78 | BOOL VBoxServiceGetFileString(const char* pszFileName,
|
---|
79 | char* pszBlock,
|
---|
80 | char* pszString,
|
---|
81 | PUINT puiSize)
|
---|
82 | {
|
---|
83 | DWORD dwHandle, dwLen = 0;
|
---|
84 | UINT uiDataLen = 0;
|
---|
85 | char* lpData = NULL;
|
---|
86 | UINT uiValueLen = 0;
|
---|
87 | LPTSTR lpValue = NULL;
|
---|
88 | BOOL bRet = FALSE;
|
---|
89 |
|
---|
90 | Assert(pszFileName);
|
---|
91 | Assert(pszBlock);
|
---|
92 | Assert(pszString);
|
---|
93 | Assert(puiSize > 0);
|
---|
94 |
|
---|
95 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
96 | This information is language and code page independent. */
|
---|
97 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
98 | dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
|
---|
99 |
|
---|
100 | if (!dwLen)
|
---|
101 | {
|
---|
102 | VBoxServiceError("No file information found! File = %s, Error: %ld\n", pszFileName, GetLastError());
|
---|
103 | return FALSE;
|
---|
104 | }
|
---|
105 |
|
---|
106 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
107 | if (!lpData)
|
---|
108 | {
|
---|
109 | VBoxServiceError("Could not allocate temp buffer!\n");
|
---|
110 | return FALSE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
|
---|
114 | {
|
---|
115 | if((bRet = VerQueryValue(lpData, pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
|
---|
116 | {
|
---|
117 | UINT uiSize = uiValueLen * sizeof(char);
|
---|
118 |
|
---|
119 | if(uiSize > *puiSize)
|
---|
120 | uiSize = *puiSize;
|
---|
121 |
|
---|
122 | ZeroMemory(pszString, *puiSize);
|
---|
123 | memcpy(pszString, lpValue, uiSize);
|
---|
124 | }
|
---|
125 | else VBoxServiceError("Could not query value!\n");
|
---|
126 | }
|
---|
127 | else VBoxServiceError("Could not get file version info!\n");
|
---|
128 |
|
---|
129 | RTMemFree(lpData);
|
---|
130 | return bRet;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | BOOL VBoxServiceGetFileVersion(const char* pszFileName,
|
---|
135 | DWORD* pdwMajor,
|
---|
136 | DWORD* pdwMinor,
|
---|
137 | DWORD* pdwBuildNumber,
|
---|
138 | DWORD* pdwRevisionNumber)
|
---|
139 | {
|
---|
140 | DWORD dwHandle, dwLen = 0;
|
---|
141 | UINT BufLen = 0;
|
---|
142 | LPTSTR lpData = NULL;
|
---|
143 | BOOL bRet = FALSE;
|
---|
144 |
|
---|
145 | Assert(pszFileName);
|
---|
146 | Assert(pdwMajor);
|
---|
147 | Assert(pdwMinor);
|
---|
148 | Assert(pdwBuildNumber);
|
---|
149 | Assert(pdwRevisionNumber);
|
---|
150 |
|
---|
151 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
152 | This information is language and code page independent. */
|
---|
153 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
154 | dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
|
---|
155 |
|
---|
156 | /* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
|
---|
157 | char szValue[_MAX_PATH] = {0};
|
---|
158 | char *pszValue = szValue;
|
---|
159 | UINT uiSize = _MAX_PATH;
|
---|
160 | int r = 0;
|
---|
161 |
|
---|
162 | bRet = VBoxServiceGetFileString(pszFileName, "\\StringFileInfo\\040904b0\\FileVersion", szValue, &uiSize);
|
---|
163 | if (bRet)
|
---|
164 | {
|
---|
165 | sscanf(pszValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber);
|
---|
166 | }
|
---|
167 | else if (dwLen > 0)
|
---|
168 | {
|
---|
169 | /* Try regular fields - this maybe is not file provided by VBox! */
|
---|
170 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
171 | if (!lpData)
|
---|
172 | {
|
---|
173 | VBoxServiceError("Could not allocate temp buffer!\n");
|
---|
174 | return FALSE;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
|
---|
178 | {
|
---|
179 | if((bRet = VerQueryValue(lpData, "\\", (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
|
---|
180 | {
|
---|
181 | *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
|
---|
182 | *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
|
---|
183 | *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
|
---|
184 | *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
|
---|
185 | }
|
---|
186 | else VBoxServiceError("Could not query file information value!\n");
|
---|
187 | }
|
---|
188 | else VBoxServiceError("Could not get file version info!\n");
|
---|
189 |
|
---|
190 | RTMemFree(lpData);
|
---|
191 | }
|
---|
192 | return bRet;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | BOOL VBoxServiceGetFileVersionString(const char* pszPath, const char* pszFileName, char* pszVersion, UINT uiSize)
|
---|
197 | {
|
---|
198 | BOOL bRet = FALSE;
|
---|
199 | char szFullPath[_MAX_PATH] = {0};
|
---|
200 | char szValue[_MAX_PATH] = {0};
|
---|
201 | int r = 0;
|
---|
202 |
|
---|
203 | RTStrPrintf(szFullPath, 4096, "%s\\%s", pszPath, pszFileName);
|
---|
204 |
|
---|
205 | DWORD dwMajor, dwMinor, dwBuild, dwRev;
|
---|
206 |
|
---|
207 | bRet = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
|
---|
208 | if (bRet)
|
---|
209 | RTStrPrintf(pszVersion, uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
|
---|
210 | else
|
---|
211 | RTStrPrintf(pszVersion, uiSize, "-");
|
---|
212 |
|
---|
213 | return bRet;
|
---|
214 | }
|
---|
215 | #endif /* !RT_OS_WINDOWS */
|
---|
216 |
|
---|