< prev index next >

src/jdk.jpackage/windows/native/libapplauncher/WindowsPlatform.cpp

Print this page

        

*** 23,56 **** * questions. */ #include "Platform.h" - #ifdef WINDOWS - #include "JavaVirtualMachine.h" #include "WindowsPlatform.h" #include "Package.h" #include "Helpers.h" #include "PlatformString.h" #include "Macros.h" #include <map> #include <vector> #include <regex> #define WINDOWS_JPACKAGE_TMP_DIR \ L"\\AppData\\Local\\Java\\JPackage\\tmp" - class Registry { private: HKEY FKey; HKEY FOpenKey; bool FOpen; public: Registry(HKEY Key) { FOpen = false; FKey = Key; } --- 23,59 ---- * questions. */ #include "Platform.h" #include "JavaVirtualMachine.h" #include "WindowsPlatform.h" #include "Package.h" #include "Helpers.h" #include "PlatformString.h" #include "Macros.h" #include <map> #include <vector> #include <regex> + #include <fstream> + #include <locale> + #include <codecvt> + + using namespace std; #define WINDOWS_JPACKAGE_TMP_DIR \ L"\\AppData\\Local\\Java\\JPackage\\tmp" class Registry { private: HKEY FKey; HKEY FOpenKey; bool FOpen; public: + Registry(HKEY Key) { FOpen = false; FKey = Key; }
*** 125,149 **** if (dwRet == ERROR_MORE_DATA || dwRet == 0) { if (!buffer.Resize(length + 1)) { return result; } dwRet = RegQueryValueEx(FOpenKey, Name.data(), NULL, NULL, ! (LPBYTE)buffer.GetData(), &length); result = buffer.GetData(); } return result; } }; ! WindowsPlatform::WindowsPlatform(void) : Platform(), GenericPlatform() { FMainThread = ::GetCurrentThreadId(); } WindowsPlatform::~WindowsPlatform(void) { } TCHAR* WindowsPlatform::ConvertStringToFileSystemString(TCHAR* Source, bool &release) { // Not Implemented. return NULL; } --- 128,165 ---- if (dwRet == ERROR_MORE_DATA || dwRet == 0) { if (!buffer.Resize(length + 1)) { return result; } dwRet = RegQueryValueEx(FOpenKey, Name.data(), NULL, NULL, ! (LPBYTE) buffer.GetData(), &length); result = buffer.GetData(); } return result; } }; ! WindowsPlatform::WindowsPlatform(void) : Platform() { FMainThread = ::GetCurrentThreadId(); } WindowsPlatform::~WindowsPlatform(void) { } + TString WindowsPlatform::GetPackageAppDirectory() { + return FilePath::IncludeTrailingSeparator( + GetPackageRootDirectory()) + _T("app"); + } + + TString WindowsPlatform::GetPackageLauncherDirectory() { + return GetPackageRootDirectory(); + } + + TString WindowsPlatform::GetPackageRuntimeBinDirectory() { + return FilePath::IncludeTrailingSeparator(GetPackageRootDirectory()) + _T("runtime\\bin"); + } + TCHAR* WindowsPlatform::ConvertStringToFileSystemString(TCHAR* Source, bool &release) { // Not Implemented. return NULL; }
*** 172,181 **** --- 188,204 ---- } return result; } + TString WindowsPlatform::GetAppName() { + TString result = GetModuleFileName(); + result = FilePath::ExtractFileName(result); + result = FilePath::ChangeFileExt(result, _T("")); + return result; + } + void WindowsPlatform::ShowMessage(TString title, TString description) { MessageBox(NULL, description.data(), !title.empty() ? title.data() : description.data(), MB_ICONERROR | MB_OK); }
*** 231,248 **** if (buffer.GetData() == NULL) { return result; } ::GetModuleFileName(NULL, buffer.GetData(), ! static_cast<DWORD>(buffer.GetSize())); while (ERROR_INSUFFICIENT_BUFFER == GetLastError()) { if (!buffer.Resize(buffer.GetSize() * 2)) { return result; } ::GetModuleFileName(NULL, buffer.GetData(), ! static_cast<DWORD>(buffer.GetSize())); } result = buffer.GetData(); return result; } --- 254,271 ---- if (buffer.GetData() == NULL) { return result; } ::GetModuleFileName(NULL, buffer.GetData(), ! static_cast<DWORD> (buffer.GetSize())); while (ERROR_INSUFFICIENT_BUFFER == GetLastError()) { if (!buffer.Resize(buffer.GetSize() * 2)) { return result; } ::GetModuleFileName(NULL, buffer.GetData(), ! static_cast<DWORD> (buffer.GetSize())); } result = buffer.GetData(); return result; }
*** 250,265 **** Module WindowsPlatform::LoadLibrary(TString FileName) { return ::LoadLibrary(FileName.data()); } void WindowsPlatform::FreeLibrary(Module AModule) { ! ::FreeLibrary((HMODULE)AModule); } Procedure WindowsPlatform::GetProcAddress(Module AModule, std::string MethodName) { ! return ::GetProcAddress((HMODULE)AModule, MethodName.c_str()); } bool WindowsPlatform::IsMainThread() { bool result = (FMainThread == ::GetCurrentThreadId()); return result; --- 273,288 ---- Module WindowsPlatform::LoadLibrary(TString FileName) { return ::LoadLibrary(FileName.data()); } void WindowsPlatform::FreeLibrary(Module AModule) { ! ::FreeLibrary((HMODULE) AModule); } Procedure WindowsPlatform::GetProcAddress(Module AModule, std::string MethodName) { ! return ::GetProcAddress((HMODULE) AModule, MethodName.c_str()); } bool WindowsPlatform::IsMainThread() { bool result = (FMainThread == ::GetCurrentThreadId()); return result;
*** 281,291 **** return result; } static BOOL CALLBACK enumWindows(HWND winHandle, LPARAM lParam) { ! DWORD pid = (DWORD)lParam, wPid = 0; GetWindowThreadProcessId(winHandle, &wPid); if (pid == wPid) { SetForegroundWindow(winHandle); return FALSE; } --- 304,314 ---- return result; } static BOOL CALLBACK enumWindows(HWND winHandle, LPARAM lParam) { ! DWORD pid = (DWORD) lParam, wPid = 0; GetWindowThreadProcessId(winHandle, &wPid); if (pid == wPid) { SetForegroundWindow(winHandle); return FALSE; }
*** 293,315 **** } TPlatformNumber WindowsPlatform::GetMemorySize() { SYSTEM_INFO si; GetSystemInfo(&si); ! size_t result = (size_t)si.lpMaximumApplicationAddress; result = result / 1048576; // Convert from bytes to megabytes. return result; } - std::vector<TString> WindowsPlatform::GetLibraryImports( - const TString FileName) { - std::vector<TString> result; - WindowsLibrary library(FileName); - result = library.GetImports(); - return result; - } - std::vector<TString> FilterList(std::vector<TString> &Items, std::wregex Pattern) { std::vector<TString> result; for (std::vector<TString>::iterator it = Items.begin(); --- 316,330 ---- } TPlatformNumber WindowsPlatform::GetMemorySize() { SYSTEM_INFO si; GetSystemInfo(&si); ! size_t result = (size_t) si.lpMaximumApplicationAddress; result = result / 1048576; // Convert from bytes to megabytes. return result; } std::vector<TString> FilterList(std::vector<TString> &Items, std::wregex Pattern) { std::vector<TString> result; for (std::vector<TString>::iterator it = Items.begin();
*** 322,366 **** } } return result; } ! std::vector<TString> WindowsPlatform::FilterOutRuntimeDependenciesForPlatform( ! std::vector<TString> Imports) { ! std::vector<TString> result; ! Package& package = Package::GetInstance(); ! Macros& macros = Macros::GetInstance(); ! TString runtimeDir = macros.ExpandMacros(package.GetJVMRuntimeDirectory()); ! std::vector<TString> filelist = FilterList(Imports, ! std::wregex(_T("MSVCR.*.DLL"), std::regex_constants::icase)); ! ! for (std::vector<TString>::iterator it = filelist.begin(); ! it != filelist.end(); ++it) { ! TString filename = *it; ! TString msvcr100FileName = FilePath::IncludeTrailingSeparator( ! runtimeDir) + _T("jre\\bin\\") + filename; ! if (FilePath::FileExists(msvcr100FileName) == true) { ! result.push_back(msvcr100FileName); ! break; } - else { - msvcr100FileName = FilePath::IncludeTrailingSeparator(runtimeDir) - + _T("bin\\") + filename; ! if (FilePath::FileExists(msvcr100FileName) == true) { ! result.push_back(msvcr100FileName); ! break; } } } return result; } ! Process* WindowsPlatform::CreateProcess() { ! return new WindowsProcess(); } #ifdef DEBUG bool WindowsPlatform::IsNativeDebuggerPresent() { bool result = false; --- 337,426 ---- } } return result; } ! Process* WindowsPlatform::CreateProcess() { ! return new WindowsProcess(); ! } ! void WindowsPlatform::InitStreamLocale(wios *stream) { ! const std::locale empty_locale = std::locale::empty(); ! const std::locale utf8_locale = ! std::locale(empty_locale, new std::codecvt_utf8<wchar_t>()); ! stream->imbue(utf8_locale); ! } ! ! void WindowsPlatform::addPlatformDependencies(JavaLibrary *pJavaLibrary) { ! if (pJavaLibrary == NULL) { ! return; } ! if (FilePath::FileExists(_T("msvcr100.dll")) == true) { ! pJavaLibrary->AddDependency(_T("msvcr100.dll")); ! } ! ! TString runtimeBin = GetPackageRuntimeBinDirectory(); ! SetDllDirectory(runtimeBin.c_str()); ! } ! ! void Platform::CopyString(char *Destination, ! size_t NumberOfElements, const char *Source) { ! strcpy_s(Destination, NumberOfElements, Source); ! ! if (NumberOfElements > 0) { ! Destination[NumberOfElements - 1] = '\0'; } + } + + void Platform::CopyString(wchar_t *Destination, + size_t NumberOfElements, const wchar_t *Source) { + wcscpy_s(Destination, NumberOfElements, Source); + + if (NumberOfElements > 0) { + Destination[NumberOfElements - 1] = '\0'; } + } + + // Owner must free the return value. + MultibyteString Platform::WideStringToMultibyteString( + const wchar_t* value) { + MultibyteString result; + size_t count = 0; + + if (value == NULL) { + return result; + } + + count = WideCharToMultiByte(CP_UTF8, 0, value, -1, NULL, 0, NULL, NULL); + + if (count > 0) { + result.data = new char[count + 1]; + result.length = WideCharToMultiByte(CP_UTF8, 0, value, -1, + result.data, (int)count, NULL, NULL); } return result; } ! // Owner must free the return value. ! WideString Platform::MultibyteStringToWideString(const char* value) { ! WideString result; ! size_t count = 0; ! ! if (value == NULL) { ! return result; ! } ! ! mbstowcs_s(&count, NULL, 0, value, _TRUNCATE); ! ! if (count > 0) { ! result.data = new wchar_t[count + 1]; ! mbstowcs_s(&result.length, result.data, count, value, count); ! } ! ! return result; } #ifdef DEBUG bool WindowsPlatform::IsNativeDebuggerPresent() { bool result = false;
*** 376,386 **** int pid = GetProcessId(GetCurrentProcess()); return pid; } #endif //DEBUG - FileHandle::FileHandle(std::wstring FileName) { FHandle = ::CreateFile(FileName.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); } --- 436,445 ----
*** 432,442 **** LPVOID FileData::GetBaseAddress() { return FBaseAddress; } - WindowsLibrary::WindowsLibrary(std::wstring FileName) { FFileName = FileName; } std::vector<TString> WindowsLibrary::GetImports() { --- 491,500 ----
*** 449,461 **** if (mapping.IsValid() == true) { FileData fileData(mapping.GetHandle()); if (fileData.IsValid() == true) { PIMAGE_DOS_HEADER dosHeader = ! (PIMAGE_DOS_HEADER)fileData.GetBaseAddress(); PIMAGE_FILE_HEADER pImgFileHdr = ! (PIMAGE_FILE_HEADER)fileData.GetBaseAddress(); if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { result = DumpPEFile(dosHeader); } } } --- 507,519 ---- if (mapping.IsValid() == true) { FileData fileData(mapping.GetHandle()); if (fileData.IsValid() == true) { PIMAGE_DOS_HEADER dosHeader = ! (PIMAGE_DOS_HEADER) fileData.GetBaseAddress(); PIMAGE_FILE_HEADER pImgFileHdr = ! (PIMAGE_FILE_HEADER) fileData.GetBaseAddress(); if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { result = DumpPEFile(dosHeader); } } }
*** 464,473 **** --- 522,532 ---- return result; } // Given an RVA, look up the section header that encloses it and return a // pointer to its IMAGE_SECTION_HEADER + PIMAGE_SECTION_HEADER WindowsLibrary::GetEnclosingSectionHeader(DWORD rva, PIMAGE_NT_HEADERS pNTHeader) { PIMAGE_SECTION_HEADER result = 0; PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(pNTHeader);
*** 488,501 **** LPVOID result = 0; PIMAGE_SECTION_HEADER pSectionHdr = GetEnclosingSectionHeader(rva, pNTHeader); if (pSectionHdr != NULL) { ! INT delta = (INT)( ! pSectionHdr->VirtualAddress-pSectionHdr->PointerToRawData); DWORD_PTR dwp = (DWORD_PTR) (imageBase + rva - delta); ! result = reinterpret_cast<LPVOID>(dwp); // VS2017 - FIXME } return result; } --- 547,560 ---- LPVOID result = 0; PIMAGE_SECTION_HEADER pSectionHdr = GetEnclosingSectionHeader(rva, pNTHeader); if (pSectionHdr != NULL) { ! INT delta = (INT) ( ! pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData); DWORD_PTR dwp = (DWORD_PTR) (imageBase + rva - delta); ! result = reinterpret_cast<LPVOID> (dwp); // VS2017 - FIXME } return result; }
*** 515,537 **** PIMAGE_SECTION_HEADER pSection = GetEnclosingSectionHeader(importsStartRVA, pNTHeader); if (pSection != NULL) { PIMAGE_IMPORT_DESCRIPTOR importDesc = ! (PIMAGE_IMPORT_DESCRIPTOR)GetPtrFromRVA( ! importsStartRVA, pNTHeader,base); if (importDesc != NULL) { ! while (true) ! { // See if we've reached an empty IMAGE_IMPORT_DESCRIPTOR if ((importDesc->TimeDateStamp == 0) && (importDesc->Name == 0)) { break; } ! std::string filename = (char*)GetPtrFromRVA( importDesc->Name, pNTHeader, base); result.push_back(PlatformString(filename)); importDesc++; // advance to next IMAGE_IMPORT_DESCRIPTOR } } --- 574,595 ---- PIMAGE_SECTION_HEADER pSection = GetEnclosingSectionHeader(importsStartRVA, pNTHeader); if (pSection != NULL) { PIMAGE_IMPORT_DESCRIPTOR importDesc = ! (PIMAGE_IMPORT_DESCRIPTOR) GetPtrFromRVA( ! importsStartRVA, pNTHeader, base); if (importDesc != NULL) { ! while (true) { // See if we've reached an empty IMAGE_IMPORT_DESCRIPTOR if ((importDesc->TimeDateStamp == 0) && (importDesc->Name == 0)) { break; } ! std::string filename = (char*) GetPtrFromRVA( importDesc->Name, pNTHeader, base); result.push_back(PlatformString(filename)); importDesc++; // advance to next IMAGE_IMPORT_DESCRIPTOR } }
*** 542,564 **** } std::vector<TString> WindowsLibrary::DumpPEFile(PIMAGE_DOS_HEADER dosHeader) { std::vector<TString> result; // all of this is VS2017 - FIXME ! DWORD_PTR dwDosHeaders = reinterpret_cast<DWORD_PTR>(dosHeader); ! DWORD_PTR dwPIHeaders = dwDosHeaders + (DWORD)(dosHeader->e_lfanew); PIMAGE_NT_HEADERS pNTHeader = ! reinterpret_cast<PIMAGE_NT_HEADERS>(dwPIHeaders); // Verify that the e_lfanew field gave us a reasonable // pointer and the PE signature. // TODO: To really fix JDK-8131321 this condition needs to be changed. // There is a matching change // in JavaVirtualMachine.cpp that also needs to be changed. if (pNTHeader->Signature == IMAGE_NT_SIGNATURE) { ! DWORD base = (DWORD)(dwDosHeaders); result = GetImportsSection(base, pNTHeader); } return result; } --- 600,622 ---- } std::vector<TString> WindowsLibrary::DumpPEFile(PIMAGE_DOS_HEADER dosHeader) { std::vector<TString> result; // all of this is VS2017 - FIXME ! DWORD_PTR dwDosHeaders = reinterpret_cast<DWORD_PTR> (dosHeader); ! DWORD_PTR dwPIHeaders = dwDosHeaders + (DWORD) (dosHeader->e_lfanew); PIMAGE_NT_HEADERS pNTHeader = ! reinterpret_cast<PIMAGE_NT_HEADERS> (dwPIHeaders); // Verify that the e_lfanew field gave us a reasonable // pointer and the PE signature. // TODO: To really fix JDK-8131321 this condition needs to be changed. // There is a matching change // in JavaVirtualMachine.cpp that also needs to be changed. if (pNTHeader->Signature == IMAGE_NT_SIGNATURE) { ! DWORD base = (DWORD) (dwDosHeaders); result = GetImportsSection(base, pNTHeader); } return result; }
*** 577,602 **** HANDLE WindowsJob::GetHandle() { if (FHandle == NULL) { FHandle = CreateJobObject(NULL, NULL); // GLOBAL ! if (FHandle == NULL) ! { ! ::MessageBox( 0, _T("Could not create job object"), _T("TEST"), MB_OK); ! } ! else ! { ! JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; // Configure all child processes associated with // the job to terminate when the jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; if (0 == SetInformationJobObject(FHandle, ! JobObjectExtendedLimitInformation, &jeli, sizeof(jeli))) { ! ::MessageBox( 0, _T("Could not SetInformationJobObject"), _T("TEST"), MB_OK); } } } --- 635,657 ---- HANDLE WindowsJob::GetHandle() { if (FHandle == NULL) { FHandle = CreateJobObject(NULL, NULL); // GLOBAL ! if (FHandle == NULL) { ! ::MessageBox(0, _T("Could not create job object"), _T("TEST"), MB_OK); ! } else { ! JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = {0}; // Configure all child processes associated with // the job to terminate when the jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; if (0 == SetInformationJobObject(FHandle, ! JobObjectExtendedLimitInformation, &jeli, sizeof (jeli))) { ! ::MessageBox(0, _T("Could not SetInformationJobObject"), _T("TEST"), MB_OK); } } }
*** 625,645 **** HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (handle == INVALID_HANDLE_VALUE) { return false; } ! PROCESSENTRY32 process = { 0 }; ! process.dwSize = sizeof(process); if (::Process32First(handle, &process)) { do { if (process.th32ProcessID == FProcessInfo.dwProcessId) { result = true; break; } ! } ! while (::Process32Next(handle, &process)); } CloseHandle(handle); return result; --- 680,699 ---- HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); if (handle == INVALID_HANDLE_VALUE) { return false; } ! PROCESSENTRY32 process = {0}; ! process.dwSize = sizeof (process); if (::Process32First(handle, &process)) { do { if (process.th32ProcessID == FProcessInfo.dwProcessId) { result = true; break; } ! } while (::Process32Next(handle, &process)); } CloseHandle(handle); return result;
*** 661,673 **** if (FRunning == false) { FRunning = true; STARTUPINFO startupInfo; ! ZeroMemory(&startupInfo, sizeof(startupInfo)); ! startupInfo.cb = sizeof(startupInfo); ! ZeroMemory(&FProcessInfo, sizeof(FProcessInfo)); TString command = Application; for (std::vector<TString>::const_iterator iterator = Arguments.begin(); iterator != Arguments.end(); iterator++) { --- 715,727 ---- if (FRunning == false) { FRunning = true; STARTUPINFO startupInfo; ! ZeroMemory(&startupInfo, sizeof (startupInfo)); ! startupInfo.cb = sizeof (startupInfo); ! ZeroMemory(&FProcessInfo, sizeof (FProcessInfo)); TString command = Application; for (std::vector<TString>::const_iterator iterator = Arguments.begin(); iterator != Arguments.end(); iterator++) {
*** 678,689 **** NULL, FALSE, 0, NULL, NULL, &startupInfo, &FProcessInfo) == FALSE) { TString message = PlatformString::Format( _T("Error: Unable to create process %s"), Application.data()); throw Exception(message); ! } ! else { if (FJob.GetHandle() != NULL) { if (::AssignProcessToJobObject(FJob.GetHandle(), FProcessInfo.hProcess) == 0) { // Failed to assign process to job. It doesn't prevent // anything from continuing so continue. --- 732,742 ---- NULL, FALSE, 0, NULL, NULL, &startupInfo, &FProcessInfo) == FALSE) { TString message = PlatformString::Format( _T("Error: Unable to create process %s"), Application.data()); throw Exception(message); ! } else { if (FJob.GetHandle() != NULL) { if (::AssignProcessToJobObject(FJob.GetHandle(), FProcessInfo.hProcess) == 0) { // Failed to assign process to job. It doesn't prevent // anything from continuing so continue.
*** 725,731 **** std::list<TString> WindowsProcess::GetOutput() { ReadOutput(); return Process::GetOutput(); } - - #endif // WINDOWS --- 778,782 ----
< prev index next >