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 | * Sun Microsystems, Inc. confidential
|
---|
11 | * All rights reserved
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include "VBoxService.h"
|
---|
15 | #include "VBoxUtils.h"
|
---|
16 |
|
---|
17 | BOOL vboxGetFileVersion (LPCWSTR a_pszFileName,
|
---|
18 | DWORD* a_pdwMajor, DWORD* a_pdwMinor, DWORD* a_pdwBuildNumber, DWORD* a_pdwRevisionNumber)
|
---|
19 | {
|
---|
20 | DWORD dwHandle, dwLen = 0;
|
---|
21 | UINT BufLen = 0;
|
---|
22 | LPTSTR lpData = NULL;
|
---|
23 | BOOL bRet = FALSE;
|
---|
24 |
|
---|
25 | Assert(a_pszFileName);
|
---|
26 | Assert(a_pdwMajor);
|
---|
27 | Assert(a_pdwMinor);
|
---|
28 | Assert(a_pdwBuildNumber);
|
---|
29 | Assert(a_pdwRevisionNumber);
|
---|
30 |
|
---|
31 | Log(("VBoxService: vboxGetFileVersionString: File = %ls\n", a_pszFileName));
|
---|
32 |
|
---|
33 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
34 | This information is language and code page independent. */
|
---|
35 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
36 | dwLen = GetFileVersionInfoSize(a_pszFileName, &dwHandle);
|
---|
37 |
|
---|
38 | Log(("VBoxService: vboxGetFileVersion: File version info size = %ld\n", dwLen));
|
---|
39 |
|
---|
40 | /* Try own fields defined in block "\\StringFileInfo\\040904b0\\FileVersion". */
|
---|
41 | TCHAR szValueUTF16[_MAX_PATH] = {0};
|
---|
42 | char szValueUTF8[_MAX_PATH] = {0};
|
---|
43 | char *pszValueUTF8 = szValueUTF8;
|
---|
44 | UINT uiSize = _MAX_PATH;
|
---|
45 | int r = 0;
|
---|
46 |
|
---|
47 | bRet = vboxGetFileString(a_pszFileName, TEXT("\\StringFileInfo\\040904b0\\FileVersion"), szValueUTF16, &uiSize);
|
---|
48 | if (bRet)
|
---|
49 | {
|
---|
50 | r = RTUtf16ToUtf8Ex(szValueUTF16, uiSize, &pszValueUTF8, _MAX_PATH, NULL);
|
---|
51 | sscanf(szValueUTF8, "%ld.%ld.%ld.%ld", a_pdwMajor, a_pdwMinor, a_pdwBuildNumber, a_pdwRevisionNumber);
|
---|
52 | }
|
---|
53 | else if (dwLen > 0)
|
---|
54 | {
|
---|
55 | /* Try regular fields - this maybe is not file provided by VBox! */
|
---|
56 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
57 | if (!lpData)
|
---|
58 | {
|
---|
59 | Log(("VBoxService: vboxGetFileVersion: Could not allocate temp buffer!\n"));
|
---|
60 | return FALSE;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (GetFileVersionInfo(a_pszFileName, dwHandle, dwLen, lpData))
|
---|
64 | {
|
---|
65 | if((bRet = VerQueryValue(lpData, TEXT("\\"), (LPVOID*)&pFileInfo, (PUINT)&BufLen)))
|
---|
66 | {
|
---|
67 | *a_pdwMajor = HIWORD(pFileInfo->dwFileVersionMS);
|
---|
68 | *a_pdwMinor = LOWORD(pFileInfo->dwFileVersionMS);
|
---|
69 | *a_pdwBuildNumber = HIWORD(pFileInfo->dwFileVersionLS);
|
---|
70 | *a_pdwRevisionNumber = LOWORD(pFileInfo->dwFileVersionLS);
|
---|
71 | }
|
---|
72 | else Log(("VBoxService: vboxGetFileVersion: Could not query value!\n"));
|
---|
73 | }
|
---|
74 | else Log(("VBoxService: vboxGetFileVersion: Could not get file version info!\n"));
|
---|
75 |
|
---|
76 | RTMemFree(lpData);
|
---|
77 | }
|
---|
78 | return bRet;
|
---|
79 | }
|
---|
80 |
|
---|
81 | BOOL vboxGetFileString (LPCWSTR a_pszFileName, LPWSTR a_pszBlock, LPWSTR a_pszString, PUINT a_puiSize)
|
---|
82 | {
|
---|
83 | DWORD dwHandle, dwLen = 0;
|
---|
84 | UINT uiDataLen = 0;
|
---|
85 | LPTSTR lpData = NULL;
|
---|
86 | UINT uiValueLen = 0;
|
---|
87 | LPTSTR lpValue = NULL;
|
---|
88 | BOOL bRet = FALSE;
|
---|
89 |
|
---|
90 | Assert(a_pszFileName);
|
---|
91 | Assert(a_pszBlock);
|
---|
92 | Assert(a_pszString);
|
---|
93 | Assert(a_puiSize > 0);
|
---|
94 |
|
---|
95 | Log(("VBoxService: vboxGetFileString: File = %ls\n", a_pszFileName));
|
---|
96 |
|
---|
97 | /* The VS_FIXEDFILEINFO structure contains version information about a file.
|
---|
98 | This information is language and code page independent. */
|
---|
99 | VS_FIXEDFILEINFO *pFileInfo = NULL;
|
---|
100 | dwLen = GetFileVersionInfoSize(a_pszFileName, &dwHandle);
|
---|
101 |
|
---|
102 | if (!dwLen)
|
---|
103 | {
|
---|
104 | Log(("VBoxService: vboxGetFileString: No file information found! File = %ls, Error: %ld\n", a_pszFileName, GetLastError()));
|
---|
105 | return FALSE; /* No version information available. */
|
---|
106 | }
|
---|
107 |
|
---|
108 | lpData = (LPTSTR) RTMemTmpAlloc(dwLen);
|
---|
109 | if (!lpData)
|
---|
110 | {
|
---|
111 | Log(("VBoxService: vboxGetFileString: Could not allocate temp buffer!\n"));
|
---|
112 | return FALSE;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (GetFileVersionInfo(a_pszFileName, dwHandle, dwLen, lpData))
|
---|
116 | {
|
---|
117 | if((bRet = VerQueryValue(lpData, a_pszBlock, (LPVOID*)&lpValue, (PUINT)&uiValueLen)))
|
---|
118 | {
|
---|
119 | UINT uiSize = uiValueLen * sizeof(TCHAR);
|
---|
120 |
|
---|
121 | if(uiSize > *a_puiSize)
|
---|
122 | uiSize = *a_puiSize;
|
---|
123 |
|
---|
124 | ZeroMemory(a_pszString, *a_puiSize);
|
---|
125 | memcpy(a_pszString, lpValue, uiSize);
|
---|
126 |
|
---|
127 | Log(("VBoxService: vboxGetFileString: Block = %ls, Size = %d, Value = %ls\n", a_pszBlock, uiValueLen, a_pszString));
|
---|
128 | }
|
---|
129 | else Log(("VBoxService: vboxGetFileString: Could not query value!\n"));
|
---|
130 | }
|
---|
131 | else Log(("VBoxService: vboxGetFileString: Could not get file version info!\n"));
|
---|
132 |
|
---|
133 | RTMemFree(lpData);
|
---|
134 | return bRet;
|
---|
135 | }
|
---|
136 |
|
---|
137 | BOOL vboxGetFileVersionString (LPCWSTR a_pszPath, LPCWSTR a_pszFileName, char* a_pszVersion, UINT a_uiSize)
|
---|
138 | {
|
---|
139 | BOOL bRet = FALSE;
|
---|
140 | TCHAR szFullPath[4096] = {0};
|
---|
141 | TCHAR szValueUTF16[_MAX_PATH] = {0};
|
---|
142 | char szValueUTF8[_MAX_PATH] = {0};
|
---|
143 | UINT uiSize = _MAX_PATH;
|
---|
144 | int r = 0;
|
---|
145 |
|
---|
146 | swprintf(szFullPath, 4096, TEXT("%s\\%s"), a_pszPath, a_pszFileName);
|
---|
147 |
|
---|
148 | DWORD dwMajor, dwMinor, dwBuild, dwRev;
|
---|
149 |
|
---|
150 | bRet = vboxGetFileVersion(szFullPath, &dwMajor, &dwMinor, &dwBuild, &dwRev);
|
---|
151 | if (bRet)
|
---|
152 | RTStrPrintf(a_pszVersion, a_uiSize, "%ld.%ld.%ldr%ld", dwMajor, dwMinor, dwBuild, dwRev);
|
---|
153 | else
|
---|
154 | RTStrPrintf(a_pszVersion, a_uiSize, "-");
|
---|
155 |
|
---|
156 | return bRet;
|
---|
157 | }
|
---|