Add simple logging
This commit is contained in:
1 parent
4bfb5b958e
commit
03a1a9c957
5 files changed
+71
-2
No files matched your search
@@ -12,6 +12,9 @@ set(CMAKE_CXX_STANDARD 20)
|
||||
add_executable(bconfig config.c config.rc)
|
||||
target_link_libraries(bconfig comctl32)
|
||||
|
||||
add_executable(blog log.c)
|
||||
add_executable(blogtest logtest.c)
|
||||
|
||||
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
|
||||
add_library(user32 SHARED user32.c)
|
||||
target_link_libraries(user32 -static-libgcc)
|
||||
|
||||
@@ -11,4 +11,25 @@
|
||||
|
||||
#define getFunction(dllName, functionName) GetProcAddress(GetModuleHandleA(dllName), functionName)
|
||||
|
||||
#ifndef DLL_NAME
|
||||
#define DLL_NAME "Unknown"
|
||||
#endif
|
||||
|
||||
void logMSG(LPCSTR msg, LPCSTR level) {
|
||||
char buffer[512];
|
||||
char name[128];
|
||||
GetModuleFileName(NULL, name, sizeof(name));
|
||||
|
||||
typedef int (__cdecl *sprintfPtr)(char* stream, const char* format, ...);
|
||||
sprintfPtr sprintf = (sprintfPtr) getFunction("msvcrt.dll", "sprintf");
|
||||
sprintf(buffer, "[%s] [%s | %s] %s", level, name, DLL_NAME, msg);
|
||||
|
||||
CallNamedPipe(TEXT("\\\\.\\pipe\\bcompat"), buffer, sizeof(buffer), NULL, 0, NULL, NMPWAIT_NOWAIT);
|
||||
}
|
||||
|
||||
#define logDebug(msg) logMSG(msg, "DEBUG");
|
||||
#define logInfo(msg) logMSG(msg, "INFO");
|
||||
#define logWarn(msg) logMSG(msg, "WARN");
|
||||
#define logError(msg) logMSG(msg, "ERROR");
|
||||
|
||||
#endif //BCOMPAT_COMMON_H
|
||||
+6
-2
@@ -1,5 +1,7 @@
|
||||
#define DLL_NAME "kernel32.dll"
|
||||
#include "common.h"
|
||||
#include "kernel32.h"
|
||||
#include <tlhelp32.h>
|
||||
|
||||
typedef LPWCH (__cdecl *wcscpyPtr)(LPWCH dest, LPWCH source);
|
||||
typedef size_t (__cdecl *wcstombsPtr)(LPSTR dest, LPWCH source, size_t maxCount);
|
||||
@@ -8,14 +10,13 @@ typedef LONG (WINAPI *RegQueryValueExWPtr)(HKEY hKey, LPCWSTR lpValueName, LPDWO
|
||||
typedef LONG (WINAPI *RegCloseKeyPtr)(HKEY hKey);
|
||||
|
||||
DLL_EXPORT WINAPI BOOL GetVersionExW(LPOSVERSIONINFOW version) {
|
||||
logDebug("GetVersionEx: Wrapping Call")
|
||||
if (version->dwOSVersionInfoSize == sizeof(OSVERSIONINFOW)) {
|
||||
version->dwMajorVersion = 4;
|
||||
version->dwMinorVersion = 0;
|
||||
version->dwBuildNumber = 1381;
|
||||
version->dwPlatformId = VER_PLATFORM_WIN32_NT;
|
||||
version->szCSDVersion[0] = L'\0';
|
||||
//wcscpyPtr wcscpy = (wcscpyPtr) getFunction("msvcrt.dll", "wcscpy");
|
||||
//wcscpy(version->szCSDVersion, L"Befator Inc gruesst Sie!");
|
||||
|
||||
RegOpenKeyExWPtr regOpenKeyExW = (RegOpenKeyExWPtr) getFunction("advapi32.dll", "RegOpenKeyExW");
|
||||
RegQueryValueExWPtr regQueryValueExW = (RegQueryValueExWPtr) getFunction("advapi32.dll", "RegQueryValueExW");
|
||||
@@ -47,6 +48,7 @@ DLL_EXPORT WINAPI BOOL GetVersionExW(LPOSVERSIONINFOW version) {
|
||||
}
|
||||
return TRUE;
|
||||
} else {
|
||||
logWarn("GetVersionExW: Invalid Buffer Size")
|
||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||
return FALSE;
|
||||
}
|
||||
@@ -68,12 +70,14 @@ DLL_EXPORT WINAPI BOOL GetVersionExA(LPOSVERSIONINFOA version) {
|
||||
}
|
||||
return FALSE;
|
||||
} else {
|
||||
logWarn("GetVersionExA: Invalid Buffer Size")
|
||||
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
DLL_EXPORT WINAPI DWORD GetVersion() {
|
||||
logDebug("GetVersion: Legacy Function called")
|
||||
OSVERSIONINFOW versionW;
|
||||
if (GetVersionExW(&versionW)) {
|
||||
return (versionW.dwPlatformId ^ 2) << 30 |
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <windows.h>
|
||||
|
||||
//From msvcrt.dll
|
||||
int __declspec(dllimport) __cdecl printf (const char* format, ...);
|
||||
int __declspec(dllimport) __cdecl sprintf (char* stream, const char* format, ...);
|
||||
|
||||
int main() {
|
||||
HANDLE pipe = CreateNamedPipe(TEXT("\\\\.\\pipe\\bcompat"), PIPE_ACCESS_DUPLEX,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1, 512, 512, 0, NULL);
|
||||
if (pipe == INVALID_HANDLE_VALUE) {
|
||||
DWORD error = GetLastError();
|
||||
if (error == ERROR_ACCESS_DENIED) {
|
||||
MessageBox(NULL, TEXT("Another instance is already running"), TEXT("BCompat Log"), MB_OK | MB_ICONERROR);
|
||||
} else {
|
||||
TCHAR buffer[128];
|
||||
sprintf(buffer, TEXT("Error opening logging pipe: 0x%lx"), error);
|
||||
MessageBox(NULL, buffer, TEXT("BCompat Log"), MB_OK | MB_ICONERROR);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (TRUE) {
|
||||
if (ConnectNamedPipe(pipe, NULL) || GetLastError() == ERROR_PIPE_CONNECTED) {
|
||||
char buffer[512];
|
||||
DWORD read;
|
||||
if (ReadFile(pipe, buffer, sizeof(buffer), &read, NULL)) {
|
||||
printf("%s\n", buffer);
|
||||
}
|
||||
DisconnectNamedPipe(pipe);
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(pipe);
|
||||
}
|
||||
Reference in new issue
Block a user