Skip to content

Commit

Permalink
Hide console in Windows if not launched from CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
bullno1 committed Nov 2, 2024
1 parent 6c57409 commit 750a553
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ compile shims/linux/gl.c ${LINUX_FLAGS}
compile shims/linux/x11.c ${LINUX_FLAGS}

compile nvapi/nvapi.c ${WIN32_FLAGS}
compile win32_tweaks.c ${WIN32_FLAGS}

compile main.c ${FLAGS} -Invapi

Expand Down
7 changes: 6 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "util/sokol_imgui.h"
#include <cosmo.h>
#include <nvapi.h>
#include "win32_tweaks.h"

typedef struct {
uint64_t last_time;
Expand Down Expand Up @@ -102,7 +103,11 @@ void input(const sapp_event* event) {

int main(int argc, char* argv[]) {
ShowCrashReports();
nvapi_disable_threaded_optimization("cosmo-sokol");

if (IsWindows()) {
win32_tweaks_hide_console();
nvapi_disable_threaded_optimization("cosmo-sokol");
}

sapp_desc app = {
.init_cb = init,
Expand Down
3 changes: 2 additions & 1 deletion nvapi/nvapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdbool.h>

bool nvapi_disable_threaded_optimization(const char* profile_name);
bool
nvapi_disable_threaded_optimization(const char* profile_name);

#endif
17 changes: 17 additions & 0 deletions win32_tweaks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "win32_tweaks.h"
#include <windowsesque.h>

void
win32_tweaks_hide_console(void) {
HMODULE kernel32 = LoadLibraryA("kernel32.dll");
if (kernel32 == NULL) { return; }

DWORD WINAPI (*GetConsoleProcessList)(LPDWORD lpdwProcessList, DWORD dwProcessCount) = GetProcAddress(kernel32, "GetConsoleProcessList");
if (GetConsoleProcessList == NULL) { return; }

DWORD pids[2];
DWORD num_procs = GetConsoleProcessList(pids, 2);
if (num_procs <= 1) {
FreeConsole();
}
}
7 changes: 7 additions & 0 deletions win32_tweaks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef WIN32_TWEAKS
#define WIN32_TWEAKS

void
win32_tweaks_hide_console(void);

#endif

0 comments on commit 750a553

Please sign in to comment.