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-2022, 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 #define UMDF_USING_NTSTATUS
17 
18 #include "c.h"
19 
20 #include "port/win32ntdll.h"
21 
23 
24 typedef struct NtDllRoutine
25 {
26  const char *name;
29 
30 static const NtDllRoutine routines[] = {
31  {"RtlGetLastNtStatus", (pg_funcptr_t *) &pg_RtlGetLastNtStatus}
32 };
33 
34 static bool initialized;
35 
36 int
38 {
39  HMODULE module;
40 
41  if (initialized)
42  return 0;
43 
44  if (!(module = LoadLibraryEx("ntdll.dll", NULL, 0)))
45  {
46  _dosmaperr(GetLastError());
47  return -1;
48  }
49 
50  for (int i = 0; i < lengthof(routines); ++i)
51  {
52  pg_funcptr_t address;
53 
54  address = (pg_funcptr_t) GetProcAddress(module, routines[i].name);
55  if (!address)
56  {
57  _dosmaperr(GetLastError());
58  FreeLibrary(module);
59 
60  return -1;
61  }
62 
63  *(pg_funcptr_t *) routines[i].address = address;
64  }
65 
66  initialized = true;
67 
68  return 0;
69 }
#define lengthof(array)
Definition: c.h:734
void(* pg_funcptr_t)(void)
Definition: c.h:340
const char * name
Definition: encode.c:561
int i
Definition: isn.c:73
pg_funcptr_t * address
Definition: win32ntdll.c:27
const char * name
Definition: win32ntdll.c:26
void _dosmaperr(unsigned long)
Definition: win32error.c:171
struct NtDllRoutine NtDllRoutine
int initialize_ntdll(void)
Definition: win32ntdll.c:37
RtlGetLastNtStatus_t pg_RtlGetLastNtStatus
Definition: win32ntdll.c:22
static const NtDllRoutine routines[]
Definition: win32ntdll.c:30
static bool initialized
Definition: win32ntdll.c:34
NTSTATUS(__stdcall * RtlGetLastNtStatus_t)(void)
Definition: win32ntdll.h:26