summaryrefslogtreecommitdiffstats
path: root/src/common/impl/debug_windows.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/common/impl/debug_windows.c
Add the files
Diffstat (limited to 'src/common/impl/debug_windows.c')
-rw-r--r--src/common/impl/debug_windows.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/common/impl/debug_windows.c b/src/common/impl/debug_windows.c
new file mode 100644
index 0000000..fb76a65
--- /dev/null
+++ b/src/common/impl/debug_windows.c
@@ -0,0 +1,55 @@
+#include "common/debug.h"
+#include "common/windows/nt.h"
+
+#include <windows.h>
+
+const char* ffDebugWin32Error(DWORD errorCode) {
+ static char buffer[512];
+
+ wchar_t bufferW[256];
+ ULONG len = FormatMessageW(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ (DWORD) errorCode,
+ 0,
+ bufferW,
+ ARRAY_SIZE(bufferW),
+ NULL);
+
+ if (len == 0) {
+ snprintf(buffer, sizeof(buffer), "Unknown error code (%lu)", errorCode);
+ } else {
+ // Remove trailing newline
+ while (len > 0 && (bufferW[len - 1] == '\r' || bufferW[len - 1] == '\n')) {
+ --len;
+ }
+
+ if (NT_SUCCESS(RtlUnicodeToUTF8N(buffer, sizeof(buffer), &len, bufferW, len * sizeof(wchar_t)))) {
+ snprintf(buffer + len, sizeof(buffer) - len, " (%lu)", errorCode);
+ } else {
+ snprintf(buffer, sizeof(buffer), "Unknown error (%lu)", errorCode);
+ }
+ }
+
+ return buffer;
+}
+
+const char* ffDebugNtStatus(NTSTATUS status) {
+ return ffDebugWin32Error(RtlNtStatusToDosError(status));
+}
+
+static inline DWORD HRESULTToWin32Error(HRESULT hr) {
+ if (SUCCEEDED(hr)) {
+ return ERROR_SUCCESS;
+ }
+
+ if (HRESULT_FACILITY(hr) == FACILITY_WIN32) {
+ return HRESULT_CODE(hr);
+ }
+
+ return ERROR_INTERNAL_ERROR;
+}
+
+const char* ffDebugHResult(HRESULT hr) {
+ return ffDebugWin32Error(HRESULTToWin32Error(hr));
+}