SystemInfo.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace PlatformAgnostic
  7. {
  8. class SystemInfo
  9. {
  10. public:
  11. static bool GetMaxVirtualMemory(size_t *totalAS);
  12. #define SET_BINARY_PATH_ERROR_MESSAGE(path, msg) \
  13. str_len = (int) strlen(msg); \
  14. memcpy(path, msg, (size_t)str_len); \
  15. path[str_len] = char(0)
  16. #ifdef _WIN32
  17. static bool GetBinaryLocation(char *path, const unsigned size)
  18. {
  19. // TODO: make AssertMsg available under PlatformAgnostic
  20. //AssertMsg(path != nullptr, "Path can not be nullptr");
  21. //AssertMsg(size < INT_MAX, "Isn't it too big for a path buffer?");
  22. LPWSTR wpath = (WCHAR*)malloc(sizeof(WCHAR) * size);
  23. int str_len;
  24. if (!wpath)
  25. {
  26. SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed. OutOfMemory!");
  27. return false;
  28. }
  29. str_len = GetModuleFileNameW(NULL, wpath, size - 1);
  30. if (str_len <= 0)
  31. {
  32. SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed.");
  33. free(wpath);
  34. return false;
  35. }
  36. str_len = WideCharToMultiByte(CP_UTF8, 0, wpath, str_len, path, size, NULL, NULL);
  37. free(wpath);
  38. if (str_len <= 0)
  39. {
  40. SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName (WideCharToMultiByte) has failed.");
  41. return false;
  42. }
  43. if ((unsigned)str_len > size - 1)
  44. {
  45. str_len = (int)size - 1;
  46. }
  47. path[str_len] = char(0);
  48. return true;
  49. }
  50. // Overloaded GetBinaryLocation: receive the location of current binary in char16.
  51. // size: the second parameter is the size of the path buffer in count of wide characters.
  52. static bool GetBinaryLocation(char16 *path, const unsigned size)
  53. {
  54. // TODO: make AssertMsg available under PlatformAgnostic
  55. //AssertMsg(path != nullptr, "Path can not be nullptr");
  56. //AssertMsg(size < INT_MAX, "Isn't it too big for a path buffer?");
  57. int str_len = GetModuleFileNameW(NULL, path, size);
  58. if (str_len <= 0)
  59. {
  60. wcscpy_s(path, size, _u("GetBinaryLocation: GetModuleFileName has failed."));
  61. return false;
  62. }
  63. return true;
  64. }
  65. #else
  66. static bool GetBinaryLocation(char *path, const unsigned size);
  67. static bool GetBinaryLocation(char16 *path, const unsigned size);
  68. #endif
  69. };
  70. } // namespace PlatformAgnostic