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.

52 lines
1.4 KiB

  1. /******************************************************************************
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. safecs.cpp
  5. Abstract:
  6. Implements a safe InitializeCriticalSection (usable on all supported platforms)
  7. ******************************************************************************/
  8. #include "stdafx.h"
  9. BOOL WINAPI WUInitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpcs, DWORD dwSpinCount)
  10. {
  11. OSVERSIONINFO osvinfo;
  12. ZeroMemory(&osvinfo, sizeof(osvinfo));
  13. osvinfo.dwOSVersionInfoSize = sizeof(osvinfo);
  14. if (!GetVersionEx(&osvinfo))
  15. {
  16. return FALSE;
  17. }
  18. typedef BOOL (WINAPI* PROC_InitializeCriticalSectionAndSpinCount)(LPCRITICAL_SECTION, DWORD);
  19. PROC_InitializeCriticalSectionAndSpinCount pfnInitCS =
  20. (PROC_InitializeCriticalSectionAndSpinCount)GetProcAddress(
  21. GetModuleHandle(_T("kernel32.dll")), "InitializeCriticalSectionAndSpinCount");
  22. //
  23. // Don't use InitializeCriticalSectionAndSpinCount on Win9x.
  24. // It exists but returns VOID (it's a thunk to InitializeCriticalSection).
  25. //
  26. if (osvinfo.dwPlatformId == VER_PLATFORM_WIN32_NT && pfnInitCS != NULL)
  27. {
  28. return (*pfnInitCS)(lpcs, dwSpinCount);
  29. }
  30. else
  31. {
  32. BOOL fSuccess = TRUE;
  33. __try
  34. {
  35. InitializeCriticalSection(lpcs);
  36. }
  37. __except(EXCEPTION_EXECUTE_HANDLER)
  38. {
  39. fSuccess = FALSE;
  40. }
  41. return fSuccess;
  42. }
  43. }