1 /*
   2  * Copyright (c) 2014, 2018, 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 #include "Platform.h"
  27 
  28 #ifdef WINDOWS
  29 
  30 #ifndef WINDOWSPLATFORM_H
  31 #define WINDOWSPLATFORM_H
  32 
  33 #include "GenericPlatform.h"
  34 
  35 #include <Windows.h>
  36 
  37 
  38 // the class is used to create and detect single instance of user application
  39 class SingleInstance {
  40 private:
  41     const int BUF_SIZE;
  42 
  43     DWORD  _lastError;
  44     HANDLE _mutex;
  45     TString _name;
  46     TString _sharedMemoryName;
  47     HANDLE _hMapFile;
  48     LPCTSTR _pBuf;
  49 
  50     SingleInstance(): BUF_SIZE(0) {}
  51 
  52     SingleInstance(TString& name_);
  53 
  54 public:
  55     static SingleInstance* getInstance(TString& name) {
  56         static SingleInstance* result = NULL;
  57 
  58         if (result == NULL) {
  59             result = new SingleInstance(name);
  60         }
  61 
  62         return result;
  63     }
  64 
  65     ~SingleInstance();
  66 
  67     bool IsAnotherInstanceRunning() {
  68         return (ERROR_ALREADY_EXISTS == _lastError);
  69     }
  70 
  71     bool writePid(DWORD pid);
  72     DWORD readPid();
  73 };
  74 
  75 #pragma warning( push )
  76 // C4250 - 'class1' : inherits 'class2::member'
  77 #pragma warning( disable : 4250 )
  78 class WindowsPlatform : virtual public Platform, GenericPlatform {
  79 private:
  80     DWORD FMainThread;
  81 
  82 public:
  83     WindowsPlatform(void);
  84     virtual ~WindowsPlatform(void);
  85 
  86     virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source,
  87             bool &release);
  88     virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source,
  89             bool &release);
  90 
  91     virtual void ShowMessage(TString title, TString description);
  92     virtual void ShowMessage(TString description);
  93     virtual MessageResponse ShowResponseMessage(TString title,
  94             TString description);
  95     //virtual MessageResponse ShowResponseMessage(TString description);
  96 
  97     virtual void SetCurrentDirectory(TString Value);
  98     virtual TString GetPackageRootDirectory();
  99     virtual TString GetAppDataDirectory();
 100     virtual TString GetBundledJVMLibraryFileName(TString RuntimePath);
 101 
 102     virtual ISectionalPropertyContainer* GetConfigFile(TString FileName);
 103 
 104     virtual TString GetModuleFileName();
 105     virtual Module LoadLibrary(TString FileName);
 106     virtual void FreeLibrary(Module AModule);
 107     virtual Procedure GetProcAddress(Module AModule, std::string MethodName);
 108     virtual std::vector<TString> GetLibraryImports(const TString FileName);
 109     virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(
 110             std::vector<TString> Imports);
 111 
 112     virtual Process* CreateProcess();
 113 
 114     virtual void reactivateAnotherInstance();
 115     virtual bool IsMainThread();
 116     virtual bool CheckForSingleInstance(TString Name);
 117     virtual TPlatformNumber GetMemorySize();
 118 
 119     virtual TString GetTempDirectory();
 120 
 121 #ifdef DEBUG
 122     virtual bool IsNativeDebuggerPresent();
 123     virtual int GetProcessID();
 124 #endif //DEBUG
 125 };
 126 #pragma warning( pop ) // C4250
 127 
 128 
 129 class FileHandle {
 130 private:
 131     HANDLE FHandle;
 132 
 133 public:
 134     FileHandle(std::wstring FileName);
 135     ~FileHandle();
 136 
 137     bool IsValid();
 138     HANDLE GetHandle();
 139 };
 140 
 141 
 142 class FileMappingHandle {
 143 private:
 144     HANDLE FHandle;
 145 
 146 public:
 147     FileMappingHandle(HANDLE FileHandle);
 148     ~FileMappingHandle();
 149 
 150     bool IsValid();
 151     HANDLE GetHandle();
 152 };
 153 
 154 
 155 class FileData {
 156 private:
 157     LPVOID FBaseAddress;
 158 
 159 public:
 160     FileData(HANDLE Handle);
 161     ~FileData();
 162 
 163     bool IsValid();
 164     LPVOID GetBaseAddress();
 165 };
 166 
 167 
 168 class WindowsLibrary {
 169 private:
 170     TString FFileName;
 171 
 172     // Given an RVA, look up the section header that encloses it and return a
 173     // pointer to its IMAGE_SECTION_HEADER
 174     static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva,
 175             PIMAGE_NT_HEADERS pNTHeader);
 176     static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader,
 177             DWORD imageBase);
 178     static std::vector<TString> GetImportsSection(DWORD base,
 179             PIMAGE_NT_HEADERS pNTHeader);
 180     static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader);
 181 
 182 public:
 183     WindowsLibrary(const TString FileName);
 184 
 185     std::vector<TString> GetImports();
 186 };
 187 
 188 
 189 class WindowsJob {
 190 private:
 191     HANDLE FHandle;
 192 
 193 public:
 194     WindowsJob();
 195     ~WindowsJob();
 196 
 197     HANDLE GetHandle();
 198 };
 199 
 200 
 201 class WindowsProcess : public Process {
 202 private:
 203     bool FRunning;
 204 
 205     PROCESS_INFORMATION FProcessInfo;
 206     static WindowsJob FJob;
 207 
 208     void Cleanup();
 209     bool ReadOutput();
 210 
 211 public:
 212     WindowsProcess();
 213     virtual ~WindowsProcess();
 214 
 215     virtual bool IsRunning();
 216     virtual bool Terminate();
 217     virtual bool Execute(const TString Application,
 218             const std::vector<TString> Arguments, bool AWait = false);
 219     virtual bool Wait();
 220     virtual TProcessID GetProcessID();
 221     virtual void SetInput(TString Value);
 222     virtual std::list<TString> GetOutput();
 223 };
 224 
 225 
 226 
 227 
 228 #endif // WINDOWSPLATFORM_H
 229 
 230 #endif // WINDOWS