xref: /libuv/src/win/winapi.c (revision 65e37353)
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include <assert.h>
23 
24 #include "uv.h"
25 #include "internal.h"
26 
27 
28 /* Ntdll function pointers */
29 sRtlGetVersion pRtlGetVersion;
30 sRtlNtStatusToDosError pRtlNtStatusToDosError;
31 sNtDeviceIoControlFile pNtDeviceIoControlFile;
32 sNtQueryInformationFile pNtQueryInformationFile;
33 sNtSetInformationFile pNtSetInformationFile;
34 sNtQueryVolumeInformationFile pNtQueryVolumeInformationFile;
35 sNtQueryDirectoryFile pNtQueryDirectoryFile;
36 sNtQuerySystemInformation pNtQuerySystemInformation;
37 sNtQueryInformationProcess pNtQueryInformationProcess;
38 
39 /* Kernel32 function pointers */
40 sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;
41 
42 /* Powrprof.dll function pointer */
43 sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification;
44 
45 /* User32.dll function pointer */
46 sSetWinEventHook pSetWinEventHook;
47 
48 /* ws2_32.dll function pointer */
49 uv_sGetHostNameW pGetHostNameW;
50 
51 /* api-ms-win-core-file-l2-1-4.dll function pointer */
52 sGetFileInformationByName pGetFileInformationByName;
53 
uv__winapi_init(void)54 void uv__winapi_init(void) {
55   HMODULE ntdll_module;
56   HMODULE powrprof_module;
57   HMODULE user32_module;
58   HMODULE kernel32_module;
59   HMODULE ws2_32_module;
60   HMODULE api_win_core_file_module;
61 
62   ntdll_module = GetModuleHandleA("ntdll.dll");
63   if (ntdll_module == NULL) {
64     uv_fatal_error(GetLastError(), "GetModuleHandleA");
65   }
66 
67   pRtlGetVersion = (sRtlGetVersion) GetProcAddress(ntdll_module,
68                                                    "RtlGetVersion");
69 
70   pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
71       ntdll_module,
72       "RtlNtStatusToDosError");
73   if (pRtlNtStatusToDosError == NULL) {
74     uv_fatal_error(GetLastError(), "GetProcAddress");
75   }
76 
77   pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
78       ntdll_module,
79       "NtDeviceIoControlFile");
80   if (pNtDeviceIoControlFile == NULL) {
81     uv_fatal_error(GetLastError(), "GetProcAddress");
82   }
83 
84   pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
85       ntdll_module,
86       "NtQueryInformationFile");
87   if (pNtQueryInformationFile == NULL) {
88     uv_fatal_error(GetLastError(), "GetProcAddress");
89   }
90 
91   pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
92       ntdll_module,
93       "NtSetInformationFile");
94   if (pNtSetInformationFile == NULL) {
95     uv_fatal_error(GetLastError(), "GetProcAddress");
96   }
97 
98   pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
99       GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
100   if (pNtQueryVolumeInformationFile == NULL) {
101     uv_fatal_error(GetLastError(), "GetProcAddress");
102   }
103 
104   pNtQueryDirectoryFile = (sNtQueryDirectoryFile)
105       GetProcAddress(ntdll_module, "NtQueryDirectoryFile");
106   if (pNtQueryDirectoryFile == NULL) {
107     uv_fatal_error(GetLastError(), "GetProcAddress");
108   }
109 
110   pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
111       ntdll_module,
112       "NtQuerySystemInformation");
113   if (pNtQuerySystemInformation == NULL) {
114     uv_fatal_error(GetLastError(), "GetProcAddress");
115   }
116 
117   pNtQueryInformationProcess = (sNtQueryInformationProcess) GetProcAddress(
118       ntdll_module,
119       "NtQueryInformationProcess");
120   if (pNtQueryInformationProcess == NULL) {
121     uv_fatal_error(GetLastError(), "GetProcAddress");
122   }
123 
124   kernel32_module = GetModuleHandleA("kernel32.dll");
125   if (kernel32_module == NULL) {
126     uv_fatal_error(GetLastError(), "GetModuleHandleA");
127   }
128 
129   pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
130       kernel32_module,
131       "GetQueuedCompletionStatusEx");
132 
133   powrprof_module = LoadLibraryExA("powrprof.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
134   if (powrprof_module != NULL) {
135     pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
136       GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
137   }
138 
139   user32_module = GetModuleHandleA("user32.dll");
140   if (user32_module != NULL) {
141     pSetWinEventHook = (sSetWinEventHook)
142       GetProcAddress(user32_module, "SetWinEventHook");
143   }
144 
145   ws2_32_module = GetModuleHandleA("ws2_32.dll");
146   if (ws2_32_module != NULL) {
147     pGetHostNameW = (uv_sGetHostNameW) GetProcAddress(
148         ws2_32_module,
149         "GetHostNameW");
150   }
151 
152   api_win_core_file_module = GetModuleHandleA("api-ms-win-core-file-l2-1-4.dll");
153   if (api_win_core_file_module != NULL) {
154     pGetFileInformationByName = (sGetFileInformationByName)GetProcAddress(
155         api_win_core_file_module, "GetFileInformationByName");
156   }
157 }
158