diff --git a/CMakeLists.txt b/CMakeLists.txt index 779b8efe6..0877159a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,7 @@ set(COMMON src/common/logging/backend.cpp src/common/uint128.h src/common/version.h src/common/ntapi.h + src/common/ntapi.cpp ) set(CORE src/core/aerolib/stubs.cpp diff --git a/src/common/ntapi.cpp b/src/common/ntapi.cpp new file mode 100644 index 000000000..f596aa4ff --- /dev/null +++ b/src/common/ntapi.cpp @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "ntapi.h" + +NtDelayExecution_t NtDelayExecution = nullptr; +NtSetInformationFile_t NtSetInformationFile = nullptr; + +namespace Common::NtApi { + +void Initialize() { + HMODULE nt_handle = GetModuleHandleA("ntdll.dll"); + + // http://stackoverflow.com/a/31411628/4725495 + NtDelayExecution = (NtDelayExecution_t)GetProcAddress(nt_handle, "NtDelayExecution"); + NtSetInformationFile = + (NtSetInformationFile_t)GetProcAddress(nt_handle, "NtSetInformationFile"); +} + +} // namespace Common::NtApi diff --git a/src/common/ntapi.h b/src/common/ntapi.h index 1cd62ffd5..17d353403 100644 --- a/src/common/ntapi.h +++ b/src/common/ntapi.h @@ -5,9 +5,8 @@ #ifdef _WIN32 -#include "common/types.h" - #include +#include "common/types.h" typedef enum _FILE_INFORMATION_CLASS { FileDirectoryInformation = 1, @@ -109,15 +108,17 @@ typedef struct _FILE_DISPOSITION_INFORMATION { BOOLEAN DeleteFile; } FILE_DISPOSITION_INFORMATION, *PFILE_DISPOSITION_INFORMATION; -// http://stackoverflow.com/a/31411628/4725495 -static u32(__stdcall* NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval) = - (u32(__stdcall*)(BOOL, PLARGE_INTEGER))GetProcAddress(GetModuleHandleA("ntdll.dll"), - "NtDelayExecution"); +typedef u32(__stdcall* NtDelayExecution_t)(BOOL Alertable, PLARGE_INTEGER DelayInterval); -static u32(__stdcall* NtSetInformationFile)(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock, - PVOID FileInformation, ULONG Length, - FILE_INFORMATION_CLASS FileInformationClass) = - (u32(__stdcall*)(HANDLE, PIO_STATUS_BLOCK, PVOID, ULONG, FILE_INFORMATION_CLASS)) - GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtSetInformationFile"); +typedef u32(__stdcall* NtSetInformationFile_t)(HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock, + PVOID FileInformation, ULONG Length, + FILE_INFORMATION_CLASS FileInformationClass); + +extern NtDelayExecution_t NtDelayExecution; +extern NtSetInformationFile_t NtSetInformationFile; + +namespace Common::NtApi { +void Initialize(); +} #endif diff --git a/src/emulator.cpp b/src/emulator.cpp index 16f202976..8d5378fd9 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -13,6 +13,7 @@ #include "common/config.h" #include "common/debug.h" #include "common/logging/backend.h" +#include "common/ntapi.h" #include "common/path_util.h" #include "common/singleton.h" #include "common/version.h" @@ -38,6 +39,9 @@ Emulator::Emulator() const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); Config::load(config_dir / "config.toml"); + // Initialize NT API functions + Common::NtApi::Initialize(); + // Start logger. Common::Log::Initialize(); Common::Log::Start();