Skip to content

Commit

Permalink
Format win32/ntapi.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ispeters committed Oct 31, 2023
1 parent d11a85a commit a6537dd
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions source/win32/ntapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,44 @@
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

namespace unifex::win32::ntapi
{
NtCreateFile_t NtCreateFile = nullptr;
NtCancelIoFileEx_t NtCancelIoFileEx = nullptr;
NtReadFile_t NtReadFile = nullptr;
NtWriteFile_t NtWriteFile = nullptr;
NtSetIoCompletion_t NtSetIoCompletion = nullptr;
NtRemoveIoCompletion_t NtRemoveIoCompletion = nullptr;
NtRemoveIoCompletionEx_t NtRemoveIoCompletionEx = nullptr;
RtlNtStatusToDosError_t RtlNtStatusToDosError = nullptr;
namespace unifex::win32::ntapi {
NtCreateFile_t NtCreateFile = nullptr;
NtCancelIoFileEx_t NtCancelIoFileEx = nullptr;
NtReadFile_t NtReadFile = nullptr;
NtWriteFile_t NtWriteFile = nullptr;
NtSetIoCompletion_t NtSetIoCompletion = nullptr;
NtRemoveIoCompletion_t NtRemoveIoCompletion = nullptr;
NtRemoveIoCompletionEx_t NtRemoveIoCompletionEx = nullptr;
RtlNtStatusToDosError_t RtlNtStatusToDosError = nullptr;

static void do_initialisation() noexcept {
HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
if (ntdll == NULL) {
static void do_initialisation() noexcept {
HMODULE ntdll = ::GetModuleHandleW(L"ntdll.dll");
if (ntdll == NULL) {
std::terminate();
}

auto init = [&](auto& fnPtr, const char* name) noexcept {
FARPROC p = ::GetProcAddress(ntdll, name);
if (p == NULL) {
std::terminate();
}
fnPtr = reinterpret_cast<std::remove_reference_t<decltype(fnPtr)>>(
reinterpret_cast<void (*)()>(p));
};

auto init = [&](auto& fnPtr, const char* name) noexcept {
FARPROC p = ::GetProcAddress(ntdll, name);
if (p == NULL) {
std::terminate();
}
fnPtr = reinterpret_cast<std::remove_reference_t<decltype(fnPtr)>>(
reinterpret_cast<void(*)()>(p));
};
init(NtCreateFile, "NtCreateFile");
init(NtCancelIoFileEx, "NtCancelIoFileEx");
init(NtReadFile, "NtReadFile");
init(NtWriteFile, "NtWriteFile");
init(NtSetIoCompletion, "NtSetIoCompletion");
init(NtRemoveIoCompletion, "NtRemoveIoCompletion");
init(NtRemoveIoCompletionEx, "NtRemoveIoCompletionEx");
init(RtlNtStatusToDosError, "RtlNtStatusToDosError");
}

init(NtCreateFile, "NtCreateFile");
init(NtCancelIoFileEx, "NtCancelIoFileEx");
init(NtReadFile, "NtReadFile");
init(NtWriteFile, "NtWriteFile");
init(NtSetIoCompletion, "NtSetIoCompletion");
init(NtRemoveIoCompletion, "NtRemoveIoCompletion");
init(NtRemoveIoCompletionEx, "NtRemoveIoCompletionEx");
init(RtlNtStatusToDosError, "RtlNtStatusToDosError");
}

void ensure_initialised() noexcept {
static struct initialiser {
initialiser() noexcept { do_initialisation(); }
} dummy;
}
void ensure_initialised() noexcept {
static struct initialiser {
initialiser() noexcept { do_initialisation(); }
} dummy;
}
} // namespace unifex::win32::ntapi

0 comments on commit a6537dd

Please sign in to comment.