modules/jdk.packager/src/main/native/library/common/WindowsPlatform.cpp

Print this page


   1 /*
   2  * Copyright (c) 2014, 2016, 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


 301     return result;
 302 }
 303 
 304 Module WindowsPlatform::LoadLibrary(TString FileName) {
 305     return ::LoadLibrary(FileName.data());
 306 }
 307 
 308 void WindowsPlatform::FreeLibrary(Module AModule) {
 309     ::FreeLibrary((HMODULE)AModule);
 310 }
 311 
 312 Procedure WindowsPlatform::GetProcAddress(Module AModule, std::string MethodName) {
 313     return ::GetProcAddress((HMODULE)AModule, MethodName.c_str());
 314 }
 315 
 316 bool WindowsPlatform::IsMainThread() {
 317     bool result = (FMainThread == ::GetCurrentThreadId());
 318     return result;
 319 }
 320 





















































































































 321 TPlatformNumber WindowsPlatform::GetMemorySize() {
 322     SYSTEM_INFO si;
 323     GetSystemInfo(&si);
 324     size_t result = (size_t)si.lpMaximumApplicationAddress;
 325     result = result / 1048576; // Convert from bytes to megabytes.
 326     return result;
 327 }
 328 
 329 std::vector<TString> WindowsPlatform::GetLibraryImports(const TString FileName) {
 330  std::vector<TString> result;
 331     WindowsLibrary library(FileName);
 332     result = library.GetImports();
 333  return result;
 334 }
 335 
 336 std::vector<TString> FilterList(std::vector<TString> &Items, std::wregex Pattern) {
 337     std::vector<TString> result;
 338 
 339     for (std::vector<TString>::iterator it = Items.begin(); it != Items.end(); ++it) {
 340         TString item = *it;


   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


 301     return result;
 302 }
 303 
 304 Module WindowsPlatform::LoadLibrary(TString FileName) {
 305     return ::LoadLibrary(FileName.data());
 306 }
 307 
 308 void WindowsPlatform::FreeLibrary(Module AModule) {
 309     ::FreeLibrary((HMODULE)AModule);
 310 }
 311 
 312 Procedure WindowsPlatform::GetProcAddress(Module AModule, std::string MethodName) {
 313     return ::GetProcAddress((HMODULE)AModule, MethodName.c_str());
 314 }
 315 
 316 bool WindowsPlatform::IsMainThread() {
 317     bool result = (FMainThread == ::GetCurrentThreadId());
 318     return result;
 319 }
 320 
 321 static BOOL CALLBACK enumWindows(HWND winHandle, LPARAM lParam) {
 322     DWORD pid = (DWORD)lParam, wPid = 0;
 323     GetWindowThreadProcessId(winHandle, &wPid);
 324     if (pid == wPid)  {
 325         SetForegroundWindow(winHandle);
 326         return FALSE;
 327     }
 328     return TRUE;
 329 }
 330 
 331 void WindowsPlatform::reactivateAnotherInstance() {
 332     if (singleInstanceProcessId == 0) {
 333         printf("Unable to reactivate another instance, PID is undefined");
 334         return;
 335     }
 336     EnumWindows(&enumWindows, (LPARAM)singleInstanceProcessId);
 337 }
 338 
 339 // returns true if another instance is already running.
 340 // if false, we need to continue regular launch.
 341 bool WindowsPlatform::CheckForSingleInstance(TString name) {
 342     if (SingleInstance::getInstance(name)->IsAnotherInstanceRunning()) {
 343         // read PID
 344         DWORD pid = SingleInstance::getInstance(name)->readPid();
 345         if (pid != 0) {
 346             singleInstanceProcessId = pid;
 347             return true;
 348         }
 349     } else {
 350         // it is the first intance
 351         // write pid and continue regular launch
 352         SingleInstance::getInstance(name)->writePid(GetCurrentProcessId());
 353     }
 354     return false;
 355 }
 356 
 357 SingleInstance::SingleInstance(TString& name_): BUF_SIZE(256), _name(name_), _hMapFile(NULL), _pBuf(NULL) {
 358     _mutex = CreateMutex(NULL, TRUE, name_.data());
 359     _lastError = GetLastError();
 360     _sharedMemoryName = _T("Local\\javapackager-") + _name;
 361 }
 362 
 363 SingleInstance::~SingleInstance() {
 364     if (_pBuf != NULL) {
 365         UnmapViewOfFile(_pBuf);
 366         _pBuf = NULL;
 367     }
 368 
 369     if (_hMapFile != NULL) {
 370         CloseHandle(_hMapFile);
 371         _hMapFile = NULL;
 372     }
 373 
 374     if (_mutex != NULL) {
 375         CloseHandle(_mutex);
 376         _mutex = NULL;
 377     }
 378 }
 379 
 380 bool SingleInstance::writePid(DWORD pid) {
 381     _hMapFile = CreateFileMapping(
 382                  INVALID_HANDLE_VALUE,
 383                  NULL,
 384                  PAGE_READWRITE,
 385                  0,
 386                  BUF_SIZE,
 387                  _sharedMemoryName.data());
 388 
 389     if (_hMapFile == NULL) {
 390         return false;
 391     }
 392 
 393     _pBuf = (LPTSTR) MapViewOfFile(_hMapFile,
 394                         FILE_MAP_ALL_ACCESS,
 395                         0,
 396                         0,
 397                         BUF_SIZE);
 398 
 399     if (_pBuf == NULL) {
 400         CloseHandle(_hMapFile);
 401         _hMapFile = NULL;
 402         return false;
 403     }
 404 
 405     CopyMemory((PVOID)_pBuf, &pid, sizeof(DWORD));
 406 
 407     return true;
 408 }
 409 
 410 DWORD SingleInstance::readPid() {
 411     _hMapFile = OpenFileMapping(
 412                    FILE_MAP_ALL_ACCESS,
 413                    FALSE,
 414                    _sharedMemoryName.data());
 415 
 416     if (_hMapFile == NULL) {
 417         return 0;
 418     }
 419 
 420    _pBuf = (LPTSTR) MapViewOfFile(_hMapFile,
 421                FILE_MAP_ALL_ACCESS,
 422                0,
 423                0,
 424                BUF_SIZE);
 425 
 426     if (_pBuf == NULL) {
 427         CloseHandle(_hMapFile);
 428         _hMapFile = NULL;
 429         return 0;
 430     }
 431 
 432     DWORD pid = 0;
 433     CopyMemory(&pid, (PVOID)_pBuf,  sizeof(DWORD));
 434 
 435     return pid;
 436 }
 437 
 438 TPlatformNumber WindowsPlatform::GetMemorySize() {
 439     SYSTEM_INFO si;
 440     GetSystemInfo(&si);
 441     size_t result = (size_t)si.lpMaximumApplicationAddress;
 442     result = result / 1048576; // Convert from bytes to megabytes.
 443     return result;
 444 }
 445 
 446 std::vector<TString> WindowsPlatform::GetLibraryImports(const TString FileName) {
 447  std::vector<TString> result;
 448     WindowsLibrary library(FileName);
 449     result = library.GetImports();
 450  return result;
 451 }
 452 
 453 std::vector<TString> FilterList(std::vector<TString> &Items, std::wregex Pattern) {
 454     std::vector<TString> result;
 455 
 456     for (std::vector<TString>::iterator it = Items.begin(); it != Items.end(); ++it) {
 457         TString item = *it;