Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.7 KiB

  1. /*
  2. * This is a repository for the utility functions used by 32-bit
  3. * windows applications.
  4. *
  5. * REFERENCES:
  6. *
  7. * NOTES:
  8. *
  9. * REVISIONS:
  10. * pam15Jul96: Initial creation
  11. * srt25Oct96: Use GetComputerName if gethostname fails.
  12. * srt19Dec96: Added GetNtComputerName
  13. * tjg05Sep97: Added GetVersionInformation function
  14. * tjg16Dec97: Added GetRegistryValue function
  15. * tjg31Jan98: Added call to RegCloseKey in GetRegistryValue
  16. * tjg26Mar98: Return correct error from GetRegistryValue
  17. * mwh15Apr98: Update GetAvailableProtocols to look in _theConfigManager
  18. * for support of a protocol. This allows a UPS service to
  19. * turn off TCP & SPX support if desired
  20. */
  21. #include "cdefine.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "w32utils.h"
  25. #include "err.h"
  26. #include "cfgmgr.h"
  27. #define WF_WINNT 0x80000000
  28. const char *NOT_FOUND_STR = "Not found";
  29. /********************************************************************
  30. UtilSelectProcessor
  31. This routine determines what CPUs are available for the machine
  32. PowerChute is running on. If there are more than 1 processor,
  33. we select the first processor as the target for PowerChute to
  34. run on, and then set the process and all subsequent threads
  35. to run on the same processor.
  36. This is the fix to a problem we were having with PowerChute run-
  37. ning on machines with more than 1 processor.
  38. ********************************************************************/
  39. INT UtilSelectProcessor(HANDLE hCurrentThread)
  40. {
  41. DWORD_PTR processMask, systemMask;
  42. HANDLE hCurrentProcess;
  43. INT err = ErrNO_ERROR;
  44. /*
  45. Get the handle ID of the PowerChute process
  46. */
  47. hCurrentProcess = GetCurrentProcess();
  48. /*
  49. Get the masked value of the processors available for our
  50. process to run on.
  51. */
  52. GetProcessAffinityMask(hCurrentProcess,
  53. &processMask,
  54. &systemMask);
  55. if (processMask > 0) {
  56. DWORD_PTR affinityMask = 0;
  57. DWORD_PTR processorMask = 0;
  58. INT processorCheck = 0, position = 1;
  59. DWORD_PTR processorBit;
  60. /*
  61. Check the mask of the available processors to find the
  62. first available processor to run on. Stop checking
  63. once we find it.
  64. */
  65. processorMask = position;
  66. processorBit = processMask & processorMask;
  67. while ((processorBit != processorMask)
  68. && (position <= 32)) {
  69. processorMask = 1L << position;
  70. processorBit = processMask & processorMask;
  71. }
  72. /*
  73. Set the affinity mask to be the first available CPU.
  74. */
  75. if (position <= 32)
  76. SET_BIT(affinityMask, (position-1));
  77. else
  78. err = ErrNO_PROCESSORS;
  79. /*
  80. Set the thread to work on the first CPU available.
  81. */
  82. if (SetThreadAffinityMask(hCurrentThread, affinityMask) == 0)
  83. err = ErrNO_PROCESSORS;
  84. }
  85. else
  86. err = ErrNO_PROCESSORS;
  87. return err;
  88. }
  89. /********************************************************************
  90. GetWindowsVersion
  91. This routine checks to see what platform we are running on.
  92. There is another (cleaner) way to do this (as seen in one of the
  93. MSDN newsletters), but it is supported only on 95 and on NT 4.0
  94. and later versions. We will wait to implement that way.
  95. ********************************************************************/
  96. tWindowsVersion GetWindowsVersion()
  97. {
  98. DWORD win_ver = GetVersion();
  99. /*
  100. Determine if we are running in NT
  101. */
  102. if (win_ver < WF_WINNT)
  103. return eWinNT;
  104. /*
  105. Determine if we are running in Win 3.1 or Win95
  106. */
  107. else if (LOBYTE(LOWORD(win_ver)) < 4)
  108. return eWin31;
  109. else
  110. return eWin95;
  111. return eUnknown;
  112. }