1 // Copyright (c) 2006-2009 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 "chrome/installer/mini_installer/pe_resource.h"
7 PEResource::PEResource(HRSRC resource
, HMODULE module
)
8 : resource_(resource
), module_(module
) {
11 PEResource::PEResource(const wchar_t* name
, const wchar_t* type
, HMODULE module
)
12 : resource_(NULL
), module_(module
) {
13 resource_
= ::FindResource(module
, name
, type
);
16 bool PEResource::IsValid() {
17 return (NULL
!= resource_
);
20 size_t PEResource::Size() {
21 return ::SizeofResource(module_
, resource_
);
24 bool PEResource::WriteToDisk(const wchar_t* full_path
) {
25 // Resource handles are not real HGLOBALs so do not attempt to close them.
26 // Windows frees them whenever there is memory pressure.
27 HGLOBAL data_handle
= ::LoadResource(module_
, resource_
);
28 if (NULL
== data_handle
) {
31 void* data
= ::LockResource(data_handle
);
35 size_t resource_size
= Size();
36 HANDLE out_file
= ::CreateFile(full_path
, GENERIC_WRITE
, 0, NULL
,
37 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
38 if (INVALID_HANDLE_VALUE
== out_file
) {
42 if (!::WriteFile(out_file
, data
, static_cast<DWORD
>(resource_size
),
44 ::CloseHandle(out_file
);
47 return ::CloseHandle(out_file
) ? true : false;