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.

253 lines
6.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Abstract:
  4. Environment independed system functions implementation for Win32,
  5. Author:
  6. Souren Aghajanyan (sourenag) 24-Sep-2001
  7. Revision History:
  8. <alias> <date> <comments>
  9. --*/
  10. #include "pch.h"
  11. #include "sysfunc.h"
  12. #include "functions.h"
  13. #include "windows.h"
  14. MY_OPENMUTEX g_OpenMutex = NULL;
  15. MY_CREATEMUTEX g_CreateMutex = NULL;
  16. MY_RELEASEMUTEX g_ReleaseMutex = NULL;
  17. MY_WAITFORSINGLEOBJECT g_WaitForSingleObject = NULL;
  18. MY_CLOSEHANDLE g_CloseHandle = NULL;
  19. MY_OPENSHAREDMEMORY g_OpenSharedMemory = NULL;
  20. MY_CREATESHAREDMEMORY g_CreateSharedMemory = NULL;
  21. MY_MAPSHAREDMEMORY g_MapSharedMemory = NULL;
  22. MY_UNMAPSHAREDMEMORY g_UnMapSharedMemory = NULL;
  23. MY_CREATESHAREDFILE g_CreateSharedFile = NULL;
  24. MY_SETFILEPOINTER g_SetFilePointer = NULL;
  25. MY_WRITEFILE g_WriteFile = NULL;
  26. MY_GETPROCESSORNUMBER g_GetProcessorsNumber = NULL;
  27. VOID ReleaseMutexWin32(HANDLE hObject)
  28. {
  29. ReleaseMutex(hObject);
  30. }
  31. DWORD WaitForSingleObjectWin32(HANDLE hObject, DWORD dwTimeout)
  32. {
  33. return WaitForSingleObject(hObject, dwTimeout);
  34. }
  35. VOID CloseHandleWin32(HANDLE hObject)
  36. {
  37. CloseHandle(hObject);
  38. }
  39. PVOID MapSharedMemoryWin32(HANDLE hObject)
  40. {
  41. return MapViewOfFile(hObject, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  42. }
  43. BOOL UnMapSharedMemoryWin32(PVOID pSharedMemory)
  44. {
  45. return UnmapViewOfFile(pSharedMemory);
  46. }
  47. BOOL SetFilePointerWin32(HANDLE hObject, UINT uiOffset, DWORD dwMoveMethod)
  48. {
  49. return SetFilePointer(hObject, uiOffset, NULL, dwMoveMethod);
  50. }
  51. BOOL WriteFileWin32(HANDLE hObject, PVOID pBuffer, UINT uiNumberOfBytesToWrite, DWORD * pdwNumberOfBytesWritten)
  52. {
  53. return WriteFile(hObject, pBuffer, uiNumberOfBytesToWrite, pdwNumberOfBytesWritten, NULL);
  54. }
  55. //
  56. // Ansi version
  57. //
  58. HANDLE OpenMutexWin32A(PCWSTR pObjectName)
  59. {
  60. ASSERT(pObjectName);
  61. CBuffer buffer;buffer.Allocate(wcslen(pObjectName) + 2);
  62. PSTR pBuffer = (PSTR)buffer.GetBuffer();
  63. sprintf(pBuffer, "%S", pObjectName);
  64. return OpenMutexA(MUTEX_ALL_ACCESS, FALSE, pBuffer);
  65. }
  66. HANDLE CreateMutexWin32A(PCWSTR pObjectName, BOOL bInitialOwnership)
  67. {
  68. ASSERT(pObjectName);
  69. CBuffer buffer;buffer.Allocate(wcslen(pObjectName) + 2);
  70. PSTR pBuffer = (PSTR)buffer.GetBuffer();
  71. sprintf(pBuffer, "%S", pObjectName);
  72. return CreateMutexA(NULL, bInitialOwnership, pBuffer);
  73. }
  74. HANDLE OpenSharedMemoryWin32A(PCWSTR pObjectName)
  75. {
  76. ASSERT(pObjectName);
  77. CBuffer buffer;buffer.Allocate(wcslen(pObjectName) + 2);
  78. PSTR pBuffer = (PSTR)buffer.GetBuffer();
  79. sprintf(pBuffer, "%S", pObjectName);
  80. return OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, pBuffer);
  81. }
  82. HANDLE CreateSharedMemoryWin32A(UINT uiInitialSizeOfMapView, PCWSTR pObjectName)
  83. {
  84. ASSERT(pObjectName);
  85. CBuffer buffer;buffer.Allocate(wcslen(pObjectName) + 2);
  86. PSTR pBuffer = (PSTR)buffer.GetBuffer();
  87. sprintf(pBuffer, "%S", pObjectName);
  88. return CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, uiInitialSizeOfMapView, pBuffer);
  89. }
  90. HANDLE
  91. CreateSharedFileWin32A(
  92. IN PCWSTR pFilePath,
  93. IN DWORD dwShareMode,
  94. IN DWORD dwCreationDisposition,
  95. IN DWORD dwFlagsAndAttributes
  96. )
  97. {
  98. ASSERT(pFilePath);
  99. CBuffer buffer;buffer.Allocate(wcslen(pFilePath) + 2);
  100. PSTR pAnsiFilePath = (PSTR)buffer.GetBuffer();
  101. sprintf(pAnsiFilePath, "%S", pFilePath);
  102. return CreateFileA(pAnsiFilePath, GENERIC_WRITE | GENERIC_READ, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);
  103. }
  104. //
  105. // Unicode version
  106. //
  107. HANDLE OpenMutexWin32W(PCWSTR pObjectName)
  108. {
  109. ASSERT(pObjectName);
  110. return OpenMutexW(MUTEX_ALL_ACCESS, FALSE, pObjectName);
  111. }
  112. HANDLE CreateMutexWin32W(PCWSTR pObjectName, BOOL bInitialOwnership)
  113. {
  114. ASSERT(pObjectName);
  115. return CreateMutexW(NULL, bInitialOwnership, pObjectName);
  116. }
  117. HANDLE OpenSharedMemoryWin32W(PCWSTR pObjectName)
  118. {
  119. ASSERT(pObjectName);
  120. return OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, pObjectName);
  121. }
  122. HANDLE CreateSharedMemoryWin32W(UINT uiInitialSizeOfMapView, PCWSTR pObjectName)
  123. {
  124. ASSERT(pObjectName);
  125. return CreateFileMappingW(INVALID_HANDLE_VALUE,
  126. NULL,
  127. PAGE_READWRITE | SEC_COMMIT,
  128. 0,
  129. uiInitialSizeOfMapView,
  130. pObjectName);
  131. }
  132. HANDLE
  133. CreateSharedFileWin32W(
  134. IN PCWSTR pFilePath,
  135. IN DWORD dwShareMode,
  136. IN DWORD dwCreationDisposition,
  137. IN DWORD dwFlagsAndAttributes
  138. )
  139. {
  140. ASSERT(pFilePath);
  141. return CreateFileW(pFilePath,
  142. GENERIC_WRITE | GENERIC_READ,
  143. dwShareMode,
  144. NULL,
  145. dwCreationDisposition,
  146. dwFlagsAndAttributes,
  147. NULL);
  148. }
  149. BOOL
  150. IsSystemNT(
  151. VOID
  152. )
  153. {
  154. static BOOL bSystemIsNT = FALSE;
  155. static BOOL bFirstTime = TRUE;
  156. if(bFirstTime){
  157. bFirstTime = FALSE;
  158. bSystemIsNT = (GetVersion() < 0x80000000);
  159. }
  160. return bSystemIsNT;
  161. }
  162. UINT
  163. GetProcessorsNumberWin32(
  164. VOID
  165. )
  166. {
  167. SYSTEM_INFO systemInfo;
  168. GetSystemInfo(&systemInfo);
  169. return systemInfo.dwNumberOfProcessors;
  170. }
  171. BOOL InitSystemFunctions()
  172. {
  173. if(IsSystemNT()){
  174. g_OpenMutex = OpenMutexWin32W;
  175. g_CreateMutex = CreateMutexWin32W;
  176. g_OpenSharedMemory = OpenSharedMemoryWin32W;
  177. g_CreateSharedMemory = CreateSharedMemoryWin32W;
  178. g_CreateSharedFile = CreateSharedFileWin32W;
  179. }
  180. else{
  181. g_OpenMutex = OpenMutexWin32A;
  182. g_CreateMutex = CreateMutexWin32A;
  183. g_OpenSharedMemory = OpenSharedMemoryWin32A;
  184. g_CreateSharedMemory = CreateSharedMemoryWin32A;
  185. g_CreateSharedFile = CreateSharedFileWin32A;
  186. }
  187. g_ReleaseMutex = ReleaseMutexWin32;
  188. g_WaitForSingleObject = WaitForSingleObjectWin32;
  189. g_CloseHandle = CloseHandleWin32;
  190. g_MapSharedMemory = MapSharedMemoryWin32;
  191. g_UnMapSharedMemory = UnMapSharedMemoryWin32;
  192. g_SetFilePointer = SetFilePointerWin32;
  193. g_WriteFile = WriteFileWin32;
  194. g_GetProcessorsNumber = GetProcessorsNumberWin32;
  195. return TRUE;
  196. }
  197. BOOL bInit = InitSystemFunctions();