1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* $Id: VBoxServiceUtils.cpp $ */
/** @file
* VBoxServiceUtils - Some utility functions.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
#include <windows.h>
#endif
#include <iprt/assert.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <VBox/VBoxGuest.h>
#include "VBoxServiceInternal.h"
#ifdef VBOX_WITH_GUEST_PROPS
int VBoxServiceWritePropF(uint32_t u32ClientId, const char *pszName, const char *pszValueFormat, ...)
{
int rc;
if (pszValueFormat != NULL)
{
VBoxServiceVerbose(3, "Writing guest property \"%s\"\n", pszName);
va_list va;
va_start(va, pszValueFormat);
rc = VbglR3GuestPropWriteValueV(u32ClientId, pszName, pszValueFormat, va);
va_end(va);
if (RT_FAILURE(rc))
VBoxServiceError("Error writing guest property \"%s\" (rc=%Rrc)\n", pszName, rc);
}
else
rc = VbglR3GuestPropWriteValue(u32ClientId, pszName, NULL);
return rc;
}
#endif /* VBOX_WITH_GUEST_PROPS */
#ifdef RT_OS_WINDOWS
BOOL VBoxServiceGetFileString(const char* pszFileName,
char* pszBlock,
char* pszString,
PUINT puiSize)
{
DWORD dwHandle, dwLen = 0;
UINT uiDataLen = 0;
char* lpData = NULL;
UINT uiValueLen = 0;
LPTSTR lpValue = NULL;
BOOL bRet = FALSE;
Assert(pszFileName);
Assert(pszBlock);
Assert(pszString);
Assert(puiSize > 0);
/* The VS_FIXEDFILEINFO structure contains version information about a file.
This information is language and code page independent. */
VS_FIXEDFILEINFO *pFileInfo = NULL;
dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
if (!dwLen)
{
VBoxServiceError("No file information found! File = %s, Error: %ld\n", pszFileName, GetLastError());
return FALSE;
}
lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
if (!lpData)
{
VBoxServiceError("Could not allocate temp buffer!\n");
return FALSE;
}
if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
{
if((bRet = VerQueryValue(lpData, pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
{
UINT uiSize = uiValueLen * sizeof(char);
if(uiSize > *puiSize)
uiSize = *puiSize;
ZeroMemory(pszString, *puiSize);
memcpy(pszString, lpValue, uiSize);
}
else VBoxServiceError("Could not query value!\n");
}
else VBoxServiceError("Could not get file version info!\n");
RTMemFree(lpData);
return bRet;
}
BOOL VBoxServiceGetFileVersion(const char* pszFileName,
DWORD* pdwMajor,
DWORD* pdwMinor,
DWORD* pdwBuildNumber,
DWORD* pdwRevisionNumber)
{
DWORD dwHandle, dwLen = 0;
UINT BufLen = 0;
LPTSTR lpData = NULL;
BOOL bRet = FALSE;
Assert(pszFileName);
Assert(pdwMajor);
Assert(pdwMinor);
Assert(pdwBuildNumber);
Assert(pdwRevisionNumber);
/* The VS_FIXEDFILEINFO structure contains version information about a file.
This information is language and code page independent. */
VS_FIXEDFILEINFO *pFileInfo = NULL;
dwLen = GetFileVersionInfoSize(pszFileName, &dwHandle);
/* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
char szValue[_MAX_PATH] = {0};
char *pszValue = szValue;
UINT uiSize = _MAX_PATH;
int r = 0;
bRet = VBoxServiceGetFileString(pszFileName, "\\StringFileInfo\\040904b0\\FileVersion", szValue, &uiSize);
if (bRet)
{
sscanf(pszValue, "%ld.%ld.%ld.%ld", pdwMajor, pdwMinor, pdwBuildNumber, pdwRevisionNumber);
}
else if (dwLen > 0)
{
/* Try regular fields - this maybe is not file provided by VBox! */
lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
if (!lpData)
{
VBoxServiceError("Could not allocate temp buffer!\n");
return FALSE;
}
if (GetFileVersionInfo(pszFileName, dwHandle, dwLen, lpData))
{
if((bRet = VerQueryValue(lpData, "\\", (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
{
*pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
*pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
*pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
*pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
}
else VBoxServiceError("Could not query file information value!\n");
}
else VBoxServiceError("Could not get file version info!\n");
RTMemFree(lpData);
}
return bRet;
}
BOOL VBoxServiceGetFileVersionString(const char* pszPath, const char* pszFileName, char* pszVersion, UINT uiSize)
{
BOOL bRet = FALSE;
char szFullPath[_MAX_PATH] = {0};
char szValue[_MAX_PATH] = {0};
int r = 0;
RTStrPrintf(szFullPath, 4096, "%s\\%s", pszPath, pszFileName);
DWORD dwMajor, dwMinor, dwBuild, dwRev;
bRet = VBoxServiceGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
if (bRet)
RTStrPrintf(pszVersion, uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
else
RTStrPrintf(pszVersion, uiSize, "-");
return bRet;
}
#endif /* !RT_OS_WINDOWS */
|