1 /*
   2  * Copyright (c) 2012, 2019, 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 <Windows.h>
  27 #include <Shellapi.h>
  28 #include <locale.h>
  29 #include <tchar.h>
  30 #include <string>
  31 
  32 #define JPACKAGE_LIBRARY TEXT("applauncher.dll")
  33 
  34 typedef bool (*start_launcher)(int argc, TCHAR* argv[]);
  35 typedef void (*stop_launcher)();
  36 
  37 std::wstring GetTitle() {
  38     std::wstring result;
  39     wchar_t buffer[MAX_PATH];
  40     GetModuleFileName(NULL, buffer, MAX_PATH - 1);
  41     buffer[MAX_PATH - 1] = '\0';
  42     result = buffer;
  43     size_t slash = result.find_last_of('\\');
  44 
  45     if (slash != std::wstring::npos)
  46         result = result.substr(slash + 1, result.size() - slash - 1);
  47 
  48     return result;
  49 }
  50 
  51 #ifdef LAUNCHERC
  52 int main(int argc0, char *argv0[]) {
  53 #else // LAUNCHERC
  54 int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  55                        LPTSTR lpCmdLine, int nCmdShow) {
  56 #endif // LAUNCHERC
  57     int result = 1;
  58     TCHAR **argv;
  59     int argc;
  60 
  61     // [RT-31061] otherwise UI can be left in back of other windows.
  62     ::AllowSetForegroundWindow(ASFW_ANY);
  63 
  64     ::setlocale(LC_ALL, "en_US.utf8");
  65     argv = CommandLineToArgvW(GetCommandLine(), &argc);
  66 
  67     HMODULE library = ::LoadLibrary(JPACKAGE_LIBRARY);
  68 
  69     if (library == NULL) {
  70         std::wstring title = GetTitle();
  71         std::wstring description = std::wstring(JPACKAGE_LIBRARY)
  72                 + std::wstring(TEXT(" not found."));
  73         MessageBox(NULL, description.data(),
  74                 title.data(), MB_ICONERROR | MB_OK);
  75     }
  76     else {
  77         start_launcher start =
  78                 (start_launcher)GetProcAddress(library, "start_launcher");
  79         stop_launcher stop =
  80                 (stop_launcher)GetProcAddress(library, "stop_launcher");
  81 
  82         if (start != NULL && stop != NULL) {
  83             if (start(argc, argv) == true) {
  84                 result = 0;
  85                 stop();
  86             }
  87         }
  88 
  89         ::FreeLibrary(library);
  90     }
  91 
  92     if (argv != NULL) {
  93         LocalFree(argv);
  94     }
  95 
  96     return result;
  97 }
  98