1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/file_version_info_win.h"
9 #include "base/file_version_info.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/threading/thread_restrictions.h"
17 FileVersionInfoWin::FileVersionInfoWin(void* data
,
20 : language_(language
), code_page_(code_page
) {
21 base::ThreadRestrictions::AssertIOAllowed();
22 data_
.reset((char*) data
);
23 fixed_file_info_
= NULL
;
25 ::VerQueryValue(data_
.get(), L
"\\", (LPVOID
*)&fixed_file_info_
, &size
);
28 FileVersionInfoWin::~FileVersionInfoWin() {
35 } LanguageAndCodePage
;
38 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfoForModule(
40 // Note that the use of MAX_PATH is basically in line with what we do for
41 // all registered paths (PathProviderWin).
42 wchar_t system_buffer
[MAX_PATH
];
44 if (!GetModuleFileName(module
, system_buffer
, MAX_PATH
))
47 FilePath
app_path(system_buffer
);
48 return CreateFileVersionInfo(app_path
);
52 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfo(
53 const FilePath
& file_path
) {
54 base::ThreadRestrictions::AssertIOAllowed();
57 const wchar_t* path
= file_path
.value().c_str();
58 DWORD length
= ::GetFileVersionInfoSize(path
, &dummy
);
62 void* data
= calloc(length
, 1);
66 if (!::GetFileVersionInfo(path
, dummy
, length
, data
)) {
71 LanguageAndCodePage
* translate
= NULL
;
73 BOOL query_result
= VerQueryValue(data
, L
"\\VarFileInfo\\Translation",
74 (void**) &translate
, &page_count
);
76 if (query_result
&& translate
) {
77 return new FileVersionInfoWin(data
, translate
->language
,
78 translate
->code_page
);
86 base::string16
FileVersionInfoWin::company_name() {
87 return GetStringValue(L
"CompanyName");
90 base::string16
FileVersionInfoWin::company_short_name() {
91 return GetStringValue(L
"CompanyShortName");
94 base::string16
FileVersionInfoWin::internal_name() {
95 return GetStringValue(L
"InternalName");
98 base::string16
FileVersionInfoWin::product_name() {
99 return GetStringValue(L
"ProductName");
102 base::string16
FileVersionInfoWin::product_short_name() {
103 return GetStringValue(L
"ProductShortName");
106 base::string16
FileVersionInfoWin::comments() {
107 return GetStringValue(L
"Comments");
110 base::string16
FileVersionInfoWin::legal_copyright() {
111 return GetStringValue(L
"LegalCopyright");
114 base::string16
FileVersionInfoWin::product_version() {
115 return GetStringValue(L
"ProductVersion");
118 base::string16
FileVersionInfoWin::file_description() {
119 return GetStringValue(L
"FileDescription");
122 base::string16
FileVersionInfoWin::legal_trademarks() {
123 return GetStringValue(L
"LegalTrademarks");
126 base::string16
FileVersionInfoWin::private_build() {
127 return GetStringValue(L
"PrivateBuild");
130 base::string16
FileVersionInfoWin::file_version() {
131 return GetStringValue(L
"FileVersion");
134 base::string16
FileVersionInfoWin::original_filename() {
135 return GetStringValue(L
"OriginalFilename");
138 base::string16
FileVersionInfoWin::special_build() {
139 return GetStringValue(L
"SpecialBuild");
142 base::string16
FileVersionInfoWin::last_change() {
143 return GetStringValue(L
"LastChange");
146 bool FileVersionInfoWin::is_official_build() {
147 return (GetStringValue(L
"Official Build").compare(L
"1") == 0);
150 bool FileVersionInfoWin::GetValue(const wchar_t* name
,
151 std::wstring
* value_str
) {
152 WORD lang_codepage
[8];
154 // Use the language and codepage from the DLL.
155 lang_codepage
[i
++] = language_
;
156 lang_codepage
[i
++] = code_page_
;
157 // Use the default language and codepage from the DLL.
158 lang_codepage
[i
++] = ::GetUserDefaultLangID();
159 lang_codepage
[i
++] = code_page_
;
160 // Use the language from the DLL and Latin codepage (most common).
161 lang_codepage
[i
++] = language_
;
162 lang_codepage
[i
++] = 1252;
163 // Use the default language and Latin codepage (most common).
164 lang_codepage
[i
++] = ::GetUserDefaultLangID();
165 lang_codepage
[i
++] = 1252;
168 while (i
< arraysize(lang_codepage
)) {
169 wchar_t sub_block
[MAX_PATH
];
170 WORD language
= lang_codepage
[i
++];
171 WORD code_page
= lang_codepage
[i
++];
172 _snwprintf_s(sub_block
, MAX_PATH
, MAX_PATH
,
173 L
"\\StringFileInfo\\%04x%04x\\%ls", language
, code_page
, name
);
176 BOOL r
= ::VerQueryValue(data_
.get(), sub_block
, &value
, &size
);
178 value_str
->assign(static_cast<wchar_t*>(value
));
185 std::wstring
FileVersionInfoWin::GetStringValue(const wchar_t* name
) {
187 if (GetValue(name
, &str
))