2023-09-27 14:14:27 +02:00
|
|
|
/**
|
|
|
|
* This is a simple forwarder that launches the main ImHex executable with the
|
|
|
|
* same command line as the current process. The reason for this is that even
|
|
|
|
* though ImHex is a GUI application in general, it also has a command line
|
|
|
|
* interface that can be used to perform various tasks. The issue with this is
|
|
|
|
* that kernel32 will automatically allocate a console for us which will float
|
|
|
|
* around in the background if we launch ImHex from the explorer. This forwarder
|
|
|
|
* will get rid of the console window if ImHex was launched from the explorer and
|
|
|
|
* enables ANSI escape sequences if ImHex was launched from the command line.
|
|
|
|
*
|
|
|
|
* The main reason this is done in a separate executable is because we use FreeConsole()
|
|
|
|
* to get rid of the console window. Due to bugs in older versions of Windows (Windows 10 and older)
|
|
|
|
* this will also close the process's standard handles (stdin, stdout, stderr) in
|
|
|
|
* a way that cannot be recovered from. This means that if we were to do this in the
|
|
|
|
* main application, if any code would try to interact with these handles (duplicate them
|
|
|
|
* or modify them in any way), the application would crash.
|
|
|
|
*
|
|
|
|
* None of this would be necessary if Windows had a third type of application (besides
|
|
|
|
* console and GUI) that would act like a console application but would not allocate
|
|
|
|
* a console window. This would allow us to have a single executable that would work
|
|
|
|
* the same as on all other platforms. There are plans to add this to Windows in the
|
|
|
|
* future, but it is not yet available. Also even if it was available, it would not
|
|
|
|
* be available on older versions of Windows, so we would still need this forwarder
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <wolv/io/fs.hpp>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
void setupConsoleWindow() {
|
|
|
|
// Get the handle of the console window
|
2023-09-27 14:14:27 +02:00
|
|
|
HWND consoleWindow = ::GetConsoleWindow();
|
2023-10-21 20:40:24 +02:00
|
|
|
|
|
|
|
// Get console process ID
|
2023-09-27 14:14:27 +02:00
|
|
|
DWORD processId = 0;
|
|
|
|
::GetWindowThreadProcessId(consoleWindow, &processId);
|
|
|
|
|
|
|
|
// Check if ImHex was launched from the explorer or from the command line
|
|
|
|
if (::GetCurrentProcessId() == processId) {
|
|
|
|
// If it was launched from the explorer, kernel32 has allocated a console for us
|
|
|
|
// Get rid of it to avoid having a useless console window floating around
|
|
|
|
::FreeConsole();
|
|
|
|
} else {
|
|
|
|
// If it was launched from the command line, enable ANSI escape sequences to have colored output
|
|
|
|
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (hConsole != INVALID_HANDLE_VALUE) {
|
|
|
|
DWORD mode = 0;
|
|
|
|
if (::GetConsoleMode(hConsole, &mode) == TRUE) {
|
|
|
|
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT;
|
|
|
|
::SetConsoleMode(hConsole, mode);
|
|
|
|
}
|
|
|
|
}
|
2023-10-21 20:40:24 +02:00
|
|
|
|
|
|
|
// Set the __IMHEX_FORWARD_CONSOLE__ environment variable,
|
|
|
|
// to let ImHex know that it was launched from the forwarder
|
|
|
|
// and that it should forward it's console output to us
|
|
|
|
::SetEnvironmentVariableA("__IMHEX_FORWARD_CONSOLE__", "1");
|
2023-09-27 14:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int launchExecutable() {
|
|
|
|
// Get the path of the main ImHex executable
|
|
|
|
auto executablePath = wolv::io::fs::getExecutablePath();
|
|
|
|
auto executableFullPath = executablePath->parent_path() / "imhex-gui.exe";
|
|
|
|
|
|
|
|
::PROCESS_INFORMATION process = { };
|
2023-10-21 20:40:24 +02:00
|
|
|
::STARTUPINFOW startupInfo = { .cb = sizeof(::STARTUPINFOW) };
|
2023-09-27 14:14:27 +02:00
|
|
|
|
|
|
|
// Create a new process for imhex-gui.exe with the same command line as the current process
|
|
|
|
if (::CreateProcessW(executableFullPath.wstring().c_str(), ::GetCommandLineW(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &startupInfo, &process) == FALSE) {
|
|
|
|
// Handle error if the process could not be created
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
// Get formatted error message from the OS
|
2023-09-27 14:14:27 +02:00
|
|
|
auto errorCode = ::GetLastError();
|
2023-11-10 20:47:08 +01:00
|
|
|
auto errorMessageString = std::system_category().message(int(errorCode));
|
2023-09-27 14:14:27 +02:00
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
// Generate error message
|
2023-09-27 14:14:27 +02:00
|
|
|
auto errorMessage = fmt::format("Failed to start ImHex:\n\nError code: 0x{:08X}\n\n{}", errorCode, errorMessageString);
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
// Display a message box with the error
|
2023-09-27 14:14:27 +02:00
|
|
|
::MessageBoxA(nullptr, errorMessage.c_str(), "ImHex Forwarder", MB_OK | MB_ICONERROR);
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
// Wait for the main ImHex process to exit
|
2023-09-27 14:14:27 +02:00
|
|
|
::WaitForSingleObject(process.hProcess, INFINITE);
|
|
|
|
::CloseHandle(process.hProcess);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2023-10-21 20:40:24 +02:00
|
|
|
setupConsoleWindow();
|
2023-09-27 14:14:27 +02:00
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
return launchExecutable();
|
2023-09-27 14:14:27 +02:00
|
|
|
}
|