Leaked source code of windows server 2003
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.

226 lines
5.5 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // sys.c - system functions
  24. ////
  25. #include "winlocal.h"
  26. #include <stdlib.h>
  27. #include <stdarg.h>
  28. #include "sys.h"
  29. #ifndef _WIN32
  30. #include <toolhelp.h> // get TimerCount and TaskFindHandle prototypes
  31. #ifdef VERTHUNK
  32. // generic thunk prototypes from "/msdev/include/wownt16.h"
  33. DWORD FAR PASCAL LoadLibraryEx32W(LPCSTR lpszLibFile, DWORD hFile, DWORD dwFlags);
  34. DWORD FAR PASCAL GetProcAddress32W(DWORD hModule, LPCSTR lpszProc);
  35. DWORD FAR PASCAL FreeLibrary32W(DWORD hLibModule);
  36. DWORD FAR CallProcEx32W( DWORD, DWORD, DWORD, ... );
  37. #endif
  38. #endif
  39. ////
  40. // private definitions
  41. ////
  42. // flag returned by GetWinFlags but not defined in windows.h
  43. //
  44. #ifndef WF_WINNT
  45. #define WF_WINNT 0x4000
  46. #endif
  47. ////
  48. // public functions
  49. ////
  50. // SysGetWinFlags - get system information
  51. // return flags
  52. // SYS_WF_WIN3X Windows 3.x
  53. // SYS_WF_WINNT Windows NT
  54. // SYS_WF_WIN95 Windows 95
  55. //
  56. DWORD DLLEXPORT WINAPI SysGetWinFlags(void)
  57. {
  58. DWORD dwSysWinFlags = 0;
  59. DWORD dwVersion = GetVersion();
  60. #ifdef _WIN32
  61. if (!(dwVersion & 0x80000000))
  62. dwSysWinFlags |= SYS_WF_WINNT;
  63. else if ((dwVersion & 0x80000000) && LOBYTE(LOWORD(dwVersion)) >= 4)
  64. dwSysWinFlags |= SYS_WF_WIN95;
  65. else
  66. dwSysWinFlags |= SYS_WF_WIN3X;
  67. #else
  68. DWORD dwWinFlags = GetWinFlags();
  69. if (dwWinFlags & WF_WINNT)
  70. dwSysWinFlags |= SYS_WF_WINNT;
  71. else if (LOBYTE(LOWORD(dwVersion)) == 3 &&
  72. HIBYTE(LOWORD(dwVersion)) == 95)
  73. dwSysWinFlags |= SYS_WF_WIN95;
  74. else
  75. dwSysWinFlags |= SYS_WF_WIN3X;
  76. #endif
  77. return dwSysWinFlags;
  78. }
  79. // SysGetWindowsVersion - get version of Microsoft Windows
  80. // return version (v3.10 = 310, etc.)
  81. //
  82. UINT DLLEXPORT WINAPI SysGetWindowsVersion(void)
  83. {
  84. static DWORD dwVersion = 0;
  85. BYTE nVersionMajor;
  86. BYTE nVersionMinor;
  87. // only get version the first time this function is called
  88. //
  89. if (dwVersion == 0)
  90. {
  91. #ifndef _WIN32
  92. #ifdef VERTHUNK
  93. DWORD dwFlags = SysGetWinFlags();
  94. #endif
  95. #endif
  96. // only get version the first time this function is called
  97. //
  98. dwVersion = GetVersion();
  99. #ifndef _WIN32
  100. #ifdef VERTHUNK
  101. // 16 bit GetVersion() returns v3.10 for WinNT and v3.95 for Win95
  102. // so we will call the 32-bit version of GetVersion
  103. //
  104. if ((dwFlags & SYS_WF_WINNT) || (dwFlags & SYS_WF_WIN95))
  105. {
  106. DWORD hKernel32;
  107. DWORD lpfnGetVersion;
  108. if ((hKernel32 = LoadLibraryEx32W("KERNEL32.DLL", NULL, 0)) != 0)
  109. {
  110. if ((lpfnGetVersion = GetProcAddress32W(hKernel32, "GetVersion")) != 0)
  111. dwVersion = CallProcEx32W(0, 0, lpfnGetVersion);
  112. FreeLibrary32W(hKernel32);
  113. }
  114. }
  115. #endif
  116. #endif
  117. }
  118. nVersionMajor = LOBYTE(LOWORD(dwVersion));
  119. nVersionMinor = HIBYTE(LOWORD(dwVersion));
  120. return ((UINT) nVersionMajor * 100) + (UINT) nVersionMinor;
  121. }
  122. // SysGetDOSVersion - get version of Microsoft DOS
  123. // return version (v6.20 = 620, etc.)
  124. //
  125. UINT DLLEXPORT WINAPI SysGetDOSVersion(void)
  126. {
  127. DWORD dwVersion = GetVersion();
  128. BYTE nVersionMajor = LOBYTE(HIWORD(dwVersion));
  129. BYTE nVersionMinor = HIBYTE(HIWORD(dwVersion));
  130. return ((UINT) nVersionMajor * 100) + (UINT) nVersionMinor;
  131. }
  132. #ifndef _WIN32
  133. // SysGetTimerCount - get elapsed time since Windows started
  134. // return milleseconds
  135. //
  136. DWORD DLLEXPORT WINAPI SysGetTimerCount(void)
  137. {
  138. DWORD msSinceStart;
  139. // TimerCount() not available under WIN32
  140. //
  141. #ifndef _WIN32
  142. TIMERINFO ti;
  143. ti.dwSize = sizeof(TIMERINFO);
  144. // use TimerCount function if possible, because
  145. // it is much more accurate than GetTickCount
  146. //
  147. if (TimerCount(&ti))
  148. msSinceStart = ti.dwmsSinceStart;
  149. else
  150. #endif
  151. msSinceStart = GetTickCount();
  152. return msSinceStart;
  153. }
  154. #endif
  155. #ifndef _WIN32
  156. // SysGetTaskInstance - get instance handle of specified task
  157. // <hTask> (i) specified task
  158. // NULL current task
  159. // returns instance handle (NULL if error)
  160. //
  161. // NOTE: under WIN32, <hTask> must be NULL
  162. //
  163. HINSTANCE DLLEXPORT WINAPI SysGetTaskInstance(HTASK hTask)
  164. {
  165. BOOL fSuccess = TRUE;
  166. HINSTANCE hInst;
  167. #ifdef _WIN32
  168. if (hTask != NULL)
  169. fSuccess = FALSE; // $FIXUP - any alternatives ?
  170. else if ((hInst = GetModuleHandle(NULL)) == NULL)
  171. fSuccess = FALSE;
  172. #else
  173. TASKENTRY te;
  174. // prepare to call TaskFindHandle
  175. //
  176. te.dwSize = sizeof(TASKENTRY);
  177. // assume current task if none specified
  178. //
  179. if (hTask == NULL && (hTask = GetCurrentTask()) == NULL)
  180. fSuccess = FALSE;
  181. // get instance handle of specified task
  182. //
  183. else if (!TaskFindHandle(&te, hTask))
  184. fSuccess = FALSE;
  185. else
  186. hInst = te.hInst;
  187. #endif
  188. return fSuccess ? hInst : NULL;
  189. }
  190. #endif