format + mingw-ify + makefile

This commit is contained in:
Mylloon 2024-08-21 12:25:31 +02:00
parent 9eede46b61
commit 898cba7a72
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 93 additions and 70 deletions

4
.clangd Normal file
View file

@ -0,0 +1,4 @@
CompileFlags:
Add: [
--target=x86_64-w64-mingw32-gcc,
]

1
.gitignore vendored
View file

@ -32,6 +32,7 @@ luac.out
*.so *.so
*.so.* *.so.*
*.dylib *.dylib
*.dll
# Executables # Executables
*.out *.out

18
Makefile Normal file
View file

@ -0,0 +1,18 @@
CC = x86_64-w64-mingw32-gcc
RM = rm
CFLAGS = -shared -lpsapi -luser32
EXES = detect_game
EXT = .dll
all: $(EXES)
$(EXES): %: %.c $(DEPS)
$(CC) $^ -o $@$(EXT) $(CFLAGS)
%.o: %.c
$(CC) -c $< -o $@ $(CFLAGS)
clean:
@$(RM) *.o $(addsuffix $(EXT), $(EXES)) 2>/dev/null |:

View file

@ -1,95 +1,95 @@
#include "pch.h" #include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule, BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
DWORD ul_reason_for_call, LPVOID lpReserved) {
LPVOID lpReserved switch (ul_reason_for_call) {
) case DLL_PROCESS_ATTACH:
{ case DLL_THREAD_ATTACH:
switch (ul_reason_for_call) case DLL_THREAD_DETACH:
{ case DLL_PROCESS_DETACH:
case DLL_PROCESS_ATTACH: break;
case DLL_THREAD_ATTACH: }
case DLL_THREAD_DETACH: return TRUE;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
} }
#include <Windows.h> #include <psapi.h>
#include <Psapi.h>
#include <tchar.h>
#include <stdlib.h> #include <stdlib.h>
#include <tchar.h>
#define MAX_TITLE_LENGTH 256 #define MAX_TITLE_LENGTH 256
static const char prefix[] = "C:\\Windows\\"; static const char prefix[] = "C:\\Windows\\";
static const char gameBarExe[] = "GameBar.exe"; static const char gameBarExe[] = "GameBar.exe";
BOOL TestFullscreen(HWND hwnd) { BOOL TestFullscreen(HWND hwnd) {
WINDOWPLACEMENT wp; WINDOWPLACEMENT wp;
wp.length = sizeof(WINDOWPLACEMENT); wp.length = sizeof(WINDOWPLACEMENT);
if (!GetWindowPlacement(hwnd, &wp)) if (!GetWindowPlacement(hwnd, &wp)) {
return FALSE; return FALSE;
if (IsZoomed(hwnd)) }
return TRUE; if (IsZoomed(hwnd)) {
RECT rcDesktop; return TRUE;
GetClientRect(GetDesktopWindow(), &rcDesktop); }
return (wp.rcNormalPosition.left <= rcDesktop.left && RECT rcDesktop;
wp.rcNormalPosition.top <= rcDesktop.top && GetClientRect(GetDesktopWindow(), &rcDesktop);
wp.rcNormalPosition.right >= rcDesktop.right && return (wp.rcNormalPosition.left <= rcDesktop.left &&
wp.rcNormalPosition.bottom >= rcDesktop.bottom); wp.rcNormalPosition.top <= rcDesktop.top &&
wp.rcNormalPosition.right >= rcDesktop.right &&
wp.rcNormalPosition.bottom >= rcDesktop.bottom);
} }
void ConvertTCHARToChar(const TCHAR* source, char* dest, size_t destSize) { void ConvertTCHARToChar(const TCHAR *source, char *dest, size_t destSize) {
#ifdef _WIN32 #ifdef _UNICODE
wcstombs_s(NULL, dest, destSize, source, _TRUNCATE); wcstombs_s(NULL, dest, destSize, source, _TRUNCATE);
#else #else
wcstombs(dest, source, destSize); strncpy_s(dest, destSize, source, _TRUNCATE);
#endif #endif
} }
__declspec(dllexport) int get_running_fullscreen_game_path(char* buffer, int bufferSize) { __declspec(dllexport) int get_running_fullscreen_game_path(char *buffer,
HWND hwnd = NULL; int bufferSize) {
while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) { HWND hwnd = NULL;
if (TestFullscreen(hwnd)) { while ((hwnd = FindWindowEx(NULL, hwnd, NULL, NULL)) != NULL) {
TCHAR windowTitle[MAX_TITLE_LENGTH]; if (TestFullscreen(hwnd)) {
GetWindowText(hwnd, windowTitle, MAX_TITLE_LENGTH); TCHAR windowTitle[MAX_TITLE_LENGTH];
GetWindowText(hwnd, windowTitle, MAX_TITLE_LENGTH);
DWORD processId; DWORD processId;
GetWindowThreadProcessId(hwnd, &processId); GetWindowThreadProcessId(hwnd, &processId);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId); HANDLE hProcess =
if (hProcess == NULL) { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, processId);
return 1; if (hProcess == NULL) {
} return 1;
}
TCHAR executablePath[MAX_PATH]; TCHAR executablePath[MAX_PATH];
if (GetModuleFileNameEx(hProcess, NULL, executablePath, MAX_PATH) == 0) { if (GetModuleFileNameEx(hProcess, NULL, executablePath, MAX_PATH) == 0) {
CloseHandle(hProcess); CloseHandle(hProcess);
return 1; return 1;
} }
CloseHandle(hProcess); CloseHandle(hProcess);
size_t exe_bufferSize = sizeof(executablePath) / sizeof(executablePath[0]); size_t exe_bufferSize =
sizeof(executablePath) / sizeof(executablePath[0]);
char* charPath = (char*)malloc(exe_bufferSize); char *charPath = (char *)malloc(exe_bufferSize);
ConvertTCHARToChar(executablePath, charPath, exe_bufferSize); ConvertTCHARToChar(executablePath, charPath, exe_bufferSize);
int result = strncmp(charPath, prefix, 11); int result = strncmp(charPath, prefix, 11);
if (result == 0) { if (result == 0) {
continue; continue;
} }
result = strcmp(charPath + strlen(charPath) - 11, gameBarExe); result = strcmp(charPath + strlen(charPath) - 11, gameBarExe);
if (result == 0) { if (result == 0) {
continue; continue;
} }
strcpy_s(buffer, bufferSize, charPath); // Use charPath as a regular char array
free(charPath);
return 0;
} // Use charPath as a regular char array
} strcpy_s(buffer, bufferSize, charPath);
return 1; free(charPath);
return 0;
}
}
return 1;
} }

Binary file not shown.