- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / installer / mini_installer / pe_resource.cc
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.
4
5 #include "chrome/installer/mini_installer/pe_resource.h"
6
7 PEResource::PEResource(HRSRC resource, HMODULE module)
8     : resource_(resource), module_(module) {
9 }
10
11 PEResource::PEResource(const wchar_t* name, const wchar_t* type, HMODULE module)
12     : resource_(NULL), module_(module) {
13   resource_ = ::FindResource(module, name, type);
14 }
15
16 bool PEResource::IsValid() {
17   return (NULL != resource_);
18 }
19
20 size_t PEResource::Size() {
21   return ::SizeofResource(module_, resource_);
22 }
23
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;
30   }
31   void* data = ::LockResource(data_handle);
32   if (NULL == data) {
33     return false;
34   }
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;
40   }
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;
46   }
47   return ::CloseHandle(out_file) ? true : false;
48 }