From 4a0b27e7ec1ed41120158efcc6328ed9878693b1 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 14 Feb 2013 15:25:31 +0100 Subject: [PATCH] Windows launcher: command line arguments --- .../WinLauncher/WinLauncher/WinLauncher.cpp | 57 +++++++++++++----- native/WinLauncher/WinLauncher/WinLauncher.rc | Bin 4998 -> 5000 bytes native/WinLauncher/WinLauncher/stdafx.h | 1 + 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/native/WinLauncher/WinLauncher/WinLauncher.cpp b/native/WinLauncher/WinLauncher/WinLauncher.cpp index d74e65f1c00d..c9a83dd018c1 100644 --- a/native/WinLauncher/WinLauncher/WinLauncher.cpp +++ b/native/WinLauncher/WinLauncher/WinLauncher.cpp @@ -29,15 +29,14 @@ JNI_createJavaVM pCreateJavaVM = NULL; JavaVM* jvm = NULL; JNIEnv* env = NULL; -bool IsValidJRE(char* path) +bool IsValidJRE(const char* path) { - char dllPath[_MAX_PATH]; - strcpy_s(dllPath, path); - strcat_s(dllPath, "\\bin\\client\\jvm.dll"); - return GetFileAttributesA(dllPath) != INVALID_FILE_ATTRIBUTES; + std::string dllPath(path); + dllPath += "\\bin\\client\\jvm.dll"; + return GetFileAttributesA(dllPath.c_str()) != INVALID_FILE_ATTRIBUTES; } -bool FindValidJVM(char* path) +bool FindValidJVM(const char* path) { if (IsValidJRE(path)) { @@ -59,6 +58,16 @@ bool FindValidJVM(char* path) return false; } +std::string GetAdjacentDir(const char* suffix) +{ + char libDir[_MAX_PATH]; + GetCurrentDirectoryA(_MAX_PATH-1, libDir); + char* lastSlash = strrchr(libDir, '\\'); + strcpy(lastSlash+1, suffix); + strcat_s(libDir, "\\"); + return std::string(libDir); +} + bool LocateJVM() { char envVarName[_MAX_PATH]; @@ -81,6 +90,13 @@ bool LocateJVM() } } } + + std::string jreDir = GetAdjacentDir("jre"); + if (FindValidJVM(jreDir.c_str())) + { + return true; + } + return false; } @@ -106,6 +122,7 @@ bool LoadVMOptionsFile(const TCHAR* path, std::vector& vmOptionLine while(fgets(line, _MAX_PATH-1, f)) { TrimLine(line); + if (line[0] == '#') continue; if (strcmp(line, "-server") == 0) { bServerJVM = true; @@ -138,11 +155,8 @@ std::string FindToolsJar() std::string BuildClassPath() { - char libDir[_MAX_PATH]; - GetCurrentDirectoryA(_MAX_PATH-1, libDir); - char* lastSlash = strrchr(libDir, '\\'); - strcpy(lastSlash+1, "lib\\"); - if (GetFileAttributesA(libDir) == INVALID_FILE_ATTRIBUTES) + std::string libDir = GetAdjacentDir("lib"); + if (GetFileAttributesA(libDir.c_str()) == INVALID_FILE_ATTRIBUTES) { return ""; } @@ -260,6 +274,20 @@ bool CreateJVM() return result == JNI_OK; } +jobjectArray PrepareCommandLine() +{ + int numArgs; + LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &numArgs); + jclass stringClass = env->FindClass("java/lang/String"); + jobjectArray args = env->NewObjectArray(numArgs-1, stringClass, NULL); + for(int i=1; iSetObjectArrayElement(args, i-1, env->NewString((const jchar *) arg, wcslen(argv[i]))); + } + return args; +} + bool RunMainClass() { char mainClassName[_MAX_PATH]; @@ -268,20 +296,19 @@ bool RunMainClass() if (!mainClass) { char buf[_MAX_PATH]; - sprintf(buf, "Could not find main class %s", mainClassName); + sprintf_s(buf, "Could not find main class %s", mainClassName); MessageBoxA(NULL, buf, "Error Launching IntelliJ Platform", MB_OK); return false; } - jclass stringClass = env->FindClass("java/lang/String"); - jobjectArray args = env->NewObjectArray(0, stringClass, NULL); jmethodID mainMethod = env->GetStaticMethodID(mainClass, "main", "([Ljava/lang/String;)V"); - if (!mainClass) + if (!mainMethod) { MessageBoxA(NULL, "Could not find main method", "Error Launching IntelliJ Platform", MB_OK); return false; } + jobjectArray args = PrepareCommandLine(); env->CallStaticVoidMethod(mainClass, mainMethod, args); return true; diff --git a/native/WinLauncher/WinLauncher/WinLauncher.rc b/native/WinLauncher/WinLauncher/WinLauncher.rc index a421f77dbf835825e3119b79453124ebfc9a3eaf..06498b703356d205d76b0fe36f04ebfa8b59522c 100644 GIT binary patch delta 14 VcmZou?@-^s#K&l~nUSxR6#yQ)1DpT= delta 16 XcmeBBZ&TmE#5Z{puh?cOzC2a{EtUk+ diff --git a/native/WinLauncher/WinLauncher/stdafx.h b/native/WinLauncher/WinLauncher/stdafx.h index c82f7764d68e..1d0001690ab0 100644 --- a/native/WinLauncher/WinLauncher/stdafx.h +++ b/native/WinLauncher/WinLauncher/stdafx.h @@ -10,6 +10,7 @@ #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files: #include +#include // C RunTime Header Files #include