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_path.h"
10 #include "base/file_version_info.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/threading/thread_restrictions.h"
15 FileVersionInfoWin::FileVersionInfoWin(void* data
, int language
, int code_page
)
16 : language_(language
), code_page_(code_page
) {
17 base::ThreadRestrictions::AssertIOAllowed();
18 data_
.reset((char*) data
);
19 fixed_file_info_
= NULL
;
21 ::VerQueryValue(data_
.get(), L
"\\", (LPVOID
*)&fixed_file_info_
, &size
);
24 FileVersionInfoWin::~FileVersionInfoWin() {
31 } LanguageAndCodePage
;
34 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfoForModule(
36 // Note that the use of MAX_PATH is basically in line with what we do for
37 // all registered paths (PathProviderWin).
38 wchar_t system_buffer
[MAX_PATH
];
40 if (!GetModuleFileName(module
, system_buffer
, MAX_PATH
))
43 FilePath
app_path(system_buffer
);
44 return CreateFileVersionInfo(app_path
);
48 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfo(
49 const FilePath
& file_path
) {
50 base::ThreadRestrictions::AssertIOAllowed();
53 const wchar_t* path
= file_path
.value().c_str();
54 DWORD length
= ::GetFileVersionInfoSize(path
, &dummy
);
58 void* data
= calloc(length
, 1);
62 if (!::GetFileVersionInfo(path
, dummy
, length
, data
)) {
67 LanguageAndCodePage
* translate
= NULL
;
69 BOOL query_result
= VerQueryValue(data
, L
"\\VarFileInfo\\Translation",
70 (void**) &translate
, &page_count
);
72 if (query_result
&& translate
) {
73 return new FileVersionInfoWin(data
, translate
->language
,
74 translate
->code_page
);
82 string16
FileVersionInfoWin::company_name() {
83 return GetStringValue(L
"CompanyName");
86 string16
FileVersionInfoWin::company_short_name() {
87 return GetStringValue(L
"CompanyShortName");
90 string16
FileVersionInfoWin::internal_name() {
91 return GetStringValue(L
"InternalName");
94 string16
FileVersionInfoWin::product_name() {
95 return GetStringValue(L
"ProductName");
98 string16
FileVersionInfoWin::product_short_name() {
99 return GetStringValue(L
"ProductShortName");
102 string16
FileVersionInfoWin::comments() {
103 return GetStringValue(L
"Comments");
106 string16
FileVersionInfoWin::legal_copyright() {
107 return GetStringValue(L
"LegalCopyright");
110 string16
FileVersionInfoWin::product_version() {
111 return GetStringValue(L
"ProductVersion");
114 string16
FileVersionInfoWin::file_description() {
115 return GetStringValue(L
"FileDescription");
118 string16
FileVersionInfoWin::legal_trademarks() {
119 return GetStringValue(L
"LegalTrademarks");
122 string16
FileVersionInfoWin::private_build() {
123 return GetStringValue(L
"PrivateBuild");
126 string16
FileVersionInfoWin::file_version() {
127 return GetStringValue(L
"FileVersion");
130 string16
FileVersionInfoWin::original_filename() {
131 return GetStringValue(L
"OriginalFilename");
134 string16
FileVersionInfoWin::special_build() {
135 return GetStringValue(L
"SpecialBuild");
138 string16
FileVersionInfoWin::last_change() {
139 return GetStringValue(L
"LastChange");
142 bool FileVersionInfoWin::is_official_build() {
143 return (GetStringValue(L
"Official Build").compare(L
"1") == 0);
146 bool FileVersionInfoWin::GetValue(const wchar_t* name
,
147 std::wstring
* value_str
) {
148 WORD lang_codepage
[8];
150 // Use the language and codepage from the DLL.
151 lang_codepage
[i
++] = language_
;
152 lang_codepage
[i
++] = code_page_
;
153 // Use the default language and codepage from the DLL.
154 lang_codepage
[i
++] = ::GetUserDefaultLangID();
155 lang_codepage
[i
++] = code_page_
;
156 // Use the language from the DLL and Latin codepage (most common).
157 lang_codepage
[i
++] = language_
;
158 lang_codepage
[i
++] = 1252;
159 // Use the default language and Latin codepage (most common).
160 lang_codepage
[i
++] = ::GetUserDefaultLangID();
161 lang_codepage
[i
++] = 1252;
164 while (i
< arraysize(lang_codepage
)) {
165 wchar_t sub_block
[MAX_PATH
];
166 WORD language
= lang_codepage
[i
++];
167 WORD code_page
= lang_codepage
[i
++];
168 _snwprintf_s(sub_block
, MAX_PATH
, MAX_PATH
,
169 L
"\\StringFileInfo\\%04x%04x\\%ls", language
, code_page
, name
);
172 BOOL r
= ::VerQueryValue(data_
.get(), sub_block
, &value
, &size
);
174 value_str
->assign(static_cast<wchar_t*>(value
));
181 std::wstring
FileVersionInfoWin::GetStringValue(const wchar_t* name
) {
183 if (GetValue(name
, &str
))