nt: separate out ntdll.dll dynamic import logic

This commit is contained in:
Bert Belder 2017-09-03 20:51:17 +02:00
parent 3319986f74
commit d6cbb73f4e
3 changed files with 60 additions and 29 deletions

View File

@ -6,6 +6,7 @@
#include "afd.h"
#include "epoll.h"
#include "error.h"
#include "nt.h"
#include "tree.h"
#include "win.h"
@ -50,7 +51,6 @@ static int epoll__afd_poll(SOCKET socket,
OVERLAPPED* overlapped);
static int epoll__initialized = 0;
static PNTDEVICEIOCONTROLFILE pNtDeviceIoControlFile;
/* State associated with a epoll handle. */
struct epoll_port_data_s {
@ -570,7 +570,6 @@ int epoll_close(epoll_t port_handle) {
}
int epoll__initialize() {
HMODULE ntdll;
int r;
WSADATA wsa_data;
@ -578,13 +577,7 @@ int epoll__initialize() {
if (r != 0)
return -1;
ntdll = LoadLibraryW(L"ntdll.dll");
if (ntdll == NULL)
return -1;
pNtDeviceIoControlFile =
(PNTDEVICEIOCONTROLFILE) GetProcAddress(ntdll, "NtDeviceIoControlFile");
if (pNtDeviceIoControlFile == NULL)
if (nt_initialize() < 0)
return -1;
return 0;
@ -747,16 +740,16 @@ int epoll__afd_poll(SOCKET socket,
}
iosb_ptr->Status = STATUS_PENDING;
status = pNtDeviceIoControlFile((HANDLE) socket,
event,
NULL,
apc_context,
iosb_ptr,
IOCTL_AFD_POLL,
info,
sizeof *info,
info,
sizeof *info);
status = NtDeviceIoControlFile((HANDLE) socket,
event,
NULL,
apc_context,
iosb_ptr,
IOCTL_AFD_POLL,
info,
sizeof *info,
info,
sizeof *info);
if (overlapped == NULL) {
/* If this is a blocking operation, wait for the event to become

27
src/nt.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdlib.h>
#include "nt.h"
#include "win.h"
#define X(return_type, declarators, name, ...) \
return_type(declarators* name)(__VA_ARGS__) = NULL;
NTDLL_IMPORT_LIST(X)
#undef X
int nt_initialize() {
HMODULE ntdll;
ntdll = LoadLibraryW(L"ntdll.dll");
if (ntdll == NULL)
return -1;
#define X(return_type, declarators, name, ...) \
name = \
(return_type(declarators*)(__VA_ARGS__)) GetProcAddress(ntdll, #name); \
if (name == NULL) \
return -1;
NTDLL_IMPORT_LIST(X)
#undef X
return 0;
}

View File

@ -4,6 +4,8 @@
#include "ntstatus.h"
#include "win.h"
int nt_initialize();
typedef struct _IO_STATUS_BLOCK {
union {
NTSTATUS Status;
@ -16,15 +18,24 @@ typedef VOID(NTAPI* PIO_APC_ROUTINE)(PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG Reserved);
typedef NTSTATUS(NTAPI* PNTDEVICEIOCONTROLFILE)(HANDLE FileHandle,
HANDLE Event,
PIO_APC_ROUTINE ApcRoutine,
PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock,
ULONG IoControlCode,
PVOID InputBuffer,
ULONG InputBufferLength,
PVOID OutputBuffer,
ULONG OutputBufferLength);
#define NTDLL_IMPORT_LIST(X) \
X(NTSTATUS, \
NTAPI, \
NtDeviceIoControlFile, \
HANDLE FileHandle, \
HANDLE Event, \
PIO_APC_ROUTINE ApcRoutine, \
PVOID ApcContext, \
PIO_STATUS_BLOCK IoStatusBlock, \
ULONG IoControlCode, \
PVOID InputBuffer, \
ULONG InputBufferLength, \
PVOID OutputBuffer, \
ULONG OutputBufferLength)
#define X(return_type, declarators, name, ...) \
extern return_type(declarators* name)(__VA_ARGS__);
NTDLL_IMPORT_LIST(X)
#undef X
#endif /* EPOLL_NT_H_ */