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
uv__winapi_init(void)51 void uv__winapi_init(void) {
52 HMODULE ntdll_module;
53 HMODULE powrprof_module;
54 HMODULE user32_module;
55 HMODULE kernel32_module;
56 HMODULE ws2_32_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 (pNtQueryVolumeInformationFile == 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 kernel32_module = GetModuleHandleA("kernel32.dll");
121 if (kernel32_module == NULL) {
122 uv_fatal_error(GetLastError(), "GetModuleHandleA");
123 }
124
125 pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
126 kernel32_module,
127 "GetQueuedCompletionStatusEx");
128
129 powrprof_module = LoadLibraryExA("powrprof.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
130 if (powrprof_module != NULL) {
131 pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
132 GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
133 }
134
135 user32_module = GetModuleHandleA("user32.dll");
136 if (user32_module != NULL) {
137 pSetWinEventHook = (sSetWinEventHook)
138 GetProcAddress(user32_module, "SetWinEventHook");
139 }
140
141 ws2_32_module = GetModuleHandleA("ws2_32.dll");
142 if (ws2_32_module != NULL) {
143 pGetHostNameW = (uv_sGetHostNameW) GetProcAddress(
144 ws2_32_module,
145 "GetHostNameW");
146 }
147 }
148