8226835: Command window pops up building exe package JDK-8200758-branch
authorherrick
Fri, 28 Jun 2019 16:50:51 -0400
branchJDK-8200758-branch
changeset 57444 91e9d4691e5e
parent 57443 c71ef0329ac6
child 57445 405ddd80496e
8226835: Command window pops up building exe package Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
src/jdk.jpackage/windows/native/libjpackage/SysInfo.h
src/jdk.jpackage/windows/native/libjpackage/WinSysInfo.cpp
src/jdk.jpackage/windows/native/msiwrapper/MsiWrapper.cpp
--- a/src/jdk.jpackage/windows/native/libjpackage/SysInfo.h	Fri Jun 28 16:49:32 2019 -0400
+++ b/src/jdk.jpackage/windows/native/libjpackage/SysInfo.h	Fri Jun 28 16:50:51 2019 -0400
@@ -51,9 +51,21 @@
 
     /**
      * Returns absolute path to the current executable module.
-     */ 
+     */
     tstring getCurrentModulePath();
 
+    enum CommandArgProgramNameMode {
+        IncludeProgramName,
+        ExcludeProgramName
+    };
+    /**
+     * Retrieves the command-line arguments for the current process.
+     * With IncludeProgramName option returns result similar to argv/argc.
+     * With ExcludeProgramName option program name (the 1st element of command line)
+     * is excluded.
+     */
+    tstring_array getCommandArgs(CommandArgProgramNameMode progNameMode = ExcludeProgramName);
+
     /**
      * Returns value of environment variable with the given name.
      * Throws exception if variable is not set or any other error occurred
--- a/src/jdk.jpackage/windows/native/libjpackage/WinSysInfo.cpp	Fri Jun 28 16:49:32 2019 -0400
+++ b/src/jdk.jpackage/windows/native/libjpackage/WinSysInfo.cpp	Fri Jun 28 16:50:51 2019 -0400
@@ -23,10 +23,15 @@
  * questions.
  */
 
+#include <windows.h>
+#include <shellapi.h>
+
 #include "WinSysInfo.h"
 #include "FileUtils.h"
 #include "WinErrorHandling.h"
 
+#pragma comment(lib, "Shell32")
+
 namespace SysInfo {
 
 tstring getTempDir() {
@@ -113,6 +118,24 @@
     return getModulePath(getCurrentModuleHandle());
 }
 
+tstring_array getCommandArgs(CommandArgProgramNameMode progNameMode)
+{
+    int argc = 0;
+    tstring_array result;
+
+    LPWSTR *parsedArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
+    if (parsedArgs == NULL) {
+        JP_THROW(SysError("CommandLineToArgvW failed", CommandLineToArgvW));
+    }
+    // the 1st element contains program name
+    for (int i = progNameMode == ExcludeProgramName ? 1 : 0; i < argc; i++) {
+        result.push_back(parsedArgs[i]);
+    }
+    LocalFree(parsedArgs);
+
+    return result;
+}
+
 namespace {
 
 tstring getEnvVariableImpl(const tstring& name, bool* errorOccured=0) {
--- a/src/jdk.jpackage/windows/native/msiwrapper/MsiWrapper.cpp	Fri Jun 28 16:49:32 2019 -0400
+++ b/src/jdk.jpackage/windows/native/msiwrapper/MsiWrapper.cpp	Fri Jun 28 16:50:51 2019 -0400
@@ -1,13 +1,14 @@
+#include <algorithm>
 #include <windows.h>
 
-#include "WinSysInfo.h"
+#include "SysInfo.h"
 #include "FileUtils.h"
 #include "Executor.h"
 #include "Resources.h"
 #include "WinErrorHandling.h"
 
 
-int wmain(int argc, wchar_t *argv[])
+int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int nShowCmd)
 {
     JP_TRY;
 
@@ -26,12 +27,15 @@
     // Setup executor to run msiexec
     Executor msiExecutor(SysInfo::getWIPath());
     msiExecutor.arg(L"/i").arg(msiPath);
-    for (int i = 1; i < argc; ++i) {
-        msiExecutor.arg(argv[i]);
-    }
+    const auto args = SysInfo::getCommandArgs();
+    std::for_each(args.begin(), args.end(), [&msiExecutor] (const tstring& arg) {
+        msiExecutor.arg(arg);
+    });
 
     // Install msi file.
     return msiExecutor.execAndWaitForExit();
 
     JP_CATCH_ALL;
+
+    return -1;
 }