This repository has been archived on 2026-11-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
my-first-apex-internal/inc/shd/winapi.h
T
2026-11-05 02:19:09 +01:00

180 lines
4.8 KiB
C++

#pragma once
#define FIND_EXPORT(MOD, EXP, TYPE) win_api::find_export<TYPE>(_XS(MOD), _XS(EXP))
#define DECLARE_FUNC(MOD, EXP, TYPE, ID) if(!table[ID]) table[ID] = FIND_EXPORT(MOD, EXP, TYPE); \
auto* const fn = (TYPE*)table[ID]
namespace win_api
{
#pragma data_seg("data")
__declspec(allocate("data")) inline void* table[8192];
#pragma data_seg()
template < typename T >
T* find_export(const char* mod_name, const char* exp_name)
{
auto mod_handle = LI_FN(GetModuleHandleA)(mod_name);
if (mod_handle == nullptr)
return nullptr;
return pe::find_export<T*>(mod_handle, exp_name);
}
inline_block auto get_last_error() -> DWORD
{
return *(DWORD*)(__readgsqword(0x30u) + 104);
}
inline_block auto get_current_process() -> HANDLE
{
return (HANDLE)-1;
}
inline_block auto get_current_thread() -> HANDLE
{
return (HANDLE)-2;
}
inline_block auto get_current_process_id() -> u_long
{
return *(DWORD*)(__readgsqword(0x30u) + 64);
}
inline_block auto get_process_heap() -> HANDLE
{
return *(HANDLE*)(__readgsqword(0x60u) + 48);
}
inline_block auto get_module_handle(const char* name) -> HMODULE
{
DECLARE_FUNC("kernel32.dll", "GetModuleHandleA", decltype(get_module_handle), __COUNTER__);
return fn(name);
}
inline_block auto get_module_handle_wide(const wchar_t* name) -> HMODULE
{
DECLARE_FUNC("kernel32.dll", "GetModuleHandleA", decltype(get_module_handle_wide), __COUNTER__);
return fn(name);
}
inline_block auto virtual_alloc(
void* address, size_t size,
u_long alloc_type, u_long protect
) -> void*
{
DECLARE_FUNC("kernel32.dll", "VirtualAlloc", decltype(virtual_alloc), __COUNTER__);
return fn(
address, size,
alloc_type, protect
);
}
inline_block auto virtual_query(const void* address, PMEMORY_BASIC_INFORMATION buffer, size_t size) -> size_t
{
DECLARE_FUNC("kernel32.dll", "VirtualQuery", decltype(virtual_query), __COUNTER__);
return fn(address, buffer, size);
}
inline_block auto virtual_free(void* address, size_t size, u_long free_type) -> int
{
DECLARE_FUNC("kernel32.dll", "VirtualFree", decltype(virtual_free), __COUNTER__);
return fn(address, size, free_type);
}
inline_block auto read_process_memory(_handle process, const void* base_address, void* buffer, size_t size, size_t* cnt) -> BOOL
{
DECLARE_FUNC("kernel32.dll", "ReadProcessMemory", decltype(read_process_memory), __COUNTER__);
return fn(process, base_address, buffer, size, cnt);
}
inline_block auto output_debug_string(const char* text) -> void
{
DECLARE_FUNC("kernel32.dll", "OutputDebugStringA", decltype(output_debug_string), __COUNTER__);
return fn(text);
}
inline_block auto suspend_thread(HANDLE handle) -> DWORD
{
DECLARE_FUNC("kernel32.dll", "SuspendThread", decltype(suspend_thread), __COUNTER__);
return fn(handle);
}
inline_block auto heap_create(DWORD options, SIZE_T initial_size, SIZE_T maximum_size) -> HANDLE
{
DECLARE_FUNC("kernel32.dll", "HeapCreate", decltype(heap_create), __COUNTER__);
return fn(options, initial_size, maximum_size);
}
inline_block auto heap_destroy(HANDLE handle) -> BOOL
{
DECLARE_FUNC("kernel32.dll", "HeapDestroy", decltype(heap_destroy), __COUNTER__);
return fn(handle);
}
inline_block auto pc_to_file_header(PVOID value, PVOID* base_of_image) -> PVOID
{
DECLARE_FUNC("kernel32.dll", "RtlPcToFileHeader", decltype(pc_to_file_header), __COUNTER__);
return fn(value, base_of_image);
}
inline_block auto heap_alloc(HANDLE handle, DWORD flags, SIZE_T size) -> PVOID
{
DECLARE_FUNC("ntdll.dll", "RtlAllocateHeap", decltype(heap_alloc), __COUNTER__);
return fn(handle, flags, size);
}
inline_block auto heap_re_alloc(HANDLE handle, DWORD flags, PVOID ptr, SIZE_T size) -> PVOID
{
DECLARE_FUNC("ntdll.dll", "RtlReAllocateHeap", decltype(heap_re_alloc), __COUNTER__);
return fn(handle, flags, ptr, size);
}
extern bool virtual_protect(void* address, size_t size, u_long new_protect, u_long* old_protect);
namespace rebuilt
{
inline_block auto virtual_alloc(void* address, size_t size, u_long alloc_type, u_long protect) -> void*
{
if (!NT_SUCCESS(sc_asm_alloc_virtual_memory(get_current_process(), &address, 0, &size, alloc_type, protect)))
return nullptr;
return address;
}
inline_block auto virtual_query(const void* address, PMEMORY_BASIC_INFORMATION info_buffer, size_t length) -> size_t
{
if (!NT_SUCCESS(sc_asm_query_virtual_memory(get_current_process(), (void*)address, k::MemoryBasicInformation, info_buffer, length, &length)))
return 0;
return length;
}
inline_block auto get_thread_context(_handle thread, PCONTEXT context) -> bool
{
return NT_SUCCESS(sc_asm_get_context_thread(thread, context));
}
inline_block auto sleep(int miliseconds)
{
LARGE_INTEGER delay;
delay.QuadPart = -(miliseconds * 10000);
sc_asm_delay_execution(FALSE, &delay);
}
}
}