1 | /* $Id: VBoxServiceUtils.cpp 23575 2009-10-06 08:23:38Z 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 | int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
|
---|
40 | {
|
---|
41 | char *pszNameUTF8;
|
---|
42 | int rc = RTStrCurrentCPToUtf8(&pszNameUTF8, pszName);
|
---|
43 | if (RT_SUCCESS(rc))
|
---|
44 | {
|
---|
45 | if (pszValueFormat != NULL)
|
---|
46 | {
|
---|
47 | va_list va;
|
---|
48 | va_start(va, pszValueFormat);
|
---|
49 | VBoxServiceVerbose(3, "Writing guest property \"%s\"=\"%s\"\n", pszNameUTF8, pszValueFormat);
|
---|
50 | rc = VbglR3GuestPropWriteValueV(u32ClientId, pszNameUTF8, pszValueFormat, va);
|
---|
51 | if(RT_FAILURE(rc))
|
---|
52 | VBoxServiceError("Error writing guest property \"%s\"=\"%s\" (rc=%Rrc)\n", pszNameUTF8, pszValueFormat, rc);
|
---|
53 | va_end(va);
|
---|
54 | }
|
---|
55 | else
|
---|
56 | rc = VbglR3GuestPropWriteValue(u32ClientId, pszNameUTF8, NULL);
|
---|
57 | RTStrFree(pszNameUTF8);
|
---|
58 | }
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 | #endif /* VBOX_WITH_GUEST_PROPS */
|
---|
62 |
|
---|
63 |
|
---|
64 | #ifdef RT_OS_WINDOWS
|
---|
65 | BOOL VBoxServiceGetFileString(const char* pszFileName,
|
---|
66 | char* pszBlock,
|
---|
67 | char* pszString,
|
---|
68 | PUINT puiSize)
|
---|
69 | {
|
---|
70 | DWORD dwHandle, dwLen = 0;
|
---|
71 | UINT uiDataLen = 0;
|
---|
72 | char* lpData = NULL;
|
---|
73 | UINT uiValueLen = 0;
|
---|
74 | LPTSTR lpValue = NULL;
|
---|
75 | BOOL bRet = FALSE;
|
---|
76 |
|
---|
77 | Assert(pszFileName);
|
---|
78 | Assert(pszBlock);
|
---|
79 | Assert(pszString);
|
---|
80 | Assert(puiSize > 0);
|
---|
81 |
|
---|
82 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
83 | This information is language and code page independent. */
|
---|
84 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
85 | dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
|
---|
86 |
|
---|
87 | if (!dwLen)
|
---|
88 | {
|
---|
89 | VBoxServiceError("No file information found! File = %s, Error: %ld\n", pszFileName, GetLastError());
|
---|
90 | return FALSE;
|
---|
91 | }
|
---|
92 |
|
---|
93 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
94 | if (!lpData)
|
---|
95 | {
|
---|
96 | VBoxServiceError("Could not allocate temp buffer!\n");
|
---|
97 | return FALSE;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
|
---|
101 | {
|
---|
102 | if((bRet = VerQueryValue(lpData, pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
|
---|
103 | {
|
---|
104 | UINT uiSize = uiValueLen * sizeof(char);
|
---|
105 |
|
---|
106 | if(uiSize > *puiSize)
|
---|
107 | uiSize = *puiSize;
|
---|
108 |
|
---|
109 | ZeroMemory(pszString, *puiSize);
|
---|
110 | memcpy(pszString, lpValue, uiSize);
|
---|
111 | }
|
---|
112 | else VBoxServiceError("Could not query value!\n");
|
---|
113 | }
|
---|
114 | else VBoxServiceError("Could not get file version info!\n");
|
---|
115 |
|
---|
116 | RTMemFree(lpData);
|
---|
117 | return bRet;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | BOOL VBoxServiceGetFileVersion(const char* pszFileName,
|
---|
122 | DWORD* pdwMajor,
|
---|
123 | DWORD* pdwMinor,
|
---|
124 | DWORD* pdwBuildNumber,
|
---|
125 | DWORD* pdwRevisionNumber)
|
---|
126 | {
|
---|
127 | DWORD dwHandle, dwLen = 0;
|
---|
128 | UINT BufLen = 0;
|
---|
129 | LPTSTR lpData = NULL;
|
---|
130 | BOOL bRet = FALSE;
|
---|
131 |
|
---|
132 | Assert(pszFileName);
|
---|
133 | Assert(pdwMajor);
|
---|
134 | Assert(pdwMinor);
|
---|
135 | Assert(pdwBuildNumber);
|
---|
136 | Assert(pdwRevisionNumber);
|
---|
137 |
|
---|
138 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
139 | This information is language and code page independent. */
|
---|
140 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
141 | dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
|
---|
142 |
|
---|
143 | /* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
|
---|
144 | char szValue[_MAX_PATH] = {0};
|
---|
145 | char *pszValue = szValue;
|
---|
146 | UINT uiSize = _MAX_PATH;
|
---|
147 | int r = 0;
|
---|
148 |
|
---|
149 | bRet = VBoxServiceGetFileString(pszFileName, "\\StringFileInfo\\040904b0\\FileVersion", szValue, &uiSize);
|
---|
150 | if (bRet)
|
---|
151 | {
|
---|
152 | sscanf(pszValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber);
|
---|
153 | }
|
---|
154 | else if (dwLen > 0)
|
---|
155 | {
|
---|
156 | /* Try regular fields - this maybe is not file provided by VBox! */
|
---|
157 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
158 | if (!lpData)
|
---|
159 | {
|
---|
160 | VBoxServiceError("Could not allocate temp buffer!\n");
|
---|
161 | return FALSE;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
|
---|
165 | {
|
---|
166 | if((bRet = VerQueryValue(lpData, "\\", (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
|
---|
167 | {
|
---|
168 | *pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
|
---|
169 | *pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
|
---|
170 | *pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
|
---|
171 | *pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
|
---|
172 | }
|
---|
173 | else VBoxServiceError("Could not query file information value!\n");
|
---|
174 | }
|
---|
175 | else VBoxServiceError("Could not get file version info!\n");
|
---|
176 |
|
---|
177 | RTMemFree(lpData);
|
---|
178 | }
|
---|
179 | return bRet;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | BOOL VBoxServiceGetFileVersionString(const char* pszPath, const char* pszFileName, char* pszVersion, UINT uiSize)
|
---|
184 | {
|
---|
185 | BOOL bRet = FALSE;
|
---|
186 | char szFullPath[_MAX_PATH] = {0};
|
---|
187 | char szValue[_MAX_PATH] = {0};
|
---|
188 | int r = 0;
|
---|
189 |
|
---|
190 | RTStrPrintf(szFullPath, 4096, "%s\\%s", pszPath, pszFileName);
|
---|
191 |
|
---|
192 | DWORD dwMajor, dwMinor, dwBuild, dwRev;
|
---|
193 |
|
---|
194 | bRet = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
|
---|
195 | if (bRet)
|
---|
196 | RTStrPrintf(pszVersion, uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
|
---|
197 | else
|
---|
198 | RTStrPrintf(pszVersion, uiSize, "-");
|
---|
199 |
|
---|
200 | return bRet;
|
---|
201 | }
|
---|
202 | #endif /* !RT_OS_WINDOWS */
|
---|
203 |
|
---|