Launch5j/src/head.c

569 lines
16 KiB
C
Raw Normal View History

2020-09-26 18:51:42 +02:00
/************************************************************/
/* Launch5j, by LoRd_MuldeR <MuldeR2@GMX.de> */
/* Java JAR wrapper for creating Windows native executables */
/* https://github.com/lordmulder/ */
/* */
/* This work has been released under the MIT license. */
/* Please see LICENSE.TXT for details! */
/* */
/* ACKNOWLEDGEMENT */
/* This project is partly inspired by the Launch4j project: */
/* https://sourceforge.net/p/launch4j/ */
/************************************************************/
#define WIN32_LEAN_AND_MEAN 1
#include <Windows.h>
2020-09-26 16:38:28 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <direct.h>
2020-09-26 18:51:42 +02:00
#include "resource.h"
2020-09-26 16:38:28 +02:00
2020-09-26 18:51:42 +02:00
// Const
static const DWORD SPLASH_SCREEN_TIMEOUT = 30000U;
2020-09-26 16:38:28 +02:00
static const wchar_t *const JRE_RELATIVE_PATH = L"runtime\\bin\\javaw.exe";
2020-09-26 18:51:42 +02:00
// Options
#ifndef ENABLE_SPLASH
#define ENABLE_SPLASH 1
#endif
#ifndef JAR_FILE_WRAPPED
#define JAR_FILE_WRAPPED 0
#endif
#ifndef WAIT_FOR_WINDOW
#define WAIT_FOR_WINDOW 1
#endif
#ifndef STAY_ALIVE
#define STAY_ALIVE 1
#endif
2020-09-26 18:51:42 +02:00
2020-09-26 16:38:28 +02:00
/* ======================================================================== */
/* String routines */
/* ======================================================================== */
#define NOT_EMPTY(STR) ((STR) && ((STR)[0U]))
static wchar_t *awprintf(const wchar_t *const fmt, ...)
{
va_list ap;
va_start(ap, fmt);
const int str_len = _vscwprintf(fmt, ap);
if (str_len < 1)
{
return NULL;
}
wchar_t *buffer = (wchar_t*) malloc(sizeof(wchar_t) * (((size_t)str_len) + 1U));
if (!buffer)
{
return NULL;
}
const int result = vswprintf(buffer, ((size_t)str_len) + 1U, fmt, ap);
if (result < 1)
{
free(buffer);
buffer = NULL;
}
va_end(ap);
return buffer;
}
static wchar_t *wcsndup (const wchar_t *const str, const size_t n)
{
size_t str_len = wcslen(str);
if (n < str_len)
{
str_len = n;
}
wchar_t *const result = (wchar_t*) malloc(sizeof(wchar_t) * (str_len + 1U));
if (!result)
{
return NULL;
}
wcsncpy(result, str, str_len);
result[str_len] = '\0';
return result;
}
/* ======================================================================== */
/* File name routines */
/* ======================================================================== */
static wchar_t *get_directory_part(const wchar_t *const path)
{
size_t lastsep = SIZE_MAX;
if(NOT_EMPTY(path))
{
for (size_t i = 0; path[i]; ++i)
{
if ((path[i] == L'\\') || (path[i] == L'/'))
{
lastsep = i;
}
}
}
if (lastsep != SIZE_MAX)
{
return wcsndup(path, lastsep);
}
return wcsdup(L".");
}
static wchar_t *remove_file_extension(const wchar_t *const path)
{
size_t lastsep = SIZE_MAX;
size_t lastdot = SIZE_MAX;
for (size_t i = 0; path[i]; ++i)
{
if ((path[i] == L'\\') || (path[i] == L'/'))
{
lastsep = i;
}
else if (path[i] == L'.')
{
lastdot = i;
}
}
if (lastdot != SIZE_MAX)
{
if((lastsep == SIZE_MAX) || (lastdot > lastsep))
{
return wcsndup(path, lastdot);
}
}
return wcsdup(path);
}
static BOOL file_exists(const wchar_t *const filename) {
struct _stat buffer;
if (_wstat(filename, &buffer) == 0)
{
return S_ISDIR(buffer.st_mode) ? FALSE : TRUE;
}
return FALSE;
}
/* ======================================================================== */
/* Path detection */
/* ======================================================================== */
static const wchar_t *const DEFAULT_JARFILE_NAME = L"application.jar";
static const wchar_t *get_executable_path(void)
{
if (_wpgmptr && _wpgmptr[0U])
{
return wcsdup(_wpgmptr);
}
return NULL;
}
static const wchar_t *get_executable_directory(const wchar_t *const executable_path)
{
const wchar_t *const directory_part = get_directory_part(executable_path);
if (directory_part)
{
return directory_part;
}
free((void*)directory_part);
return wcsdup(L".");
}
static const wchar_t *get_jarfile_path(const wchar_t *const executable_path, const wchar_t *const executable_directory)
{
#if JAR_FILE_WRAPPED
return wcsdup(executable_path); /*JAR file is wrapped*/
#else
2020-09-26 16:38:28 +02:00
const wchar_t *jarfile_path = NULL;
const wchar_t *const path_prefix = remove_file_extension(executable_path);
if (NOT_EMPTY(path_prefix))
{
const size_t len = wcslen(path_prefix);
if (!((len > 0U) && ((path_prefix[len-1U] == L'\\') || (path_prefix[len-1U] == L'/'))))
{
jarfile_path = awprintf(L"%ls.jar", path_prefix);
}
}
if (!jarfile_path)
{
jarfile_path = NOT_EMPTY(executable_directory) ? awprintf(L"%ls\\%ls", executable_directory, DEFAULT_JARFILE_NAME) : wcsdup(DEFAULT_JARFILE_NAME);
}
free((void*)path_prefix);
return jarfile_path;
2020-09-26 18:51:42 +02:00
#endif
2020-09-26 16:38:28 +02:00
}
/* ======================================================================== */
/* Path manipulation */
/* ======================================================================== */
static const BOOL set_current_directory(const wchar_t *const path)
{
if(NOT_EMPTY(path))
{
if(iswalpha(path[0U]) && (path[1U] == L':') && (path[2U] == L'\0'))
{
const wchar_t root_path[4U] = { path[0U], L':', L'\\', L'\0' };
return SetCurrentDirectoryW(root_path);
}
else
{
return SetCurrentDirectoryW(path);
}
}
else
{
return SetCurrentDirectoryW(L"\\");
}
}
2020-09-26 18:51:42 +02:00
/* ======================================================================== */
/* Splash screen */
/* ======================================================================== */
static BOOL create_splash_screen(const HWND hwnd, const HANDLE splash_image)
{
if (hwnd && splash_image)
{
RECT rect;
SendMessageW(hwnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) splash_image);
GetWindowRect(hwnd, &rect);
const int x = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
const int y = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE);
ShowWindow(hwnd, SW_SHOW);
return UpdateWindow(hwnd);
}
return FALSE;
}
static BOOL process_window_messages(const HWND hwnd)
{
BOOL result = FALSE;
if (hwnd != NULL)
{
MSG msg = {};
for (DWORD k = 0U; k < MAXWORD; ++k)
{
if (PeekMessageW(&msg, hwnd, 0U, 0U, PM_REMOVE))
{
result = TRUE;
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
else
{
break; /*no more messages!*/
}
}
}
return result;
}
/* ======================================================================== */
/* Find window functions */
/* ======================================================================== */
typedef struct
{
2020-09-26 22:30:12 +02:00
const DWORD process_id;
HWND hwnd;
}
find_window_t;
static BOOL CALLBACK enum_windows_callback(const HWND hwnd, const LPARAM lparam)
{
DWORD process_id = MAXDWORD;
find_window_t *const ptr = (find_window_t*) lparam;
if(IsWindowVisible(hwnd))
{
GetWindowThreadProcessId(hwnd, &process_id);
if(process_id == ptr->process_id)
{
ptr->hwnd = hwnd;
return FALSE;
}
}
return TRUE;
}
static HWND find_window_by_process_id(const DWORD process_id)
{
2020-09-26 22:30:12 +02:00
find_window_t find_window = { process_id, NULL };
EnumWindows(enum_windows_callback, (LONG_PTR)&find_window);
return find_window.hwnd;
}
/* ======================================================================== */
/* Wait for process */
/* ======================================================================== */
static BOOL signaled_or_failed(const DWORD wait_result)
{
return (wait_result == WAIT_OBJECT_0) || (wait_result == WAIT_FAILED);
}
static BOOL wait_for_process_ready(const HWND hwnd, const HANDLE process_handle, const DWORD process_id)
{
BOOL input_idle = FALSE;
const DWORD ticks_start = GetTickCount();
for (;;)
{
if (input_idle || signaled_or_failed(WaitForInputIdle(process_handle, 125U)))
{
const HWND child_hwnd = find_window_by_process_id(process_id);
if (child_hwnd)
{
SwitchToThisWindow(child_hwnd, TRUE);
return TRUE;
}
input_idle = TRUE;
}
if (signaled_or_failed(WaitForSingleObject(process_handle, 1U)))
{
break;
}
const DWORD ticks_delta = GetTickCount() - ticks_start;
if(ticks_delta > SPLASH_SCREEN_TIMEOUT)
{
break;
}
process_window_messages(hwnd);
}
return FALSE;
}
2020-09-26 16:38:28 +02:00
/* ======================================================================== */
/* Message box */
/* ======================================================================== */
static const wchar_t *describe_system_error(const DWORD error_code)
{
const wchar_t *error_test = NULL, *buffer = NULL;
const DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&buffer, 0, NULL);
if((len > 0U) && NOT_EMPTY(buffer))
{
error_test = wcsdup(buffer);
LocalFree((HLOCAL)buffer);
}
return error_test;
}
#define show_message(HWND, FLAGS, TITLE, TEXT) \
{ \
MessageBoxW((HWND), (TEXT), (TITLE), (FLAGS)); \
} \
while(0)
#define show_message_format(HWND, FLAGS, TITLE, FORMAT, ...) do \
{ \
const wchar_t *const _text = awprintf((FORMAT), __VA_ARGS__); \
if(_text) \
{ \
MessageBoxW((HWND), _text, (TITLE), (FLAGS)); \
} \
free((void*)_text); \
} \
while(0)
2020-09-26 18:51:42 +02:00
/* ======================================================================== */
/* Utilities */
/* ======================================================================== */
static void close_handle(HANDLE *const handle)
{
if(*handle)
{
CloseHandle(*handle);
*handle = NULL;
}
}
static void delete_object(HGDIOBJ *const handle)
{
if(*handle)
{
DeleteObject(*handle);
*handle = NULL;
}
}
static void destroy_window(HWND *const hwnd)
{
if(*hwnd)
{
DestroyWindow(*hwnd);
*hwnd = NULL;
}
}
2020-09-26 16:38:28 +02:00
/* ======================================================================== */
/* MAIN */
/* ======================================================================== */
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
2020-09-26 18:51:42 +02:00
int result = -1;
2020-09-26 16:38:28 +02:00
const wchar_t *executable_path = NULL, *executable_directory = NULL, *jarfile_path = NULL, *java_runtime_path = NULL, *command_line = NULL;
2020-09-26 18:51:42 +02:00
HGDIOBJ splash_image = NULL;
2020-09-26 16:38:28 +02:00
PROCESS_INFORMATION process_info;
2020-09-26 18:51:42 +02:00
STARTUPINFOW startup_info;
2020-09-26 16:38:28 +02:00
// Initialize
SecureZeroMemory(&startup_info, sizeof(STARTUPINFOW));
SecureZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
2020-09-26 18:51:42 +02:00
// Create the window
HWND hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_TOPMOST, L"STATIC", L"", WS_POPUP | SS_BITMAP, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
// Show the splash screen
#if ENABLE_SPLASH
2020-09-26 18:51:42 +02:00
if(splash_image = LoadImage(hInstance, MAKEINTRESOURCE(ID_SPLASH_BITMAP), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE))
{
if(create_splash_screen(hwnd, splash_image))
{
process_window_messages(hwnd);
}
}
#endif
2020-09-26 16:38:28 +02:00
// Find executable path
if(!(executable_path = get_executable_path()))
{
2020-09-26 18:51:42 +02:00
show_message(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"The path of the executable could not be determined!");
2020-09-26 16:38:28 +02:00
goto cleanup;
}
// Find executable directory
if(!(executable_directory = get_executable_directory(executable_path)))
{
2020-09-26 18:51:42 +02:00
show_message(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"The executable directory could not be determined!");
2020-09-26 16:38:28 +02:00
goto cleanup;
}
// Set the current directory
if(_wcsicmp(executable_directory, L".") != 0)
{
set_current_directory(executable_directory);
}
// Find the JAR file path
if(!(jarfile_path = get_jarfile_path(executable_path, executable_directory)))
{
2020-09-26 18:51:42 +02:00
show_message(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"The path of the JAR file could not be determined!");
2020-09-26 16:38:28 +02:00
goto cleanup;
}
2020-09-26 18:51:42 +02:00
// Find the Java runtime executable path
2020-09-26 16:38:28 +02:00
if(!(java_runtime_path = awprintf(L"%ls\\%ls", executable_directory, JRE_RELATIVE_PATH)))
{
2020-09-26 18:51:42 +02:00
show_message(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"The path of the Java runtime could not be determined!");
2020-09-26 16:38:28 +02:00
goto cleanup;
}
2020-09-26 18:51:42 +02:00
// Does the JAR file exist?
#if !JAR_FILE_WRAPPED
2020-09-26 16:38:28 +02:00
if(!file_exists(jarfile_path))
{
2020-09-26 18:51:42 +02:00
show_message_format(hwnd, MB_ICONERROR | MB_TOPMOST, L"JAR not found", L"The required JAR file could not be found:\n\n%ls\n\n\nRe-installing the application may fix the problem!", jarfile_path);
2020-09-26 16:38:28 +02:00
goto cleanup;
}
2020-09-26 18:51:42 +02:00
#endif
2020-09-26 16:38:28 +02:00
2020-09-26 18:51:42 +02:00
// Does the Java runtime executable exist?
2020-09-26 16:38:28 +02:00
if(!file_exists(java_runtime_path))
{
2020-09-26 18:51:42 +02:00
show_message_format(hwnd, MB_ICONERROR | MB_TOPMOST, L"Java not found", L"The required Java runtime could not be found:\n\n%ls\n\n\nRe-installing the application may fix the problem!", java_runtime_path);
2020-09-26 16:38:28 +02:00
goto cleanup;
}
// Build the command-line
command_line = NOT_EMPTY(pCmdLine) ? awprintf(L"\"%ls\" -jar \"%ls\" %ls", java_runtime_path, jarfile_path, pCmdLine) : awprintf(L"\"%ls\" -jar \"%ls\"", java_runtime_path, jarfile_path);
if(!command_line)
{
2020-09-26 18:51:42 +02:00
show_message(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"The Java command-line could not be generated!");
2020-09-26 16:38:28 +02:00
goto cleanup;
}
2020-09-26 18:51:42 +02:00
// Process pending window messages
#if ENABLE_SPLASH
2020-09-26 18:51:42 +02:00
process_window_messages(hwnd);
#endif
2020-09-26 16:38:28 +02:00
// Now actually start the process!
if(!CreateProcessW(NULL, (LPWSTR)command_line, NULL, NULL, FALSE, 0U, NULL, executable_directory, &startup_info, &process_info))
{
const wchar_t *const error_text = describe_system_error(GetLastError());
if(error_text)
{
2020-09-26 18:51:42 +02:00
show_message_format(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"Failed to create the Java process:\n\n%ls\n\n\n%ls", command_line, error_text);
2020-09-26 16:38:28 +02:00
free((void*)error_text);
}
else
{
2020-09-26 18:51:42 +02:00
show_message_format(hwnd, MB_ICONERROR | MB_TOPMOST, L"System Error", L"Failed to create the Java process:\n\n%ls", command_line);
2020-09-26 16:38:28 +02:00
}
goto cleanup;
}
2020-09-26 18:51:42 +02:00
// Process pending window messages
#if ENABLE_SPLASH
2020-09-26 18:51:42 +02:00
process_window_messages(hwnd);
#if WAIT_FOR_WINDOW
wait_for_process_ready(hwnd, process_info.hProcess, process_info.dwProcessId);
2020-09-26 18:51:42 +02:00
#endif
destroy_window(&hwnd);
#endif
2020-09-26 16:38:28 +02:00
2020-09-26 22:30:12 +02:00
// Wait for process to exit, then get the exit code
#if STAY_ALIVE
2020-09-26 22:30:12 +02:00
if(signaled_or_failed(WaitForSingleObject(process_info.hProcess, INFINITE)))
2020-09-26 16:38:28 +02:00
{
DWORD exit_code = 0U;
if(GetExitCodeProcess(process_info.hProcess, &exit_code))
{
result = (int) exit_code;
}
2020-09-26 16:38:28 +02:00
}
#else
result = 0;
#endif
2020-09-26 16:38:28 +02:00
2020-09-26 18:51:42 +02:00
cleanup:
close_handle(&process_info.hThread);
close_handle(&process_info.hProcess);
destroy_window(&hwnd);
delete_object(splash_image);
2020-09-26 16:38:28 +02:00
free((void*)command_line);
free((void*)java_runtime_path);
free((void*)jarfile_path);
free((void*)executable_directory);
free((void*)executable_path);
2020-09-26 18:51:42 +02:00
return result;
2020-09-26 16:38:28 +02:00
}