< prev index next >

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

Print this page

        

@@ -23,34 +23,37 @@
  * 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>
+#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,25 +128,38 @@
         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);
+                    (LPBYTE) buffer.GetData(), &length);
             result = buffer.GetData();
         }
 
         return result;
     }
 };
 
-WindowsPlatform::WindowsPlatform(void) : Platform(), GenericPlatform() {
+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,10 +188,17 @@
     }
 
     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,18 +254,18 @@
     if (buffer.GetData() == NULL) {
         return result;
     }
 
     ::GetModuleFileName(NULL, buffer.GetData(),
-            static_cast<DWORD>(buffer.GetSize()));
+            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()));
+                static_cast<DWORD> (buffer.GetSize()));
     }
 
     result = buffer.GetData();
     return result;
 }

@@ -250,16 +273,16 @@
 Module WindowsPlatform::LoadLibrary(TString FileName) {
     return ::LoadLibrary(FileName.data());
 }
 
 void WindowsPlatform::FreeLibrary(Module AModule) {
-    ::FreeLibrary((HMODULE)AModule);
+    ::FreeLibrary((HMODULE) AModule);
 }
 
 Procedure WindowsPlatform::GetProcAddress(Module AModule,
         std::string MethodName) {
-    return ::GetProcAddress((HMODULE)AModule, MethodName.c_str());
+    return ::GetProcAddress((HMODULE) AModule, MethodName.c_str());
 }
 
 bool WindowsPlatform::IsMainThread() {
     bool result = (FMainThread == ::GetCurrentThreadId());
     return result;

@@ -281,11 +304,11 @@
 
     return result;
 }
 
 static BOOL CALLBACK enumWindows(HWND winHandle, LPARAM lParam) {
-    DWORD pid = (DWORD)lParam, wPid = 0;
+    DWORD pid = (DWORD) lParam, wPid = 0;
     GetWindowThreadProcessId(winHandle, &wPid);
     if (pid == wPid)  {
         SetForegroundWindow(winHandle);
         return FALSE;
     }

@@ -293,23 +316,15 @@
 }
 
 TPlatformNumber WindowsPlatform::GetMemorySize() {
     SYSTEM_INFO si;
     GetSystemInfo(&si);
-    size_t result = (size_t)si.lpMaximumApplicationAddress;
+    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();

@@ -322,45 +337,90 @@
         }
     }
     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;
+Process* WindowsPlatform::CreateProcess() {
+    return new WindowsProcess();
+}
 
-        if (FilePath::FileExists(msvcr100FileName) == true) {
-            result.push_back(msvcr100FileName);
-            break;
+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;
         }
-        else {
-            msvcr100FileName = FilePath::IncludeTrailingSeparator(runtimeDir)
-                    + _T("bin\\") + filename;
 
-            if (FilePath::FileExists(msvcr100FileName) == true) {
-                result.push_back(msvcr100FileName);
-                break;
+    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;
 }
 
-Process* WindowsPlatform::CreateProcess() {
-    return new WindowsProcess();
+// 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,11 +436,10 @@
     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);
 }
 

@@ -432,11 +491,10 @@
 
 LPVOID FileData::GetBaseAddress() {
     return FBaseAddress;
 }
 
-
 WindowsLibrary::WindowsLibrary(std::wstring FileName) {
     FFileName = FileName;
 }
 
 std::vector<TString> WindowsLibrary::GetImports() {

@@ -449,13 +507,13 @@
         if (mapping.IsValid() == true) {
             FileData fileData(mapping.GetHandle());
 
             if (fileData.IsValid() == true) {
                 PIMAGE_DOS_HEADER dosHeader =
-                        (PIMAGE_DOS_HEADER)fileData.GetBaseAddress();
+                        (PIMAGE_DOS_HEADER) fileData.GetBaseAddress();
                 PIMAGE_FILE_HEADER pImgFileHdr =
-                        (PIMAGE_FILE_HEADER)fileData.GetBaseAddress();
+                        (PIMAGE_FILE_HEADER) fileData.GetBaseAddress();
                 if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) {
                     result = DumpPEFile(dosHeader);
                 }
             }
         }

@@ -464,10 +522,11 @@
     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,14 +547,14 @@
     LPVOID result = 0;
     PIMAGE_SECTION_HEADER pSectionHdr = GetEnclosingSectionHeader(rva,
             pNTHeader);
 
     if (pSectionHdr != NULL) {
-        INT delta = (INT)(
-                pSectionHdr->VirtualAddress-pSectionHdr->PointerToRawData);
+        INT delta = (INT) (
+                pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData);
         DWORD_PTR dwp = (DWORD_PTR) (imageBase + rva - delta);
-        result = reinterpret_cast<LPVOID>(dwp); // VS2017 - FIXME
+        result = reinterpret_cast<LPVOID> (dwp); // VS2017 - FIXME
     }
 
     return result;
 }
 

@@ -515,23 +574,22 @@
         PIMAGE_SECTION_HEADER pSection =
                 GetEnclosingSectionHeader(importsStartRVA, pNTHeader);
 
         if (pSection != NULL) {
             PIMAGE_IMPORT_DESCRIPTOR importDesc =
-                    (PIMAGE_IMPORT_DESCRIPTOR)GetPtrFromRVA(
-                    importsStartRVA, pNTHeader,base);
+                    (PIMAGE_IMPORT_DESCRIPTOR) GetPtrFromRVA(
+                    importsStartRVA, pNTHeader, base);
 
             if (importDesc != NULL) {
-                while (true)
-                {
+                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(
+                    std::string filename = (char*) GetPtrFromRVA(
                             importDesc->Name, pNTHeader, base);
                     result.push_back(PlatformString(filename));
                     importDesc++;   // advance to next IMAGE_IMPORT_DESCRIPTOR
                 }
             }

@@ -542,23 +600,23 @@
 }
 
 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);
+    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);
+            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);
+        DWORD base = (DWORD) (dwDosHeaders);
         result = GetImportsSection(base, pNTHeader);
     }
 
     return result;
 }

@@ -577,26 +635,23 @@
 
 HANDLE WindowsJob::GetHandle() {
     if (FHandle == NULL) {
         FHandle = CreateJobObject(NULL, NULL); // GLOBAL
 
-        if (FHandle == NULL)
-        {
-            ::MessageBox( 0, _T("Could not create job object"),
+        if (FHandle == NULL) {
+            ::MessageBox(0, _T("Could not create job object"),
                     _T("TEST"), MB_OK);
-        }
-        else
-        {
-            JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
+        } 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"),
+                    JobObjectExtendedLimitInformation, &jeli, sizeof (jeli))) {
+                ::MessageBox(0, _T("Could not SetInformationJobObject"),
                         _T("TEST"), MB_OK);
             }
         }
     }
 

@@ -625,21 +680,20 @@
     HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
     if (handle == INVALID_HANDLE_VALUE) {
         return false;
     }
 
-    PROCESSENTRY32 process = { 0 };
-    process.dwSize = sizeof(process);
+    PROCESSENTRY32 process = {0};
+    process.dwSize = sizeof (process);
 
     if (::Process32First(handle, &process)) {
         do {
             if (process.th32ProcessID == FProcessInfo.dwProcessId) {
                 result = true;
                 break;
             }
-        }
-        while (::Process32Next(handle, &process));
+        } while (::Process32Next(handle, &process));
     }
 
     CloseHandle(handle);
 
     return result;

@@ -661,13 +715,13 @@
 
     if (FRunning == false) {
         FRunning = true;
 
         STARTUPINFO startupInfo;
-        ZeroMemory(&startupInfo, sizeof(startupInfo));
-        startupInfo.cb = sizeof(startupInfo);
-        ZeroMemory(&FProcessInfo, sizeof(FProcessInfo));
+        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,12 +732,11 @@
             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 {
+        } 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,7 +778,5 @@
 
 std::list<TString> WindowsProcess::GetOutput() {
     ReadOutput();
     return Process::GetOutput();
 }
-
-#endif // WINDOWS
< prev index next >