From 8412ee2c16db2a4d76a43a7cb34cd57c5cea55b5 Mon Sep 17 00:00:00 2001 From: Felix Date: Mon, 25 Oct 2021 09:30:34 +0000 Subject: [PATCH] util/lib.c: Add module file path utility --- util/lib.c | 29 +++++++++++++++++++++++++++++ util/lib.h | 5 +++++ util/meson.build | 2 ++ 3 files changed, 36 insertions(+) create mode 100644 util/lib.c create mode 100644 util/lib.h diff --git a/util/lib.c b/util/lib.c new file mode 100644 index 0000000..bd7a5eb --- /dev/null +++ b/util/lib.c @@ -0,0 +1,29 @@ +#include + +#include + +wchar_t *module_file_name(HMODULE module) +{ + size_t buf_len; + DWORD len; + wchar_t *buf; + + buf_len = MAX_PATH; + buf = malloc(buf_len * sizeof(*buf)); + + while (true) { + len = GetModuleFileNameW(module, buf, buf_len); + + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + buf_len = len; + buf = realloc(buf, buf_len * sizeof(*buf)); + + break; + } + + buf_len *= 2; + buf = realloc(buf, buf_len * sizeof(*buf)); + } + + return buf; +} diff --git a/util/lib.h b/util/lib.h new file mode 100644 index 0000000..f3d717e --- /dev/null +++ b/util/lib.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +wchar_t *module_file_name(HMODULE module); diff --git a/util/meson.build b/util/meson.build index c3c07f9..575d123 100644 --- a/util/meson.build +++ b/util/meson.build @@ -17,6 +17,8 @@ util_lib = static_library( 'dprintf.h', 'dump.c', 'dump.h', + 'lib.c', + 'lib.h', 'str.c', 'str.h', ],