PostgreSQL Source Code  git master
win32ntdll.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * win32ntdll.c
4  * Dynamically loaded Windows NT functions.
5  *
6  * Portions Copyright (c) 2021-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/port/win32ntdll.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "c.h"
17 
18 #include "port/win32ntdll.h"
19 
23 
24 typedef struct NtDllRoutine
25 {
26  const char *name;
29 
30 static const NtDllRoutine routines[] = {
31  {"RtlGetLastNtStatus", (pg_funcptr_t *) &pg_RtlGetLastNtStatus},
32  {"RtlNtStatusToDosError", (pg_funcptr_t *) &pg_RtlNtStatusToDosError},
33  {"NtFlushBuffersFileEx", (pg_funcptr_t *) &pg_NtFlushBuffersFileEx}
34 };
35 
36 static bool initialized;
37 
38 int
40 {
41  HMODULE module;
42 
43  if (initialized)
44  return 0;
45 
46  if (!(module = LoadLibraryEx("ntdll.dll", NULL, 0)))
47  {
48  _dosmaperr(GetLastError());
49  return -1;
50  }
51 
52  for (int i = 0; i < lengthof(routines); ++i)
53  {
54  pg_funcptr_t address;
55 
56  address = (pg_funcptr_t) GetProcAddress(module, routines[i].name);
57  if (!address)
58  {
59  _dosmaperr(GetLastError());
60  FreeLibrary(module);
61 
62  return -1;
63  }
64 
65  *(pg_funcptr_t *) routines[i].address = address;
66  }
67 
68  initialized = true;
69 
70  return 0;
71 }
#define lengthof(array)
Definition: c.h:791
void(* pg_funcptr_t)(void)
Definition: c.h:391
int i
Definition: isn.c:73
pg_funcptr_t * address
Definition: win32ntdll.c:27
const char * name
Definition: win32ntdll.c:26
const char * name
void _dosmaperr(unsigned long)
Definition: win32error.c:177
NtFlushBuffersFileEx_t pg_NtFlushBuffersFileEx
Definition: win32ntdll.c:22
struct NtDllRoutine NtDllRoutine
int initialize_ntdll(void)
Definition: win32ntdll.c:39
RtlGetLastNtStatus_t pg_RtlGetLastNtStatus
Definition: win32ntdll.c:20
static const NtDllRoutine routines[]
Definition: win32ntdll.c:30
RtlNtStatusToDosError_t pg_RtlNtStatusToDosError
Definition: win32ntdll.c:21
static bool initialized
Definition: win32ntdll.c:36
NTSTATUS(__stdcall * RtlGetLastNtStatus_t)(void)
Definition: win32ntdll.h:24
NTSTATUS(__stdcall * NtFlushBuffersFileEx_t)(HANDLE, ULONG, PVOID, ULONG, PIO_STATUS_BLOCK)
Definition: win32ntdll.h:26
ULONG(__stdcall * RtlNtStatusToDosError_t)(NTSTATUS)
Definition: win32ntdll.h:25