winapi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include <assert.h>
  22. #include "uv.h"
  23. #include "internal.h"
  24. /* Ntdll function pointers */
  25. sRtlGetVersion pRtlGetVersion;
  26. sRtlNtStatusToDosError pRtlNtStatusToDosError;
  27. sNtDeviceIoControlFile pNtDeviceIoControlFile;
  28. sNtQueryInformationFile pNtQueryInformationFile;
  29. sNtSetInformationFile pNtSetInformationFile;
  30. sNtQueryVolumeInformationFile pNtQueryVolumeInformationFile;
  31. sNtQueryDirectoryFile pNtQueryDirectoryFile;
  32. sNtQuerySystemInformation pNtQuerySystemInformation;
  33. sNtQueryInformationProcess pNtQueryInformationProcess;
  34. /* Kernel32 function pointers */
  35. sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;
  36. /* Powrprof.dll function pointer */
  37. sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification;
  38. /* User32.dll function pointer */
  39. sSetWinEventHook pSetWinEventHook;
  40. /* ws2_32.dll function pointer */
  41. uv_sGetHostNameW pGetHostNameW;
  42. void uv__winapi_init(void) {
  43. HMODULE ntdll_module;
  44. HMODULE powrprof_module;
  45. HMODULE user32_module;
  46. HMODULE kernel32_module;
  47. HMODULE ws2_32_module;
  48. ntdll_module = GetModuleHandleA("ntdll.dll");
  49. if (ntdll_module == NULL) {
  50. uv_fatal_error(GetLastError(), "GetModuleHandleA");
  51. }
  52. pRtlGetVersion = (sRtlGetVersion) GetProcAddress(ntdll_module,
  53. "RtlGetVersion");
  54. pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
  55. ntdll_module,
  56. "RtlNtStatusToDosError");
  57. if (pRtlNtStatusToDosError == NULL) {
  58. uv_fatal_error(GetLastError(), "GetProcAddress");
  59. }
  60. pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
  61. ntdll_module,
  62. "NtDeviceIoControlFile");
  63. if (pNtDeviceIoControlFile == NULL) {
  64. uv_fatal_error(GetLastError(), "GetProcAddress");
  65. }
  66. pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
  67. ntdll_module,
  68. "NtQueryInformationFile");
  69. if (pNtQueryInformationFile == NULL) {
  70. uv_fatal_error(GetLastError(), "GetProcAddress");
  71. }
  72. pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
  73. ntdll_module,
  74. "NtSetInformationFile");
  75. if (pNtSetInformationFile == NULL) {
  76. uv_fatal_error(GetLastError(), "GetProcAddress");
  77. }
  78. pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
  79. GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
  80. if (pNtQueryVolumeInformationFile == NULL) {
  81. uv_fatal_error(GetLastError(), "GetProcAddress");
  82. }
  83. pNtQueryDirectoryFile = (sNtQueryDirectoryFile)
  84. GetProcAddress(ntdll_module, "NtQueryDirectoryFile");
  85. if (pNtQueryVolumeInformationFile == NULL) {
  86. uv_fatal_error(GetLastError(), "GetProcAddress");
  87. }
  88. pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
  89. ntdll_module,
  90. "NtQuerySystemInformation");
  91. if (pNtQuerySystemInformation == NULL) {
  92. uv_fatal_error(GetLastError(), "GetProcAddress");
  93. }
  94. pNtQueryInformationProcess = (sNtQueryInformationProcess) GetProcAddress(
  95. ntdll_module,
  96. "NtQueryInformationProcess");
  97. if (pNtQueryInformationProcess == NULL) {
  98. uv_fatal_error(GetLastError(), "GetProcAddress");
  99. }
  100. kernel32_module = GetModuleHandleA("kernel32.dll");
  101. if (kernel32_module == NULL) {
  102. uv_fatal_error(GetLastError(), "GetModuleHandleA");
  103. }
  104. pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
  105. kernel32_module,
  106. "GetQueuedCompletionStatusEx");
  107. powrprof_module = LoadLibraryExA("powrprof.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  108. if (powrprof_module != NULL) {
  109. pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
  110. GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
  111. }
  112. user32_module = GetModuleHandleA("user32.dll");
  113. if (user32_module != NULL) {
  114. pSetWinEventHook = (sSetWinEventHook)
  115. GetProcAddress(user32_module, "SetWinEventHook");
  116. }
  117. ws2_32_module = GetModuleHandleA("ws2_32.dll");
  118. if (ws2_32_module != NULL) {
  119. pGetHostNameW = (uv_sGetHostNameW) GetProcAddress(
  120. ws2_32_module,
  121. "GetHostNameW");
  122. }
  123. }