Environment.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_WINRT || IL2CPP_TARGET_XBOXONE
  3. #include "os\Environment.h"
  4. #include "os\Win32\WindowsHelpers.h"
  5. #include "utils\Expected.h"
  6. #include "utils\Il2CppError.h"
  7. #include "utils\StringUtils.h"
  8. #include <windows.storage.h>
  9. #include <wrl.h>
  10. using namespace ABI::Windows::Storage;
  11. using namespace Microsoft::WRL;
  12. using namespace Microsoft::WRL::Wrappers;
  13. #if !IL2CPP_TARGET_XBOXONE
  14. namespace winrt_interfaces
  15. {
  16. MIDL_INTERFACE("F9C53912-ABC4-46FF-8A2B-DC9D7FA6E52F")
  17. IUserDataPaths : IInspectable
  18. {
  19. virtual HRESULT STDMETHODCALLTYPE get_CameraRoll(HSTRING* value) = 0;
  20. virtual HRESULT STDMETHODCALLTYPE get_Cookies(HSTRING* value) = 0;
  21. virtual HRESULT STDMETHODCALLTYPE get_Desktop(HSTRING* value) = 0;
  22. virtual HRESULT STDMETHODCALLTYPE get_Documents(HSTRING* value) = 0;
  23. virtual HRESULT STDMETHODCALLTYPE get_Downloads(HSTRING* value) = 0;
  24. virtual HRESULT STDMETHODCALLTYPE get_Favorites(HSTRING* value) = 0;
  25. virtual HRESULT STDMETHODCALLTYPE get_History(HSTRING* value) = 0;
  26. virtual HRESULT STDMETHODCALLTYPE get_InternetCache(HSTRING* value) = 0;
  27. virtual HRESULT STDMETHODCALLTYPE get_LocalAppData(HSTRING* value) = 0;
  28. virtual HRESULT STDMETHODCALLTYPE get_LocalAppDataLow(HSTRING* value) = 0;
  29. virtual HRESULT STDMETHODCALLTYPE get_Music(HSTRING* value) = 0;
  30. virtual HRESULT STDMETHODCALLTYPE get_Pictures(HSTRING* value) = 0;
  31. virtual HRESULT STDMETHODCALLTYPE get_Profile(HSTRING* value) = 0;
  32. virtual HRESULT STDMETHODCALLTYPE get_Recent(HSTRING* value) = 0;
  33. virtual HRESULT STDMETHODCALLTYPE get_RoamingAppData(HSTRING* value) = 0;
  34. virtual HRESULT STDMETHODCALLTYPE get_SavedPictures(HSTRING* value) = 0;
  35. virtual HRESULT STDMETHODCALLTYPE get_Screenshots(HSTRING* value) = 0;
  36. virtual HRESULT STDMETHODCALLTYPE get_Templates(HSTRING* value) = 0;
  37. virtual HRESULT STDMETHODCALLTYPE get_Videos(HSTRING* value) = 0;
  38. };
  39. MIDL_INTERFACE("01B29DEF-E062-48A1-8B0C-F2C7A9CA56C0")
  40. IUserDataPathsStatics : IInspectable
  41. {
  42. virtual HRESULT STDMETHODCALLTYPE GetForUser(struct IUser* user, IUserDataPaths** result) = 0;
  43. virtual HRESULT STDMETHODCALLTYPE GetDefault(IUserDataPaths** result) = 0;
  44. };
  45. }
  46. #endif
  47. namespace il2cpp
  48. {
  49. namespace os
  50. {
  51. #define CSIDL_DESKTOP 0x0000 // <desktop>
  52. #define CSIDL_PROGRAMS 0x0002 // Start Menu\Programs
  53. #define CSIDL_PERSONAL 0x0005 // My Documents
  54. #define CSIDL_FAVORITES 0x0006 // <user name>\Favorites
  55. #define CSIDL_STARTUP 0x0007 // Start Menu\Programs\Startup
  56. #define CSIDL_RECENT 0x0008 // <user name>\Recent
  57. #define CSIDL_SENDTO 0x0009 // <user name>\SendTo
  58. #define CSIDL_STARTMENU 0x000b // <user name>\Start Menu
  59. #define CSIDL_MYMUSIC 0x000d // "My Music" folder
  60. #define CSIDL_MYVIDEO 0x000e // "My Videos" folder
  61. #define CSIDL_DESKTOPDIRECTORY 0x0010 // <user name>\Desktop
  62. #define CSIDL_DRIVES 0x0011 // My Computer
  63. #define CSIDL_NETWORK 0x0012 // Network Neighborhood (My Network Places)
  64. #define CSIDL_NETHOOD 0x0013 // <user name>\nethood
  65. #define CSIDL_FONTS 0x0014 // windows\fonts
  66. #define CSIDL_TEMPLATES 0x0015
  67. #define CSIDL_COMMON_STARTMENU 0x0016 // All Users\Start Menu
  68. #define CSIDL_COMMON_PROGRAMS 0X0017 // All Users\Start Menu\Programs
  69. #define CSIDL_COMMON_STARTUP 0x0018 // All Users\Startup
  70. #define CSIDL_COMMON_DESKTOPDIRECTORY 0x0019 // All Users\Desktop
  71. #define CSIDL_APPDATA 0x001a // <user name>\Application Data
  72. #define CSIDL_PRINTHOOD 0x001b // <user name>\PrintHood
  73. #define CSIDL_LOCAL_APPDATA 0x001c // <user name>\Local Settings\Applicaiton Data (non roaming)
  74. #define CSIDL_ALTSTARTUP 0x001d // non localized startup
  75. #define CSIDL_COMMON_ALTSTARTUP 0x001e // non localized common startup
  76. #define CSIDL_COMMON_FAVORITES 0x001f
  77. #define CSIDL_INTERNET_CACHE 0x0020
  78. #define CSIDL_COOKIES 0x0021
  79. #define CSIDL_HISTORY 0x0022
  80. #define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data
  81. #define CSIDL_WINDOWS 0x0024 // GetWindowsDirectory()
  82. #define CSIDL_SYSTEM 0x0025 // GetSystemDirectory()
  83. #define CSIDL_PROGRAM_FILES 0x0026 // C:\Program Files
  84. #define CSIDL_MYPICTURES 0x0027 // C:\Program Files\My Pictures
  85. #define CSIDL_PROFILE 0x0028 // USERPROFILE
  86. #define CSIDL_SYSTEMX86 0x0029 // x86 system directory on RISC
  87. #define CSIDL_PROGRAM_FILESX86 0x002a // x86 C:\Program Files on RISC
  88. #define CSIDL_PROGRAM_FILES_COMMON 0x002b // C:\Program Files\Common
  89. #define CSIDL_PROGRAM_FILES_COMMONX86 0x002c // x86 Program Files\Common on RISC
  90. #define CSIDL_COMMON_TEMPLATES 0x002d // All Users\Templates
  91. #define CSIDL_COMMON_DOCUMENTS 0x002e // All Users\Documents
  92. #define CSIDL_COMMON_ADMINTOOLS 0x002f // All Users\Start Menu\Programs\Administrative Tools
  93. #define CSIDL_ADMINTOOLS 0x0030 // <user name>\Start Menu\Programs\Administrative Tools
  94. #define CSIDL_CONNECTIONS 0x0031 // Network and Dial-up Connections
  95. #define CSIDL_COMMON_MUSIC 0x0035 // All Users\My Music
  96. #define CSIDL_COMMON_PICTURES 0x0036 // All Users\My Pictures
  97. #define CSIDL_COMMON_VIDEO 0x0037 // All Users\My Video
  98. #define CSIDL_RESOURCES 0x0038 // Resource Direcotry
  99. #define CSIDL_RESOURCES_LOCALIZED 0x0039 // Localized Resource Direcotry
  100. #define CSIDL_COMMON_OEM_LINKS 0x003a // Links to All Users OEM specific apps
  101. #define CSIDL_CDBURN_AREA 0x003b // USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning
  102. #define CSIDL_COMPUTERSNEARME 0x003d // Computers Near Me (computered from Workgroup membership)
  103. template<typename T>
  104. static inline utils::Expected<std::string> GetAppFolder(T appDataToStorageFolder)
  105. {
  106. ComPtr<IApplicationDataStatics> appDataStatics;
  107. ComPtr<IApplicationData> appData;
  108. ComPtr<IStorageFolder> appDataFolder;
  109. ComPtr<IStorageItem> appDataFolderItem;
  110. HString appDataPath;
  111. auto hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_ApplicationData).Get(), __uuidof(IApplicationDataStatics), &appDataStatics);
  112. if (IL2CPP_HR_FAILED(hr))
  113. return utils::Il2CppError(utils::ComError, hr);
  114. hr = appDataStatics->get_Current(&appData);
  115. if (IL2CPP_HR_FAILED(hr))
  116. return utils::Il2CppError(utils::ComError, hr);
  117. hr = appDataToStorageFolder(appData.Get(), &appDataFolder);
  118. if (IL2CPP_HR_FAILED(hr))
  119. return utils::Il2CppError(utils::ComError, hr);
  120. hr = appDataFolder.As(&appDataFolderItem);
  121. if (IL2CPP_HR_FAILED(hr))
  122. return utils::Il2CppError(utils::ComError, hr);
  123. hr = appDataFolderItem->get_Path(appDataPath.GetAddressOf());
  124. if (IL2CPP_HR_FAILED(hr))
  125. return utils::Il2CppError(utils::ComError, hr);
  126. unsigned int dummy;
  127. return utils::StringUtils::Utf16ToUtf8(appDataPath.GetRawBuffer(&dummy));
  128. }
  129. static inline utils::Expected<std::string> GetLocalAppDataFolder()
  130. {
  131. return GetAppFolder([](IApplicationData* appData, IStorageFolder** folder) { return appData->get_LocalFolder(folder); });
  132. }
  133. #if !IL2CPP_TARGET_XBOXONE
  134. static inline utils::Expected<std::string> GetRoamingAppDataFolder()
  135. {
  136. return GetAppFolder([](IApplicationData* appData, IStorageFolder** folder) { return appData->get_RoamingFolder(folder); });
  137. }
  138. template<typename T>
  139. static utils::Expected<std::string> GetUserFolderPath(T&& userDataPathToFolderPathConverter)
  140. {
  141. ComPtr<winrt_interfaces::IUserDataPathsStatics> userDataPathsStatics;
  142. auto hr = RoGetActivationFactory(HStringReference(L"Windows.Storage.UserDataPaths").Get(), __uuidof(userDataPathsStatics), &userDataPathsStatics);
  143. if (FAILED(hr))
  144. {
  145. // Before OS version 16299, you weren't allowed to touch these folders.
  146. // In OS version 16299 they added UserDataPaths class for this purpose
  147. // If that class does not exist, we throw UnauthorizedAccessException
  148. return utils::Il2CppError(utils::UnauthorizedAccess, "Failed getting the path of a special folder: Access Denied.");
  149. }
  150. ComPtr<winrt_interfaces::IUserDataPaths> userDataPaths;
  151. hr = userDataPathsStatics->GetDefault(&userDataPaths);
  152. if (IL2CPP_HR_FAILED(hr))
  153. return utils::Il2CppError(utils::ComError, hr);
  154. HString resultHString;
  155. hr = userDataPathToFolderPathConverter(userDataPaths.Get(), resultHString.GetAddressOf());
  156. if (IL2CPP_HR_FAILED(hr))
  157. return utils::Il2CppError(utils::ComError, hr);
  158. unsigned int dummy;
  159. return utils::StringUtils::Utf16ToUtf8(resultHString.GetRawBuffer(&dummy));
  160. }
  161. #endif
  162. utils::Expected<std::string> Environment::GetWindowsFolderPath(int32_t folder)
  163. {
  164. switch (folder)
  165. {
  166. #if !IL2CPP_TARGET_XBOXONE
  167. case CSIDL_APPDATA:
  168. return GetRoamingAppDataFolder();
  169. case CSIDL_COOKIES:
  170. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Cookies(result); });
  171. case CSIDL_DESKTOP:
  172. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Desktop(result); });
  173. case CSIDL_FAVORITES:
  174. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Favorites(result); });
  175. case CSIDL_HISTORY:
  176. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_History(result); });
  177. case CSIDL_INTERNET_CACHE:
  178. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_InternetCache(result); });
  179. case CSIDL_MYMUSIC:
  180. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Music(result); });
  181. case CSIDL_MYPICTURES:
  182. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Pictures(result); });
  183. case CSIDL_MYVIDEO:
  184. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Videos(result); });
  185. case CSIDL_PERSONAL:
  186. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Documents(result); });
  187. case CSIDL_PROFILE:
  188. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Profile(result); });
  189. case CSIDL_RECENT:
  190. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Recent(result); });
  191. case CSIDL_TEMPLATES:
  192. return GetUserFolderPath([](winrt_interfaces::IUserDataPaths* userDataPaths, HSTRING* result) { return userDataPaths->get_Templates(result); });
  193. #endif
  194. case CSIDL_LOCAL_APPDATA:
  195. return GetLocalAppDataFolder();
  196. default:
  197. return utils::Il2CppError(utils::UnauthorizedAccess, "Failed getting the path of a special folder: Access Denied.");
  198. }
  199. return std::string();
  200. }
  201. utils::Expected<bool> Environment::Is64BitOs()
  202. {
  203. #if IL2CPP_TARGET_WINRT
  204. #if WINDOWS_SDK_BUILD_VERSION >= 16299
  205. BOOL isWow64Process = FALSE;
  206. if (IsWow64Process(GetCurrentProcess(), &isWow64Process))
  207. return isWow64Process == TRUE;
  208. #endif // WINDOWS_SDK_BUILD_VERSION >= 16299
  209. return false;
  210. #endif // IL2CPP_TARGET_WINRT
  211. return true;
  212. }
  213. }
  214. }
  215. #endif