From 15d817221f010bae58929c72891f3676637fe88b Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Sun, 31 Jul 2022 21:02:11 +0200 Subject: [PATCH] need to hack stderr for non-console build --- bnk-extract/defs.h | 3 ++- gui.c | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bnk-extract/defs.h b/bnk-extract/defs.h index 67626d5..d2560fa 100644 --- a/bnk-extract/defs.h +++ b/bnk-extract/defs.h @@ -49,7 +49,8 @@ typedef struct { #else #define dprintf(...) #endif -#define eprintf(...) fprintf(stderr, __VA_ARGS__) +extern FILE* consoleless_stderr; +#define eprintf(...) fprintf(consoleless_stderr ? consoleless_stderr : stderr, __VA_ARGS__) #define v_printf(level, ...) if (VERBOSE >= level) printf(__VA_ARGS__) #ifdef __cplusplus diff --git a/gui.c b/gui.c index 12fe0cb..d1bde08 100644 --- a/gui.c +++ b/gui.c @@ -14,6 +14,8 @@ #include "treeview_extension.h" #include "bnk-extract/api.h" +FILE* consoleless_stderr; + // global window variables static HINSTANCE me; static HWND mainWindow; @@ -250,6 +252,9 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) // redirect stderr to a temporary file, in order to be able to read it back and display later FILE* temp_file = tmpfile(); dup2(fileno(temp_file), STDERR_FILENO); + if (fileno(stderr) == -2) { // no console, but we still want to capture stderr + consoleless_stderr = temp_file; + } WemInformation* wemInformation = bnk_extract(onlyAudioGiven ? 3 : 7, bnk_extract_args); free(binPath); free(audioPath); free(eventsPath); if (wemInformation) { @@ -268,6 +273,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) MessageBox(mainWindow, stderr_buffer, "Failed to read audio files", MB_ICONERROR); } fclose(temp_file); + consoleless_stderr = NULL; // restore stderr dup2(duped_stderr, STDERR_FILENO); } else if ((HWND) lParam == XButton) {