1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef RESOURCES_H
  27 #define RESOURCES_H
  28 
  29 #include "WinSysInfo.h"
  30 
  31 
  32 /**
  33  * Classes for resource loading.
  34  * Common use cases:
  35  *  - check if resource is available and save it to file:
  36  *      Resource res(_T("MyResource"), _T("CustomResourceType"));
  37  *      if (res.available()) {
  38  *          res.saveToFile(_T("c:\\temp\\my_resource.bin"));
  39  *      }
  40  */
  41 
  42 class Resource {
  43 public:
  44     // name and type can be specified by string id, by integer id (RT_* constants or MAKEINTRESOURCE)
  45     Resource(LPCWSTR name, LPCWSTR type, HINSTANCE module = SysInfo::getCurrentModuleHandle());
  46     Resource(UINT id, LPCWSTR type, HINSTANCE module = SysInfo::getCurrentModuleHandle());
  47 
  48     bool available() const;
  49 
  50     // all this methods throw exception if the resource is not available
  51     unsigned size() const;
  52     // gets raw pointer to the resource data
  53     LPCVOID rawData() const;
  54 
  55     // save the resource to a file
  56     void saveToFile(const std::wstring &filePath) const;
  57 
  58     typedef std::vector<BYTE> ByteArray;
  59     // returns the resource as byte array
  60     ByteArray binary() const;
  61 
  62 private:
  63     std::wstring nameStr;
  64     LPCWSTR namePtr;    // can be integer (MAKEINTRESOURCE) value or point to nameStr.c_str()
  65     std::wstring typeStr;
  66     LPCWSTR typePtr;    // can be integer (MAKEINTRESOURCE) value or point to nameStr.c_str()
  67     HINSTANCE instance;
  68 
  69     void init(LPCWSTR name, LPCWSTR type, HINSTANCE module);
  70 
  71     // generates error message
  72     std::string getErrMsg(const std::string &descr) const;
  73     HRSRC findResource() const;
  74     LPVOID getPtr(DWORD &size) const;
  75 
  76 private:
  77     // disable copying
  78     Resource(const Resource&);
  79     Resource& operator = (const Resource&);
  80 };
  81 
  82 #endif // RESOURCES_H