1 /*
   2  * Copyright (c) 2014, 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 WINDOWSPLATFORM_H
  27 #define WINDOWSPLATFORM_H
  28 
  29 #include <Windows.h>
  30 #include "Platform.h"
  31 
  32 class WindowsPlatform : virtual public Platform {
  33 private:
  34     DWORD FMainThread;
  35 
  36 public:
  37     WindowsPlatform(void);
  38     virtual ~WindowsPlatform(void);
  39 
  40     virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source,
  41             bool &release);
  42     virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source,
  43             bool &release);
  44 
  45     virtual void ShowMessage(TString title, TString description);
  46     virtual void ShowMessage(TString description);
  47     virtual MessageResponse ShowResponseMessage(TString title,
  48             TString description);
  49 
  50     virtual TString GetPackageRootDirectory();
  51     virtual TString GetAppDataDirectory();
  52     virtual TString GetAppName();
  53     virtual TString GetBundledJavaLibraryFileName(TString RuntimePath);
  54     TString GetPackageAppDirectory();
  55     TString GetPackageLauncherDirectory();
  56     TString GetPackageRuntimeBinDirectory();
  57 
  58     virtual ISectionalPropertyContainer* GetConfigFile(TString FileName);
  59 
  60     virtual TString GetModuleFileName();
  61     virtual Module LoadLibrary(TString FileName);
  62     virtual void FreeLibrary(Module AModule);
  63     virtual Procedure GetProcAddress(Module AModule, std::string MethodName);
  64 
  65     virtual Process* CreateProcess();
  66 
  67     virtual bool IsMainThread();
  68     virtual TPlatformNumber GetMemorySize();
  69 
  70     virtual TString GetTempDirectory();
  71     void InitStreamLocale(wios *stream);
  72     void addPlatformDependencies(JavaLibrary *pJavaLibrary);
  73 };
  74 
  75 class FileHandle {
  76 private:
  77     HANDLE FHandle;
  78 
  79 public:
  80     FileHandle(std::wstring FileName);
  81     ~FileHandle();
  82 
  83     bool IsValid();
  84     HANDLE GetHandle();
  85 };
  86 
  87 
  88 class FileMappingHandle {
  89 private:
  90     HANDLE FHandle;
  91 
  92 public:
  93     FileMappingHandle(HANDLE FileHandle);
  94     ~FileMappingHandle();
  95 
  96     bool IsValid();
  97     HANDLE GetHandle();
  98 };
  99 
 100 
 101 class FileData {
 102 private:
 103     LPVOID FBaseAddress;
 104 
 105 public:
 106     FileData(HANDLE Handle);
 107     ~FileData();
 108 
 109     bool IsValid();
 110     LPVOID GetBaseAddress();
 111 };
 112 
 113 
 114 class WindowsLibrary {
 115 private:
 116     TString FFileName;
 117 
 118     // Given an RVA, look up the section header that encloses it and return a
 119     // pointer to its IMAGE_SECTION_HEADER
 120     static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva,
 121             PIMAGE_NT_HEADERS pNTHeader);
 122     static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader,
 123             DWORD imageBase);
 124     static std::vector<TString> GetImportsSection(DWORD base,
 125             PIMAGE_NT_HEADERS pNTHeader);
 126     static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader);
 127 
 128 public:
 129     WindowsLibrary(const TString FileName);
 130 
 131     std::vector<TString> GetImports();
 132 };
 133 
 134 
 135 class WindowsJob {
 136 private:
 137     HANDLE FHandle;
 138 
 139 public:
 140     WindowsJob();
 141     ~WindowsJob();
 142 
 143     HANDLE GetHandle();
 144 };
 145 
 146 
 147 class WindowsProcess : public Process {
 148 private:
 149     bool FRunning;
 150 
 151     PROCESS_INFORMATION FProcessInfo;
 152     static WindowsJob FJob;
 153 
 154     void Cleanup();
 155     bool ReadOutput();
 156 
 157 public:
 158     WindowsProcess();
 159     virtual ~WindowsProcess();
 160 
 161     virtual bool IsRunning();
 162     virtual bool Terminate();
 163     virtual bool Execute(const TString Application,
 164             const std::vector<TString> Arguments, bool AWait = false);
 165     virtual bool Wait();
 166     virtual TProcessID GetProcessID();
 167     virtual void SetInput(TString Value);
 168     virtual std::list<TString> GetOutput();
 169 };
 170 
 171 #endif // WINDOWSPLATFORM_H