DelayLoadLibrary.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include "RuntimeBasePch.h"
  6. #ifdef _CONTROL_FLOW_GUARD
  7. #if !defined(DELAYLOAD_SET_CFG_TARGET)
  8. extern "C"
  9. WINBASEAPI
  10. BOOL
  11. WINAPI
  12. SetProcessValidCallTargets(
  13. _In_ HANDLE hProcess,
  14. _In_ PVOID VirtualAddress,
  15. _In_ SIZE_T RegionSize,
  16. _In_ ULONG NumberOfOffsets,
  17. _In_reads_(NumberOfOffsets) PCFG_CALL_TARGET_INFO OffsetInformation
  18. );
  19. #endif
  20. #endif
  21. namespace Js
  22. {
  23. HRESULT DelayLoadWinRtString::WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  24. {
  25. if (m_hModule)
  26. {
  27. if (m_pfnWindowsCreateString == nullptr)
  28. {
  29. m_pfnWindowsCreateString = (PFNCWindowsCreateString)GetFunction("WindowsCreateString");
  30. if (m_pfnWindowsCreateString == nullptr)
  31. {
  32. *string = nullptr;
  33. return E_UNEXPECTED;
  34. }
  35. }
  36. Assert(m_pfnWindowsCreateString != nullptr);
  37. return m_pfnWindowsCreateString(sourceString, length, string);
  38. }
  39. *string = nullptr;
  40. return E_NOTIMPL;
  41. }
  42. HRESULT DelayLoadWinRtString::WindowsCreateStringReference(_In_reads_opt_(length + 1) const WCHAR *sourceString, UINT32 length, _Out_ HSTRING_HEADER *hstringHeader, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  43. {
  44. if (m_hModule)
  45. {
  46. if (m_pfnWindowsCreateStringReference == nullptr)
  47. {
  48. m_pfnWindowsCreateStringReference = (PFNCWindowsCreateStringReference)GetFunction("WindowsCreateStringReference");
  49. if (m_pfnWindowsCreateStringReference == nullptr)
  50. {
  51. *string = nullptr;
  52. return E_UNEXPECTED;
  53. }
  54. }
  55. Assert(m_pfnWindowsCreateStringReference != nullptr);
  56. return m_pfnWindowsCreateStringReference(sourceString, length, hstringHeader, string);
  57. }
  58. *string = nullptr;
  59. return E_NOTIMPL;
  60. }
  61. HRESULT DelayLoadWinRtString::WindowsDeleteString(_In_opt_ HSTRING string)
  62. {
  63. if (m_hModule)
  64. {
  65. if (m_pfnWindowsDeleteString == nullptr)
  66. {
  67. m_pfnWindowsDeleteString = (PFNCWindowsDeleteString)GetFunction("WindowsDeleteString");
  68. if (m_pfnWindowsDeleteString == nullptr)
  69. {
  70. return E_UNEXPECTED;
  71. }
  72. }
  73. Assert(m_pfnWindowsDeleteString != nullptr);
  74. HRESULT hr = m_pfnWindowsDeleteString(string);
  75. Assert(SUCCEEDED(hr));
  76. return hr;
  77. }
  78. return E_NOTIMPL;
  79. }
  80. PCWSTR DelayLoadWinRtString::WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length)
  81. {
  82. if (m_hModule)
  83. {
  84. if (m_pfWindowsGetStringRawBuffer == nullptr)
  85. {
  86. m_pfWindowsGetStringRawBuffer = (PFNCWindowsGetStringRawBuffer)GetFunction("WindowsGetStringRawBuffer");
  87. if (m_pfWindowsGetStringRawBuffer == nullptr)
  88. {
  89. if (length)
  90. {
  91. *length = 0;
  92. }
  93. return _u("\0");
  94. }
  95. }
  96. Assert(m_pfWindowsGetStringRawBuffer != nullptr);
  97. return m_pfWindowsGetStringRawBuffer(string, length);
  98. }
  99. if (length)
  100. {
  101. *length = 0;
  102. }
  103. return _u("\0");
  104. }
  105. HRESULT DelayLoadWinRtString::WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result)
  106. {
  107. if (m_hModule)
  108. {
  109. if (m_pfnWindowsCompareStringOrdinal == nullptr)
  110. {
  111. m_pfnWindowsCompareStringOrdinal = (PFNCWindowsCompareStringOrdinal)GetFunction("WindowsCompareStringOrdinal");
  112. if (m_pfnWindowsCompareStringOrdinal == nullptr)
  113. {
  114. return E_UNEXPECTED;
  115. }
  116. }
  117. Assert(m_pfnWindowsCompareStringOrdinal != nullptr);
  118. return m_pfnWindowsCompareStringOrdinal(string1,string2,result);
  119. }
  120. return E_NOTIMPL;
  121. }
  122. HRESULT DelayLoadWinRtString::WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *newString)
  123. {
  124. if(m_hModule)
  125. {
  126. if(m_pfnWindowsDuplicateString == nullptr)
  127. {
  128. m_pfnWindowsDuplicateString = (PFNCWindowsDuplicateString)GetFunction("WindowsDuplicateString");
  129. if(m_pfnWindowsDuplicateString == nullptr)
  130. {
  131. *newString = nullptr;
  132. return E_UNEXPECTED;
  133. }
  134. }
  135. Assert(m_pfnWindowsDuplicateString != nullptr);
  136. return m_pfnWindowsDuplicateString(original, newString);
  137. }
  138. *newString = nullptr;
  139. return E_NOTIMPL;
  140. }
  141. #ifdef INTL_WINGLOB
  142. bool DelayLoadWindowsGlobalization::HasGlobalizationDllLoaded()
  143. {
  144. return this->hasGlobalizationDllLoaded;
  145. }
  146. HRESULT DelayLoadWindowsGlobalization::DllGetActivationFactory(
  147. __in HSTRING activatableClassId,
  148. __out IActivationFactory** factory)
  149. {
  150. if (m_hModule)
  151. {
  152. if (m_pfnFNCWDllGetActivationFactory == nullptr)
  153. {
  154. m_pfnFNCWDllGetActivationFactory = (PFNCWDllGetActivationFactory)GetFunction("DllGetActivationFactory");
  155. if (m_pfnFNCWDllGetActivationFactory == nullptr)
  156. {
  157. return E_UNEXPECTED;
  158. }
  159. }
  160. Assert(m_pfnFNCWDllGetActivationFactory != nullptr);
  161. return m_pfnFNCWDllGetActivationFactory(activatableClassId, factory);
  162. }
  163. return E_NOTIMPL;
  164. }
  165. #endif
  166. HRESULT DelayLoadWinRtFoundation::RoGetActivationFactory(
  167. __in HSTRING activatableClassId,
  168. __in REFIID iid,
  169. __out IActivationFactory** factory)
  170. {
  171. if (m_hModule)
  172. {
  173. if (m_pfnFNCWRoGetActivationFactory == nullptr)
  174. {
  175. m_pfnFNCWRoGetActivationFactory = (PFNCWRoGetActivationFactory)GetFunction("RoGetActivationFactory");
  176. if (m_pfnFNCWRoGetActivationFactory == nullptr)
  177. {
  178. return E_UNEXPECTED;
  179. }
  180. }
  181. Assert(m_pfnFNCWRoGetActivationFactory != nullptr);
  182. return m_pfnFNCWRoGetActivationFactory(activatableClassId, iid, factory);
  183. }
  184. return E_NOTIMPL;
  185. }
  186. #ifdef INTL_WINGLOB
  187. void DelayLoadWindowsGlobalization::Ensure(Js::DelayLoadWinRtString *winRTStringLibrary)
  188. {
  189. if (!this->m_isInit)
  190. {
  191. DelayLoadLibrary::EnsureFromSystemDirOnly();
  192. #if DBG
  193. // This unused variable is to allow one to see the value of lastError in case both LoadLibrary (DelayLoadLibrary::Ensure has one) fail.
  194. // As the issue might be with the first one, as opposed to the second
  195. DWORD errorWhenLoadingBluePlus = GetLastError();
  196. Unused(errorWhenLoadingBluePlus);
  197. #endif
  198. //Perform a check to see if Windows.Globalization.dll was loaded; if not try loading jsIntl.dll as we are on Win7.
  199. if (m_hModule == nullptr)
  200. {
  201. m_hModule = LoadLibraryEx(GetWin7LibraryName(), nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
  202. }
  203. // Set the flag depending on Windows.globalization.dll or jsintl.dll was loaded successfully or not
  204. if (m_hModule != nullptr)
  205. {
  206. hasGlobalizationDllLoaded = true;
  207. }
  208. this->winRTStringLibrary = winRTStringLibrary;
  209. this->winRTStringsPresent = GetFunction("WindowsDuplicateString") != nullptr;
  210. }
  211. }
  212. HRESULT DelayLoadWindowsGlobalization::WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  213. {
  214. //If winRtStringLibrary isn't nullptr, that means it is available and we are on Win8+
  215. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  216. {
  217. return winRTStringLibrary->WindowsCreateString(sourceString, length, string);
  218. }
  219. return DelayLoadWinRtString::WindowsCreateString(sourceString, length, string);
  220. }
  221. HRESULT DelayLoadWindowsGlobalization::WindowsCreateStringReference(_In_reads_opt_(length + 1) const WCHAR * sourceString, UINT32 length, _Out_ HSTRING_HEADER * header, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string)
  222. {
  223. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  224. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  225. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  226. {
  227. return winRTStringLibrary->WindowsCreateStringReference(sourceString, length, header, string);
  228. }
  229. return DelayLoadWinRtString::WindowsCreateStringReference(sourceString, length, header, string);
  230. }
  231. HRESULT DelayLoadWindowsGlobalization::WindowsDeleteString(_In_opt_ HSTRING string)
  232. {
  233. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  234. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  235. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  236. {
  237. return winRTStringLibrary->WindowsDeleteString(string);
  238. }
  239. return DelayLoadWinRtString::WindowsDeleteString(string);
  240. }
  241. PCWSTR DelayLoadWindowsGlobalization::WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length)
  242. {
  243. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  244. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  245. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  246. {
  247. return winRTStringLibrary->WindowsGetStringRawBuffer(string, length);
  248. }
  249. return DelayLoadWinRtString::WindowsGetStringRawBuffer(string, length);
  250. }
  251. HRESULT DelayLoadWindowsGlobalization::WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result)
  252. {
  253. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  254. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  255. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  256. {
  257. return winRTStringLibrary->WindowsCompareStringOrdinal(string1, string2, result);
  258. }
  259. return DelayLoadWinRtString::WindowsCompareStringOrdinal(string1, string2, result);
  260. }
  261. HRESULT DelayLoadWindowsGlobalization::WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *newString)
  262. {
  263. //First, we attempt to use the WinStringRT api encapsulated in the globalization dll; if it is available then it is a downlevel dll.
  264. //Otherwise; we might run into an error where we are using the Win8 (because testing is being done for instance) with the downlevel dll, and that would cause errors.
  265. if(!winRTStringsPresent && winRTStringLibrary->IsAvailable())
  266. {
  267. return winRTStringLibrary->WindowsDuplicateString(original, newString);
  268. }
  269. return DelayLoadWinRtString::WindowsDuplicateString(original, newString);
  270. }
  271. #endif
  272. BOOL DelayLoadWinCoreProcessThreads::GetProcessInformation(
  273. __in HANDLE hProcess,
  274. __in PROCESS_INFORMATION_CLASS ProcessInformationClass,
  275. __out_bcount(nLength) PVOID lpBuffer,
  276. __in SIZE_T nLength
  277. )
  278. {
  279. #if defined(DELAYLOAD_SET_CFG_TARGET) || defined(_M_ARM)
  280. if (m_hModule)
  281. {
  282. if (m_pfnGetProcessInformation == nullptr)
  283. {
  284. m_pfnGetProcessInformation = (PFNCGetProcessInformation)GetFunction("GetProcessInformation");
  285. if (m_pfnGetProcessInformation == nullptr)
  286. {
  287. return FALSE;
  288. }
  289. }
  290. Assert(m_pfnGetProcessInformation != nullptr);
  291. return m_pfnGetProcessInformation(hProcess, ProcessInformationClass, lpBuffer, nLength);
  292. }
  293. #endif
  294. return FALSE;
  295. }
  296. }