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
, int language
, int code_page
)
18 : language_(language
), code_page_(code_page
) {
19 base::ThreadRestrictions::AssertIOAllowed();
20 data_
.reset((char*) data
);
21 fixed_file_info_
= NULL
;
23 ::VerQueryValue(data_
.get(), L
"\\", (LPVOID
*)&fixed_file_info_
, &size
);
26 FileVersionInfoWin::~FileVersionInfoWin() {
33 } LanguageAndCodePage
;
36 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfoForModule(
38 // Note that the use of MAX_PATH is basically in line with what we do for
39 // all registered paths (PathProviderWin).
40 wchar_t system_buffer
[MAX_PATH
];
42 if (!GetModuleFileName(module
, system_buffer
, MAX_PATH
))
45 FilePath
app_path(system_buffer
);
46 return CreateFileVersionInfo(app_path
);
50 FileVersionInfo
* FileVersionInfo::CreateFileVersionInfo(
51 const FilePath
& file_path
) {
52 base::ThreadRestrictions::AssertIOAllowed();
55 const wchar_t* path
= file_path
.value().c_str();
56 DWORD length
= ::GetFileVersionInfoSize(path
, &dummy
);
60 void* data
= calloc(length
, 1);
64 if (!::GetFileVersionInfo(path
, dummy
, length
, data
)) {
69 LanguageAndCodePage
* translate
= NULL
;
71 BOOL query_result
= VerQueryValue(data
, L
"\\VarFileInfo\\Translation",
72 (void**) &translate
, &page_count
);
74 if (query_result
&& translate
) {
75 return new FileVersionInfoWin(data
, translate
->language
,
76 translate
->code_page
);
84 string16
FileVersionInfoWin::company_name() {
85 return GetStringValue(L
"CompanyName");
88 string16
FileVersionInfoWin::company_short_name() {
89 return GetStringValue(L
"CompanyShortName");
92 string16
FileVersionInfoWin::internal_name() {
93 return GetStringValue(L
"InternalName");
96 string16
FileVersionInfoWin::product_name() {
97 return GetStringValue(L
"ProductName");
100 string16
FileVersionInfoWin::product_short_name() {
101 return GetStringValue(L
"ProductShortName");
104 string16
FileVersionInfoWin::comments() {
105 return GetStringValue(L
"Comments");
108 string16
FileVersionInfoWin::legal_copyright() {
109 return GetStringValue(L
"LegalCopyright");
112 string16
FileVersionInfoWin::product_version() {
113 return GetStringValue(L
"ProductVersion");
116 string16
FileVersionInfoWin::file_description() {
117 return GetStringValue(L
"FileDescription");
120 string16
FileVersionInfoWin::legal_trademarks() {
121 return GetStringValue(L
"LegalTrademarks");
124 string16
FileVersionInfoWin::private_build() {
125 return GetStringValue(L
"PrivateBuild");
128 string16
FileVersionInfoWin::file_version() {
129 return GetStringValue(L
"FileVersion");
132 string16
FileVersionInfoWin::original_filename() {
133 return GetStringValue(L
"OriginalFilename");
136 string16
FileVersionInfoWin::special_build() {
137 return GetStringValue(L
"SpecialBuild");
140 string16
FileVersionInfoWin::last_change() {
141 return GetStringValue(L
"LastChange");
144 bool FileVersionInfoWin::is_official_build() {
145 return (GetStringValue(L
"Official Build").compare(L
"1") == 0);
148 bool FileVersionInfoWin::GetValue(const wchar_t* name
,
149 std::wstring
* value_str
) {
150 WORD lang_codepage
[8];
152 // Use the language and codepage from the DLL.
153 lang_codepage
[i
++] = language_
;
154 lang_codepage
[i
++] = code_page_
;
155 // Use the default language and codepage from the DLL.
156 lang_codepage
[i
++] = ::GetUserDefaultLangID();
157 lang_codepage
[i
++] = code_page_
;
158 // Use the language from the DLL and Latin codepage (most common).
159 lang_codepage
[i
++] = language_
;
160 lang_codepage
[i
++] = 1252;
161 // Use the default language and Latin codepage (most common).
162 lang_codepage
[i
++] = ::GetUserDefaultLangID();
163 lang_codepage
[i
++] = 1252;
166 while (i
< arraysize(lang_codepage
)) {
167 wchar_t sub_block
[MAX_PATH
];
168 WORD language
= lang_codepage
[i
++];
169 WORD code_page
= lang_codepage
[i
++];
170 _snwprintf_s(sub_block
, MAX_PATH
, MAX_PATH
,
171 L
"\\StringFileInfo\\%04x%04x\\%ls", language
, code_page
, name
);
174 BOOL r
= ::VerQueryValue(data_
.get(), sub_block
, &value
, &size
);
176 value_str
->assign(static_cast<wchar_t*>(value
));
183 std::wstring
FileVersionInfoWin::GetStringValue(const wchar_t* name
) {
185 if (GetValue(name
, &str
))