fix: Cannot start application when code page is 65001. Closes #814.

This commit may cause breaking change: changing working directory to the directory where the executable locates in.
This commit is contained in:
huanghongxun 2021-05-30 02:25:31 +08:00
parent 228695d511
commit 258a6628e4
6 changed files with 28 additions and 14 deletions

View File

@ -0,0 +1,2 @@
# Use the Google style in this project.
BasedOnStyle: Google

View File

@ -120,7 +120,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>false</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions);_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<ConformanceMode>false</ConformanceMode>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>

View File

@ -8,13 +8,13 @@ using namespace std;
Version J8(TEXT("8"));
void RawLaunchJVM(const wstring &javaPath, const wstring &jarPath)
void RawLaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath)
{
if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\""))
if (MyCreateProcess(L"\"" + javaPath + L"\" -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=15 -jar \"" + jarPath + L"\"", workdir))
exit(EXIT_SUCCESS);
}
void LaunchJVM(const wstring &javaPath, const wstring &jarPath)
void LaunchJVM(const wstring &javaPath, const wstring& workdir, const wstring &jarPath)
{
Version javaVersion(L"");
if (!MyGetFileVersionInfo(javaPath, javaVersion))
@ -22,7 +22,7 @@ void LaunchJVM(const wstring &javaPath, const wstring &jarPath)
if (J8 <= javaVersion)
{
RawLaunchJVM(javaPath, jarPath);
RawLaunchJVM(javaPath, workdir, jarPath);
}
}
@ -34,6 +34,13 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
if (ERROR_SUCCESS != MyGetModuleFileName(NULL, exeName))
return 1;
wstring workdir;
size_t last_slash = exeName.find_last_of(L"/\\");
if (last_slash != wstring::npos && last_slash + 1 < exeName.size()) {
workdir = exeName.substr(0, last_slash);
exeName = exeName.substr(last_slash + 1);
}
// TODO: check whether the bundled JRE is valid.
// First try the Java packaged together.
bool is64Bit = false;
@ -41,15 +48,15 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
if (is64Bit)
{
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", exeName);
RawLaunchJVM(L"jre-x64\\bin\\javaw.exe", workdir, exeName);
}
else
{
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", exeName);
RawLaunchJVM(L"jre-x86\\bin\\javaw.exe", workdir, exeName);
}
if (FindJava(path))
LaunchJVM(path + L"\\bin\\javaw.exe", exeName);
LaunchJVM(path + L"\\bin\\javaw.exe", workdir, exeName);
// Or we try to search Java in C:\Program Files.
{
@ -60,7 +67,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
do {
wstring javaw = wstring(L"C:\\Program Files\\Java\\") + data.cFileName + wstring(L"\\bin\\javaw.exe");
if (FindFirstFileExists(javaw.c_str(), 0)) {
LaunchJVM(javaw, exeName);
LaunchJVM(javaw, workdir, exeName);
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
@ -76,7 +83,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
do {
wstring javaw = wstring(L"C:\\Program Files (x86)\\Java\\") + data.cFileName + L"\\bin\\javaw.exe";
if (FindFirstFileExists(javaw.c_str(), 0)) {
LaunchJVM(javaw, exeName);
LaunchJVM(javaw, workdir, exeName);
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
@ -84,7 +91,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd
}
// Try java in PATH
RawLaunchJVM(L"javaw", exeName);
RawLaunchJVM(L"javaw", workdir, exeName);
MessageBox(NULL, ERROR_PROMPT, L"Error", MB_ICONERROR | MB_OK);
ShellExecute(0, 0, L"https://java.com/", 0, 0, SW_SHOW);

View File

@ -52,7 +52,7 @@ LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring & out)
}
}
bool MyCreateProcess(const std::wstring &command)
bool MyCreateProcess(const std::wstring &command, const std::wstring &workdir)
{
wstring writable_command = command;
STARTUPINFO si;
@ -61,7 +61,12 @@ bool MyCreateProcess(const std::wstring &command)
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
return (CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi));
if (workdir.empty()) {
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
}
else {
return CreateProcess(NULL, &writable_command[0], NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, workdir.c_str(), &si, &pi);
}
}
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter)

View File

@ -16,7 +16,7 @@ LSTATUS MyGetModuleFileName(HMODULE hModule, std::wstring &out);
LSTATUS MyGetEnvironmentVariable(LPCWSTR name, std::wstring &out);
// Create process by invoking CreateProcess, only pass command.
bool MyCreateProcess(const std::wstring &command);
bool MyCreateProcess(const std::wstring &command, const std::wstring& workdir);
// Check if file lpPath exists.
bool FindFirstFileExists(LPCWSTR lpPath, DWORD dwFilter);