DelayLoadLibrary.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. //#include <roapi.h>
  7. #include "activation.h"
  8. #include <winstring.h>
  9. #ifdef ENABLE_PROJECTION
  10. // cor.h includes corhdr.h which is not clean with warning 4091
  11. #pragma warning(push)
  12. #pragma warning(disable: 4091) /* warning C4091: 'typedef ': ignored on left of '' when no variable is declared */
  13. #include <cor.h>
  14. #pragma warning(pop)
  15. #endif
  16. #include "RoParameterizedIID.h"
  17. namespace Js
  18. {
  19. class DelayLoadWinRtString : public DelayLoadLibrary
  20. {
  21. private:
  22. // WinRTString specific functions
  23. typedef HRESULT FNCWindowsCreateString(const WCHAR *, UINT32, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *);
  24. typedef FNCWindowsCreateString* PFNCWindowsCreateString;
  25. PFNCWindowsCreateString m_pfnWindowsCreateString;
  26. typedef HRESULT FNCWindowsCreateStringReference(const WCHAR *, UINT32, HSTRING_HEADER *, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *);
  27. typedef FNCWindowsCreateStringReference* PFNCWindowsCreateStringReference;
  28. PFNCWindowsCreateStringReference m_pfnWindowsCreateStringReference;
  29. typedef PCWSTR FNCWindowsGetStringRawBuffer(HSTRING, UINT32*);
  30. typedef FNCWindowsGetStringRawBuffer* PFNCWindowsGetStringRawBuffer;
  31. PFNCWindowsGetStringRawBuffer m_pfWindowsGetStringRawBuffer;
  32. typedef HRESULT FNCWindowsDeleteString(HSTRING);
  33. typedef FNCWindowsDeleteString* PFNCWindowsDeleteString;
  34. PFNCWindowsDeleteString m_pfnWindowsDeleteString;
  35. typedef HRESULT FNCWindowsCompareStringOrdinal(HSTRING,HSTRING,INT32*);
  36. typedef FNCWindowsCompareStringOrdinal* PFNCWindowsCompareStringOrdinal;
  37. PFNCWindowsCompareStringOrdinal m_pfnWindowsCompareStringOrdinal;
  38. typedef HRESULT FNCWindowsDuplicateString(HSTRING, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING*);
  39. typedef FNCWindowsDuplicateString* PFNCWindowsDuplicateString;
  40. PFNCWindowsDuplicateString m_pfnWindowsDuplicateString;
  41. public:
  42. DelayLoadWinRtString() : DelayLoadLibrary(),
  43. m_pfnWindowsCreateString(NULL),
  44. m_pfWindowsGetStringRawBuffer(NULL),
  45. m_pfnWindowsDeleteString(NULL),
  46. m_pfnWindowsCreateStringReference(NULL),
  47. m_pfnWindowsDuplicateString(NULL),
  48. m_pfnWindowsCompareStringOrdinal(NULL) { }
  49. virtual ~DelayLoadWinRtString() { }
  50. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-winrt-string-l1-1-0.dll"); }
  51. virtual HRESULT WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string);
  52. virtual HRESULT WindowsCreateStringReference(_In_reads_opt_(length + 1) const WCHAR * sourceString, UINT32 length, _Out_ HSTRING_HEADER * header, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string);
  53. virtual HRESULT WindowsDeleteString(_In_opt_ HSTRING string);
  54. virtual PCWSTR WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length);
  55. virtual HRESULT WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result);
  56. virtual HRESULT WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * newString);
  57. };
  58. class DelayLoadWinRtTypeResolution sealed : public DelayLoadLibrary
  59. {
  60. private:
  61. // WinRtTypeResolution specific functions
  62. typedef HRESULT FNCWRoParseTypeName(HSTRING, DWORD *, HSTRING **);
  63. typedef FNCWRoParseTypeName* PFNCWRoParseTypeName;
  64. PFNCWRoParseTypeName m_pfnRoParseTypeName;
  65. typedef HRESULT FNCRoResolveNamespace(const HSTRING, const HSTRING, const DWORD, const HSTRING*, DWORD*, HSTRING**, DWORD*, HSTRING**);
  66. typedef FNCRoResolveNamespace* PFNCRoResolveNamespace;
  67. PFNCRoResolveNamespace m_pfnRoResolveNamespace;
  68. public:
  69. DelayLoadWinRtTypeResolution() : DelayLoadLibrary(),
  70. m_pfnRoParseTypeName(nullptr) { }
  71. virtual ~DelayLoadWinRtTypeResolution() { }
  72. LPCTSTR GetLibraryName() const { return _u("api-ms-win-ro-typeresolution-l1-1-0.dll"); }
  73. HRESULT RoParseTypeName(__in HSTRING typeName, __out DWORD *partsCount, __RPC__deref_out_ecount_full_opt(*partsCount) HSTRING **typeNameParts);
  74. HRESULT RoResolveNamespace(
  75. __in_opt const HSTRING namespaceName,
  76. __in_opt const HSTRING windowsMetaDataPath,
  77. __in const DWORD packageGraphPathsCount,
  78. __in_opt const HSTRING *packageGraphPaths,
  79. __out DWORD *metaDataFilePathsCount,
  80. HSTRING **metaDataFilePaths,
  81. __out DWORD *subNamespacesCount,
  82. HSTRING **subNamespaces);
  83. };
  84. #ifdef ENABLE_PROJECTION
  85. class DelayLoadWinRtRoParameterizedIID sealed : public DelayLoadLibrary
  86. {
  87. private:
  88. // WinRtRoParameterizedIID specific functions
  89. typedef HRESULT FNCWRoGetParameterizedTypeInstanceIID(UINT32, PCWSTR*, const IRoMetaDataLocator&, GUID*, ROPARAMIIDHANDLE*);
  90. typedef FNCWRoGetParameterizedTypeInstanceIID* PFNCWRoGetParameterizedTypeInstanceIID;
  91. PFNCWRoGetParameterizedTypeInstanceIID m_pfnRoGetParameterizedTypeInstanceIID;
  92. public:
  93. DelayLoadWinRtRoParameterizedIID() : DelayLoadLibrary(),
  94. m_pfnRoGetParameterizedTypeInstanceIID(nullptr) { }
  95. virtual ~DelayLoadWinRtRoParameterizedIID() { }
  96. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-winrt-roparameterizediid-l1-1-0.dll"); }
  97. HRESULT RoGetParameterizedTypeInstanceIID(
  98. __in UINT32 nameElementCount,
  99. __in_ecount(nameElementCount) PCWSTR* nameElements,
  100. __in const IRoMetaDataLocator& metaDataLocator,
  101. __out GUID* iid,
  102. __deref_opt_out ROPARAMIIDHANDLE* pExtra = nullptr);
  103. };
  104. #endif
  105. #ifdef INTL_WINGLOB
  106. class DelayLoadWindowsGlobalization sealed : public DelayLoadWinRtString
  107. {
  108. private:
  109. // DelayLoadWindowsGlobalization specific functions
  110. typedef HRESULT FNCWDllGetActivationFactory(HSTRING clsid, IActivationFactory** factory);
  111. typedef FNCWDllGetActivationFactory* PFNCWDllGetActivationFactory;
  112. PFNCWDllGetActivationFactory m_pfnFNCWDllGetActivationFactory;
  113. Js::DelayLoadWinRtString *winRTStringLibrary;
  114. bool winRTStringsPresent;
  115. bool hasGlobalizationDllLoaded;
  116. public:
  117. DelayLoadWindowsGlobalization() : DelayLoadWinRtString(),
  118. m_pfnFNCWDllGetActivationFactory(nullptr),
  119. winRTStringLibrary(nullptr),
  120. winRTStringsPresent(false),
  121. hasGlobalizationDllLoaded(false) { }
  122. virtual ~DelayLoadWindowsGlobalization() { }
  123. LPCTSTR GetLibraryName() const
  124. {
  125. return _u("windows.globalization.dll");
  126. }
  127. LPCTSTR GetWin7LibraryName() const
  128. {
  129. return _u("jsIntl.dll");
  130. }
  131. void Ensure(Js::DelayLoadWinRtString *winRTStringLibrary);
  132. HRESULT DllGetActivationFactory(__in HSTRING activatibleClassId, __out IActivationFactory** factory);
  133. bool HasGlobalizationDllLoaded();
  134. HRESULT WindowsCreateString(_In_reads_opt_(length) const WCHAR * sourceString, UINT32 length, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string) override;
  135. HRESULT WindowsCreateStringReference(_In_reads_opt_(length+1) const WCHAR * sourceString, UINT32 length, _Out_ HSTRING_HEADER * header, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING * string) override;
  136. HRESULT WindowsDeleteString(_In_opt_ HSTRING string) override;
  137. PCWSTR WindowsGetStringRawBuffer(_In_opt_ HSTRING string, _Out_opt_ UINT32 * length) override;
  138. HRESULT WindowsCompareStringOrdinal(_In_opt_ HSTRING string1, _In_opt_ HSTRING string2, _Out_ INT32 * result) override;
  139. HRESULT WindowsDuplicateString(_In_opt_ HSTRING original, _Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING *newString) override;
  140. };
  141. #endif
  142. class DelayLoadWinRtFoundation sealed : public DelayLoadLibrary
  143. {
  144. private:
  145. // DelayLoadWindowsFoundation specific functions
  146. typedef HRESULT FNCWRoGetActivationFactory(HSTRING clsid, REFIID iid, IActivationFactory** factory);
  147. typedef FNCWRoGetActivationFactory* PFNCWRoGetActivationFactory;
  148. PFNCWRoGetActivationFactory m_pfnFNCWRoGetActivationFactory;
  149. public:
  150. DelayLoadWinRtFoundation() : DelayLoadLibrary(),
  151. m_pfnFNCWRoGetActivationFactory(nullptr) { }
  152. virtual ~DelayLoadWinRtFoundation() { }
  153. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-winrt-l1-1-0.dll"); }
  154. HRESULT RoGetActivationFactory(
  155. __in HSTRING activatibleClassId,
  156. __in REFIID iid,
  157. __out IActivationFactory** factory);
  158. };
  159. #ifdef ENABLE_PROJECTION
  160. class DelayLoadWinRtError sealed : public DelayLoadLibrary
  161. {
  162. private:
  163. // DelayLoadWinRtError specific functions
  164. typedef void FNCRoClearError();
  165. typedef FNCRoClearError* PFNCRoClearError;
  166. PFNCRoClearError m_pfnRoClearError;
  167. typedef BOOL FNCRoOriginateLanguageException(HRESULT, HSTRING, IUnknown *);
  168. typedef FNCRoOriginateLanguageException* PFNCRoOriginateLanguageException;
  169. PFNCRoOriginateLanguageException m_pfnRoOriginateLanguageException;
  170. public:
  171. DelayLoadWinRtError() : DelayLoadLibrary(),
  172. m_pfnRoClearError(nullptr),
  173. m_pfnRoOriginateLanguageException(nullptr) { }
  174. virtual ~DelayLoadWinRtError() { }
  175. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-winrt-error-l1-1-1.dll"); }
  176. HRESULT RoClearError();
  177. BOOL RoOriginateLanguageException(__in HRESULT error, __in_opt HSTRING message, __in IUnknown * languageException);
  178. };
  179. #endif
  180. #if defined(_CONTROL_FLOW_GUARD)
  181. class DelayLoadWinCoreMemory sealed : public DelayLoadLibrary
  182. {
  183. private:
  184. // LoadWinCoreMemory specific functions
  185. typedef BOOL FNCSetProcessValidCallTargets(HANDLE, PVOID, SIZE_T, ULONG, PCFG_CALL_TARGET_INFO);
  186. typedef FNCSetProcessValidCallTargets* PFNCSetProcessValidCallTargets;
  187. PFNCSetProcessValidCallTargets m_pfnSetProcessValidCallTargets;
  188. public:
  189. DelayLoadWinCoreMemory() : DelayLoadLibrary(),
  190. m_pfnSetProcessValidCallTargets(nullptr) { }
  191. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-memory-l1-1-3.dll"); }
  192. BOOL SetProcessCallTargets(
  193. _In_ HANDLE hProcess,
  194. _In_ PVOID VirtualAddress,
  195. _In_ SIZE_T RegionSize,
  196. _In_ ULONG NumberOfOffsets,
  197. _In_reads_(NumberOfOffsets) PCFG_CALL_TARGET_INFO OffsetInformation
  198. );
  199. };
  200. #endif
  201. class DelayLoadWinCoreProcessThreads sealed : public DelayLoadLibrary
  202. {
  203. private:
  204. // LoadWinCoreMemory specific functions
  205. typedef BOOL FNCGetMitigationPolicyForProcess(HANDLE, PROCESS_MITIGATION_POLICY, PVOID, SIZE_T);
  206. typedef FNCGetMitigationPolicyForProcess* PFNCGetMitigationPolicyForProcess;
  207. PFNCGetMitigationPolicyForProcess m_pfnGetProcessMitigationPolicy;
  208. typedef BOOL FNCGetProcessInformation(HANDLE, PROCESS_INFORMATION_CLASS, PVOID, SIZE_T);
  209. typedef FNCGetProcessInformation* PFNCGetProcessInformation;
  210. PFNCGetProcessInformation m_pfnGetProcessInformation;
  211. public:
  212. DelayLoadWinCoreProcessThreads() :
  213. DelayLoadLibrary(),
  214. m_pfnGetProcessMitigationPolicy(nullptr),
  215. m_pfnGetProcessInformation(nullptr)
  216. {
  217. }
  218. LPCTSTR GetLibraryName() const { return _u("api-ms-win-core-processthreads-l1-1-3.dll"); }
  219. BOOL GetMitigationPolicyForProcess(
  220. __in HANDLE hProcess,
  221. __in PROCESS_MITIGATION_POLICY MitigationPolicy,
  222. __out_bcount(nLength) PVOID lpBuffer,
  223. __in SIZE_T nLength
  224. );
  225. BOOL GetProcessInformation(
  226. __in HANDLE hProcess,
  227. __in PROCESS_INFORMATION_CLASS ProcessInformationClass,
  228. __out_bcount(nLength) PVOID lpBuffer,
  229. __in SIZE_T nLength
  230. );
  231. };
  232. #ifdef ENABLE_PROJECTION
  233. // Implement this function inlined so that WinRT.lib can be used without the runtime.
  234. inline HRESULT DelayLoadWinRtRoParameterizedIID::RoGetParameterizedTypeInstanceIID(
  235. __in UINT32 nameElementCount,
  236. __in_ecount(nameElementCount) PCWSTR* nameElements,
  237. __in const IRoMetaDataLocator& metaDataLocator,
  238. __out GUID* iid,
  239. __deref_opt_out ROPARAMIIDHANDLE* pExtra)
  240. {
  241. if (m_hModule)
  242. {
  243. if (m_pfnRoGetParameterizedTypeInstanceIID == NULL)
  244. {
  245. m_pfnRoGetParameterizedTypeInstanceIID = (PFNCWRoGetParameterizedTypeInstanceIID)GetFunction("RoGetParameterizedTypeInstanceIID");
  246. if (m_pfnRoGetParameterizedTypeInstanceIID == NULL)
  247. {
  248. return E_UNEXPECTED;
  249. }
  250. }
  251. Assert(m_pfnRoGetParameterizedTypeInstanceIID != NULL);
  252. return m_pfnRoGetParameterizedTypeInstanceIID(nameElementCount, nameElements, metaDataLocator, iid, pExtra);
  253. }
  254. return E_NOTIMPL;
  255. }
  256. #endif
  257. }