1 /* 2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #include "Platform.h" 35 36 #ifdef WINDOWS 37 38 #ifndef WINDOWSPLATFORM_H 39 #define WINDOWSPLATFORM_H 40 41 #include "GenericPlatform.h" 42 #include "JavaUserPreferences.h" 43 44 #include <Windows.h> 45 46 47 // the class is used to create and detect single instance of user application 48 class SingleInstance { 49 private: 50 const int BUF_SIZE; 51 52 DWORD _lastError; 53 HANDLE _mutex; 54 TString _name; 55 TString _sharedMemoryName; 56 HANDLE _hMapFile; 57 LPCTSTR _pBuf; 58 59 SingleInstance(): BUF_SIZE(0) {} 60 61 SingleInstance(TString& name_); 62 63 public: 64 static SingleInstance* getInstance(TString& name) { 65 static SingleInstance* result = NULL; 66 67 if (result == NULL) { 68 result = new SingleInstance(name); 69 } 70 71 return result; 72 } 73 74 ~SingleInstance(); 75 76 bool IsAnotherInstanceRunning() { 77 return (ERROR_ALREADY_EXISTS == _lastError); 78 } 79 80 bool writePid(DWORD pid); 81 DWORD readPid(); 82 }; 83 84 #pragma warning( push ) 85 #pragma warning( disable : 4250 ) // C4250 - 'class1' : inherits 'class2::member' 86 class WindowsPlatform : virtual public Platform, GenericPlatform { 87 private: 88 DWORD FMainThread; 89 90 public: 91 WindowsPlatform(void); 92 virtual ~WindowsPlatform(void); 93 94 virtual TCHAR* ConvertStringToFileSystemString(TCHAR* Source, bool &release); 95 virtual TCHAR* ConvertFileSystemStringToString(TCHAR* Source, bool &release); 96 97 virtual void ShowMessage(TString title, TString description); 98 virtual void ShowMessage(TString description); 99 virtual MessageResponse ShowResponseMessage(TString title, TString description); 100 //virtual MessageResponse ShowResponseMessage(TString description); 101 102 virtual void SetCurrentDirectory(TString Value); 103 virtual TString GetPackageRootDirectory(); 104 virtual TString GetAppDataDirectory(); 105 virtual TString GetBundledJVMLibraryFileName(TString RuntimePath); 106 virtual TString GetSystemJVMLibraryFileName(); 107 virtual TString GetSystemJRE(); 108 109 virtual ISectionalPropertyContainer* GetConfigFile(TString FileName); 110 111 virtual TString GetModuleFileName(); 112 virtual Module LoadLibrary(TString FileName); 113 virtual void FreeLibrary(Module AModule); 114 virtual Procedure GetProcAddress(Module AModule, std::string MethodName); 115 virtual std::vector<TString> GetLibraryImports(const TString FileName); 116 virtual std::vector<TString> FilterOutRuntimeDependenciesForPlatform(std::vector<TString> Imports); 117 118 virtual Process* CreateProcess(); 119 120 virtual void reactivateAnotherInstance(); 121 virtual bool IsMainThread(); 122 virtual bool CheckForSingleInstance(TString Name); 123 virtual TPlatformNumber GetMemorySize(); 124 125 #ifdef DEBUG 126 virtual bool IsNativeDebuggerPresent(); 127 virtual int GetProcessID(); 128 #endif //DEBUG 129 }; 130 #pragma warning( pop ) // C4250 131 132 133 class WindowsJavaUserPreferences : public JavaUserPreferences { 134 public: 135 WindowsJavaUserPreferences(void); 136 ~WindowsJavaUserPreferences(void); 137 138 virtual bool Load(TString Appid); 139 }; 140 141 142 class FileHandle { 143 private: 144 HANDLE FHandle; 145 146 public: 147 FileHandle(std::wstring FileName); 148 ~FileHandle(); 149 150 bool IsValid(); 151 HANDLE GetHandle(); 152 }; 153 154 155 class FileMappingHandle { 156 private: 157 HANDLE FHandle; 158 159 public: 160 FileMappingHandle(HANDLE FileHandle); 161 ~FileMappingHandle(); 162 163 bool IsValid(); 164 HANDLE GetHandle(); 165 }; 166 167 168 class FileData { 169 private: 170 LPVOID FBaseAddress; 171 172 public: 173 FileData(HANDLE Handle); 174 ~FileData(); 175 176 bool IsValid(); 177 LPVOID GetBaseAddress(); 178 }; 179 180 181 class WindowsLibrary { 182 private: 183 TString FFileName; 184 185 // Given an RVA, look up the section header that encloses it and return a 186 // pointer to its IMAGE_SECTION_HEADER 187 static PIMAGE_SECTION_HEADER GetEnclosingSectionHeader(DWORD rva, PIMAGE_NT_HEADERS pNTHeader); 188 static LPVOID GetPtrFromRVA(DWORD rva, PIMAGE_NT_HEADERS pNTHeader, DWORD imageBase); 189 static std::vector<TString> GetImportsSection(DWORD base, PIMAGE_NT_HEADERS pNTHeader); 190 static std::vector<TString> DumpPEFile(PIMAGE_DOS_HEADER dosHeader); 191 192 public: 193 WindowsLibrary(const TString FileName); 194 195 std::vector<TString> GetImports(); 196 }; 197 198 199 class WindowsJob { 200 private: 201 HANDLE FHandle; 202 203 public: 204 WindowsJob(); 205 ~WindowsJob(); 206 207 HANDLE GetHandle(); 208 }; 209 210 211 class WindowsProcess : public Process { 212 private: 213 bool FRunning; 214 215 PROCESS_INFORMATION FProcessInfo; 216 static WindowsJob FJob; 217 218 void Cleanup(); 219 bool ReadOutput(); 220 221 public: 222 WindowsProcess(); 223 virtual ~WindowsProcess(); 224 225 virtual bool IsRunning(); 226 virtual bool Terminate(); 227 virtual bool Execute(const TString Application, const std::vector<TString> Arguments, 228 bool AWait = false); 229 virtual bool Wait(); 230 virtual TProcessID GetProcessID(); 231 virtual void SetInput(TString Value); 232 virtual std::list<TString> GetOutput(); 233 }; 234 235 236 237 238 #endif //WINDOWSPLATFORM_H 239 240 #endif // WINDOWS