xref: /libuv/src/win/winapi.c (revision 6af08fb5)
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 /* Powrprof.dll function pointer */
40 sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification;
41 
42 /* User32.dll function pointer */
43 sSetWinEventHook pSetWinEventHook;
44 
45 /* ws2_32.dll function pointer */
46 uv_sGetHostNameW pGetHostNameW;
47 
48 /* api-ms-win-core-file-l2-1-4.dll function pointer */
49 sGetFileInformationByName pGetFileInformationByName;
50 
uv__winapi_init(void)51 void uv__winapi_init(void) {
52   HMODULE ntdll_module;
53   HMODULE powrprof_module;
54   HMODULE user32_module;
55   HMODULE ws2_32_module;
56   HMODULE api_win_core_file_module;
57 
58   ntdll_module = GetModuleHandleA("ntdll.dll");
59   if (ntdll_module == NULL) {
60     uv_fatal_error(GetLastError(), "GetModuleHandleA");
61   }
62 
63   pRtlGetVersion = (sRtlGetVersion) GetProcAddress(ntdll_module,
64                                                    "RtlGetVersion");
65 
66   pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
67       ntdll_module,
68       "RtlNtStatusToDosError");
69   if (pRtlNtStatusToDosError == NULL) {
70     uv_fatal_error(GetLastError(), "GetProcAddress");
71   }
72 
73   pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
74       ntdll_module,
75       "NtDeviceIoControlFile");
76   if (pNtDeviceIoControlFile == NULL) {
77     uv_fatal_error(GetLastError(), "GetProcAddress");
78   }
79 
80   pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
81       ntdll_module,
82       "NtQueryInformationFile");
83   if (pNtQueryInformationFile == NULL) {
84     uv_fatal_error(GetLastError(), "GetProcAddress");
85   }
86 
87   pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
88       ntdll_module,
89       "NtSetInformationFile");
90   if (pNtSetInformationFile == NULL) {
91     uv_fatal_error(GetLastError(), "GetProcAddress");
92   }
93 
94   pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
95       GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
96   if (pNtQueryVolumeInformationFile == NULL) {
97     uv_fatal_error(GetLastError(), "GetProcAddress");
98   }
99 
100   pNtQueryDirectoryFile = (sNtQueryDirectoryFile)
101       GetProcAddress(ntdll_module, "NtQueryDirectoryFile");
102   if (pNtQueryDirectoryFile == NULL) {
103     uv_fatal_error(GetLastError(), "GetProcAddress");
104   }
105 
106   pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
107       ntdll_module,
108       "NtQuerySystemInformation");
109   if (pNtQuerySystemInformation == NULL) {
110     uv_fatal_error(GetLastError(), "GetProcAddress");
111   }
112 
113   pNtQueryInformationProcess = (sNtQueryInformationProcess) GetProcAddress(
114       ntdll_module,
115       "NtQueryInformationProcess");
116   if (pNtQueryInformationProcess == NULL) {
117     uv_fatal_error(GetLastError(), "GetProcAddress");
118   }
119 
120   powrprof_module = LoadLibraryExA("powrprof.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
121   if (powrprof_module != NULL) {
122     pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
123       GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
124   }
125 
126   user32_module = GetModuleHandleA("user32.dll");
127   if (user32_module != NULL) {
128     pSetWinEventHook = (sSetWinEventHook)
129       GetProcAddress(user32_module, "SetWinEventHook");
130   }
131 
132   ws2_32_module = GetModuleHandleA("ws2_32.dll");
133   if (ws2_32_module != NULL) {
134     pGetHostNameW = (uv_sGetHostNameW) GetProcAddress(
135         ws2_32_module,
136         "GetHostNameW");
137   }
138 
139   api_win_core_file_module = GetModuleHandleA("api-ms-win-core-file-l2-1-4.dll");
140   if (api_win_core_file_module != NULL) {
141     pGetFileInformationByName = (sGetFileInformationByName)GetProcAddress(
142         api_win_core_file_module, "GetFileInformationByName");
143   }
144 }
145