Close a window for a race with the system linker
[chromium-blink-merge.git] / chrome / installer / mini_installer / pe_resource.cc
blobce63627ddf5a86b9d3a13b1199315baea2128171
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) {
29 return false;
31 void* data = ::LockResource(data_handle);
32 if (NULL == data) {
33 return false;
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) {
39 return false;
41 DWORD written = 0;
42 if (!::WriteFile(out_file, data, static_cast<DWORD>(resource_size),
43 &written, NULL)) {
44 ::CloseHandle(out_file);
45 return false;
47 return ::CloseHandle(out_file) ? true : false;