1 |
|
---|
2 | /* $Id: VBoxUtil.cpp 33909 2008-07-31 09:30:59Z andy $ */
|
---|
3 | /** @file
|
---|
4 | * VBoxUtil - Some tool functions.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "VBoxService.h"
|
---|
24 | #include "VBoxUtils.h"
|
---|
25 |
|
---|
26 | BOOL vboxGetFileVersion (LPCWSTR a_pszFileName,
|
---|
27 | DWORD* a_pdwMajor, DWORD* a_pdwMinor, DWORD* a_pdwBuildNumber, DWORD* a_pdwRevisionNumber)
|
---|
28 | {
|
---|
29 | DWORD dwHandle, dwLen = 0;
|
---|
30 | UINT BufLen = 0;
|
---|
31 | LPTSTR lpData = NULL;
|
---|
32 | BOOL bRet = FALSE;
|
---|
33 |
|
---|
34 | Assert(a_pszFileName);
|
---|
35 | Assert(a_pdwMajor);
|
---|
36 | Assert(a_pdwMinor);
|
---|
37 | Assert(a_pdwBuildNumber);
|
---|
38 | Assert(a_pdwRevisionNumber);
|
---|
39 |
|
---|
40 | Log(("VBoxService: vboxGetFileVersionString: File = %ls\n", a_pszFileName));
|
---|
41 |
|
---|
42 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
43 | This information is language and code page independent. */
|
---|
44 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
45 | dwLen = GetFileVersionInfoSize(a_pszFileName, &dwHandle);
|
---|
46 |
|
---|
47 | Log(("VBoxService: vboxGetFileVersion: File version info size = %ld\n", dwLen));
|
---|
48 |
|
---|
49 | /* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
|
---|
50 | TCHAR szValueUTF16[_MAX_PATH] = {0};
|
---|
51 | char szValueUTF8[_MAX_PATH] = {0};
|
---|
52 | char *pszValueUTF8 = szValueUTF8;
|
---|
53 | UINT uiSize = _MAX_PATH;
|
---|
54 | int r = 0;
|
---|
55 |
|
---|
56 | bRet = vboxGetFileString(a_pszFileName, TEXT("\\StringFileInfo\\040904b0\\FileVersion"), szValueUTF16, &uiSize);
|
---|
57 | if (bRet)
|
---|
58 | {
|
---|
59 | r = RTUtf16ToUtf8Ex(szValueUTF16, uiSize, &pszValueUTF8, _MAX_PATH, NULL);
|
---|
60 | sscanf(szValueUTF8, "%ld.%ld.%ld.%ld", a_pdwMajor, a_pdwMinor, a_pdwBuildNumber, a_pdwRevisionNumber);
|
---|
61 | }
|
---|
62 | else if (dwLen > 0)
|
---|
63 | {
|
---|
64 | /* Try regular fields - this maybe is not file provided by VBox! */
|
---|
65 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
66 | if (!lpData)
|
---|
67 | {
|
---|
68 | Log(("VBoxService: vboxGetFileVersion: Could not allocate temp buffer!\n"));
|
---|
69 | return FALSE;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (GetFileVersionInfo(a_pszFileName, dwHandle, dwLen, lpData))
|
---|
73 | {
|
---|
74 | if((bRet = VerQueryValue(lpData, TEXT("\\"), (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
|
---|
75 | {
|
---|
76 | *a_pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
|
---|
77 | *a_pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
|
---|
78 | *a_pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
|
---|
79 | *a_pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
|
---|
80 | }
|
---|
81 | else Log(("VBoxService: vboxGetFileVersion: Could not query value!\n"));
|
---|
82 | }
|
---|
83 | else Log(("VBoxService: vboxGetFileVersion: Could not get file version info!\n"));
|
---|
84 |
|
---|
85 | RTMemFree(lpData);
|
---|
86 | }
|
---|
87 | return bRet;
|
---|
88 | }
|
---|
89 |
|
---|
90 | BOOL vboxGetFileString (LPCWSTR a_pszFileName, LPWSTR a_pszBlock, LPWSTR a_pszString, PUINT a_puiSize)
|
---|
91 | {
|
---|
92 | DWORD dwHandle, dwLen = 0;
|
---|
93 | UINT uiDataLen = 0;
|
---|
94 | LPTSTR lpData = NULL;
|
---|
95 | UINT uiValueLen = 0;
|
---|
96 | LPTSTR lpValue = NULL;
|
---|
97 | BOOL bRet = FALSE;
|
---|
98 |
|
---|
99 | Assert(a_pszFileName);
|
---|
100 | Assert(a_pszBlock);
|
---|
101 | Assert(a_pszString);
|
---|
102 | Assert(a_puiSize > 0);
|
---|
103 |
|
---|
104 | Log(("VBoxService: vboxGetFileString: File = %ls\n", a_pszFileName));
|
---|
105 |
|
---|
106 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
107 | This information is language and code page independent. */
|
---|
108 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
109 | dwLen = GetFileVersionInfoSize(a_pszFileName, &dwHandle);
|
---|
110 |
|
---|
111 | if (!dwLen)
|
---|
112 | {
|
---|
113 | Log(("VBoxService: vboxGetFileString: No file information found! File = %ls, Error: %ld\n", a_pszFileName, GetLastError()));
|
---|
114 | return FALSE; /* No version information available. */
|
---|
115 | }
|
---|
116 |
|
---|
117 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
118 | if (!lpData)
|
---|
119 | {
|
---|
120 | Log(("VBoxService: vboxGetFileString: Could not allocate temp buffer!\n"));
|
---|
121 | return FALSE;
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (GetFileVersionInfo(a_pszFileName, dwHandle, dwLen, lpData))
|
---|
125 | {
|
---|
126 | if((bRet = VerQueryValue(lpData, a_pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
|
---|
127 | {
|
---|
128 | UINT uiSize = uiValueLen * sizeof(TCHAR);
|
---|
129 |
|
---|
130 | if(uiSize > *a_puiSize)
|
---|
131 | uiSize = *a_puiSize;
|
---|
132 |
|
---|
133 | ZeroMemory(a_pszString, *a_puiSize);
|
---|
134 | memcpy(a_pszString, lpValue, uiSize);
|
---|
135 |
|
---|
136 | Log(("VBoxService: vboxGetFileString: Block = %ls, Size = %d, Value = %ls\n", a_pszBlock, uiValueLen, a_pszString));
|
---|
137 | }
|
---|
138 | else Log(("VBoxService: vboxGetFileString: Could not query value!\n"));
|
---|
139 | }
|
---|
140 | else Log(("VBoxService: vboxGetFileString: Could not get file version info!\n"));
|
---|
141 |
|
---|
142 | RTMemFree(lpData);
|
---|
143 | return bRet;
|
---|
144 | }
|
---|
145 |
|
---|
146 | BOOL vboxGetFileVersionString (LPCWSTR a_pszPath, LPCWSTR a_pszFileName, char* a_pszVersion, UINT a_uiSize)
|
---|
147 | {
|
---|
148 | BOOL bRet = FALSE;
|
---|
149 | TCHAR szFullPath[4096] = {0};
|
---|
150 | TCHAR szValueUTF16[_MAX_PATH] = {0};
|
---|
151 | char szValueUTF8[_MAX_PATH] = {0};
|
---|
152 | UINT uiSize = _MAX_PATH;
|
---|
153 | int r = 0;
|
---|
154 |
|
---|
155 | swprintf(szFullPath, 4096, TEXT("%s\\%s"), a_pszPath, a_pszFileName);
|
---|
156 |
|
---|
157 | DWORD dwMajor, dwMinor, dwBuild, dwRev;
|
---|
158 |
|
---|
159 | bRet = vboxGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
|
---|
160 | if (bRet)
|
---|
161 | RTStrPrintf(a_pszVersion, a_uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
|
---|
162 | else
|
---|
163 | RTStrPrintf(a_pszVersion, a_uiSize, "-");
|
---|
164 |
|
---|
165 | return bRet;
|
---|
166 | }
|
---|