winapi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. void uv_winapi_init(void) {
  41. HMODULE ntdll_module;
  42. HMODULE powrprof_module;
  43. HMODULE user32_module;
  44. HMODULE kernel32_module;
  45. ntdll_module = GetModuleHandleA("ntdll.dll");
  46. if (ntdll_module == NULL) {
  47. uv_fatal_error(GetLastError(), "GetModuleHandleA");
  48. }
  49. pRtlGetVersion = (sRtlGetVersion) GetProcAddress(ntdll_module,
  50. "RtlGetVersion");
  51. pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
  52. ntdll_module,
  53. "RtlNtStatusToDosError");
  54. if (pRtlNtStatusToDosError == NULL) {
  55. uv_fatal_error(GetLastError(), "GetProcAddress");
  56. }
  57. pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
  58. ntdll_module,
  59. "NtDeviceIoControlFile");
  60. if (pNtDeviceIoControlFile == NULL) {
  61. uv_fatal_error(GetLastError(), "GetProcAddress");
  62. }
  63. pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
  64. ntdll_module,
  65. "NtQueryInformationFile");
  66. if (pNtQueryInformationFile == NULL) {
  67. uv_fatal_error(GetLastError(), "GetProcAddress");
  68. }
  69. pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
  70. ntdll_module,
  71. "NtSetInformationFile");
  72. if (pNtSetInformationFile == NULL) {
  73. uv_fatal_error(GetLastError(), "GetProcAddress");
  74. }
  75. pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
  76. GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
  77. if (pNtQueryVolumeInformationFile == NULL) {
  78. uv_fatal_error(GetLastError(), "GetProcAddress");
  79. }
  80. pNtQueryDirectoryFile = (sNtQueryDirectoryFile)
  81. GetProcAddress(ntdll_module, "NtQueryDirectoryFile");
  82. if (pNtQueryVolumeInformationFile == NULL) {
  83. uv_fatal_error(GetLastError(), "GetProcAddress");
  84. }
  85. pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
  86. ntdll_module,
  87. "NtQuerySystemInformation");
  88. if (pNtQuerySystemInformation == NULL) {
  89. uv_fatal_error(GetLastError(), "GetProcAddress");
  90. }
  91. pNtQueryInformationProcess = (sNtQueryInformationProcess) GetProcAddress(
  92. ntdll_module,
  93. "NtQueryInformationProcess");
  94. if (pNtQueryInformationProcess == NULL) {
  95. uv_fatal_error(GetLastError(), "GetProcAddress");
  96. }
  97. kernel32_module = GetModuleHandleA("kernel32.dll");
  98. if (kernel32_module == NULL) {
  99. uv_fatal_error(GetLastError(), "GetModuleHandleA");
  100. }
  101. pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
  102. kernel32_module,
  103. "GetQueuedCompletionStatusEx");
  104. powrprof_module = LoadLibraryA("powrprof.dll");
  105. if (powrprof_module != NULL) {
  106. pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
  107. GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
  108. }
  109. user32_module = LoadLibraryA("user32.dll");
  110. if (user32_module != NULL) {
  111. pSetWinEventHook = (sSetWinEventHook)
  112. GetProcAddress(user32_module, "SetWinEventHook");
  113. }
  114. }