Win32ApiWinRTEmulation.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINRT
  3. #include "os/Win32/WindowsHeaders.h"
  4. #include <string>
  5. #include <windows.system.profile.h>
  6. #include <windows.system.userprofile.h>
  7. #include "os/Mutex.h"
  8. #include "SynchronousOperation.h"
  9. #include "utils/Il2CppHashMap.h"
  10. #include "utils/Memory.h"
  11. #include "utils/StringUtils.h"
  12. #include "Win32ApiSharedEmulation.h"
  13. #include "Win32ApiWinRTEmulation.h"
  14. #include "Baselib.h"
  15. #include "Cpp/ReentrantLock.h"
  16. using namespace ABI::Windows::Foundation;
  17. using namespace ABI::Windows::System::Profile;
  18. using namespace ABI::Windows::System::UserProfile;
  19. using namespace Microsoft::WRL;
  20. using namespace Microsoft::WRL::Wrappers;
  21. using namespace il2cpp::winrt;
  22. #if WINDOWS_SDK_BUILD_VERSION < 16299
  23. struct WideStringHash
  24. {
  25. public:
  26. size_t operator()(const std::wstring& str) const
  27. {
  28. return Hash(str);
  29. }
  30. static size_t Hash(const std::wstring& str)
  31. {
  32. return il2cpp::utils::StringUtils::Hash((const char*)str.c_str(), sizeof(std::wstring::value_type) * str.length());
  33. }
  34. };
  35. typedef Il2CppHashMap<std::wstring, std::wstring, WideStringHash> EnvironmentVariableMap;
  36. static EnvironmentVariableMap s_EnvironmentVariables;
  37. static baselib::ReentrantLock s_EnvironmentVariablesMutex;
  38. #endif
  39. extern "C"
  40. {
  41. #if WINDOWS_SDK_BUILD_VERSION < 16299
  42. BOOL WINAPI FreeEnvironmentStringsW(LPWCH strings)
  43. {
  44. IL2CPP_FREE(strings);
  45. return TRUE;
  46. }
  47. LPWCH WINAPI GetEnvironmentStringsW()
  48. {
  49. il2cpp::os::FastAutoLock lock(&s_EnvironmentVariablesMutex);
  50. // Two iterations
  51. // 1) Figure out length
  52. // 2) Make result string
  53. size_t length = 0;
  54. for (EnvironmentVariableMap::const_iterator it = s_EnvironmentVariables.begin(); it != s_EnvironmentVariables.end(); it++)
  55. {
  56. // Key + value + '=' + '\0'
  57. length += it->first.key.length() + it->second.length() + 2;
  58. }
  59. // Terminating '\0'
  60. length++;
  61. LPWCH result = static_cast<LPWCH>(IL2CPP_MALLOC(length * sizeof(WCHAR)));
  62. size_t index = 0;
  63. for (EnvironmentVariableMap::const_iterator it = s_EnvironmentVariables.begin(); it != s_EnvironmentVariables.end(); it++)
  64. {
  65. const size_t keyLength = it->first.key.length();
  66. const size_t valueLength = it->second.length();
  67. memcpy(result + index, it->first.key.c_str(), keyLength * sizeof(WCHAR));
  68. index += keyLength;
  69. result[index++] = L'=';
  70. memcpy(result + index, it->second.c_str(), valueLength * sizeof(WCHAR));
  71. index += valueLength;
  72. result[index++] = '\0';
  73. }
  74. result[index++] = '\0';
  75. IL2CPP_ASSERT(index == length);
  76. return result;
  77. }
  78. DWORD WINAPI GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)
  79. {
  80. il2cpp::os::FastAutoLock lock(&s_EnvironmentVariablesMutex);
  81. std::wstring key(lpName);
  82. auto it = s_EnvironmentVariables.find(key);
  83. if (it == s_EnvironmentVariables.end())
  84. return 0;
  85. DWORD sizeNeeded = static_cast<DWORD>(it->second.length());
  86. if (nSize < sizeNeeded)
  87. {
  88. SetLastError(ERROR_BUFFER_OVERFLOW);
  89. }
  90. else
  91. {
  92. memcpy(lpBuffer, it->second.data(), (sizeNeeded + 1) * sizeof(wchar_t));
  93. }
  94. return sizeNeeded;
  95. }
  96. BOOL WINAPI GetVersionExW(LPOSVERSIONINFOW lpVersionInformation)
  97. {
  98. Assert(lpVersionInformation->dwOSVersionInfoSize == sizeof(OSVERSIONINFOW));
  99. #define ERROR_CHECK(hr) do { if (FAILED(hr)) { SetLastError(WIN32_FROM_HRESULT(hr)); return FALSE; } } while (false)
  100. ComPtr<IAnalyticsInfoStatics> analytics;
  101. auto hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_System_Profile_AnalyticsInfo).Get(), __uuidof(IAnalyticsInfoStatics), &analytics);
  102. ERROR_CHECK(hr);
  103. ComPtr<IAnalyticsVersionInfo> versionInfo;
  104. hr = analytics->get_VersionInfo(&versionInfo);
  105. ERROR_CHECK(hr);
  106. HString versionString;
  107. hr = versionInfo->get_DeviceFamilyVersion(versionString.GetAddressOf());
  108. ERROR_CHECK(hr);
  109. #undef ERROR_CHECK
  110. unsigned int dummy;
  111. int64_t versionNumber = _wtoi64(versionString.GetRawBuffer(&dummy));
  112. if (versionNumber == 0)
  113. {
  114. SetLastError(ERROR_VERSION_PARSE_ERROR);
  115. return FALSE;
  116. }
  117. lpVersionInformation->dwMajorVersion = versionNumber >> 48;
  118. lpVersionInformation->dwMinorVersion = (versionNumber >> 32) & 0xFFFF;
  119. lpVersionInformation->dwBuildNumber = (versionNumber >> 16) & 0xFFFF;
  120. lpVersionInformation->dwPlatformId = VER_PLATFORM_WIN32_NT;
  121. ZeroMemory(lpVersionInformation->szCSDVersion, sizeof(lpVersionInformation->szCSDVersion));
  122. return TRUE;
  123. }
  124. BOOL WINAPI SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue)
  125. {
  126. il2cpp::os::FastAutoLock lock(&s_EnvironmentVariablesMutex);
  127. if (lpValue != NULL)
  128. {
  129. s_EnvironmentVariables[std::wstring(lpName)] = lpValue;
  130. }
  131. else
  132. {
  133. s_EnvironmentVariables.erase(std::wstring(lpName));
  134. }
  135. return TRUE;
  136. }
  137. #endif
  138. BOOL WINAPI GetUserNameW(LPWSTR lpBuffer, LPDWORD pcbBuffer)
  139. {
  140. #define ERROR_CHECK(hr) do { if (FAILED(hr)) { SetLastError(WIN32_FROM_HRESULT(hr)); return FALSE; } } while (false)
  141. ComPtr<IUserInformationStatics> info;
  142. auto hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_System_UserProfile_UserInformation).Get(), __uuidof(info), &info);
  143. ERROR_CHECK(hr);
  144. boolean isAccessAllowed;
  145. hr = info->get_NameAccessAllowed(&isAccessAllowed);
  146. ERROR_CHECK(hr);
  147. if (!isAccessAllowed)
  148. {
  149. SetLastError(ERROR_ACCESS_DENIED);
  150. return FALSE;
  151. }
  152. ComPtr<IAsyncOperation<HSTRING> > op;
  153. hr = info->GetDisplayNameAsync(&op);
  154. ERROR_CHECK(hr);
  155. HString name;
  156. hr = MakeSynchronousOperation(op.Get())->GetResults(name.GetAddressOf());
  157. ERROR_CHECK(hr);
  158. #undef ERROR_CHECK
  159. return CopyHStringToBuffer(name, lpBuffer, pcbBuffer);
  160. }
  161. } // extern "C"
  162. #endif