dxcapi.use.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // dxcapi.use.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides support for DXC API users. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef __DXCAPI_USE_H__
  12. #define __DXCAPI_USE_H__
  13. #include "dxc/dxcapi.h"
  14. namespace dxc {
  15. // Helper class to dynamically load the dxcompiler or a compatible libraries.
  16. class DxcDllSupport {
  17. protected:
  18. HMODULE m_dll;
  19. DxcCreateInstanceProc m_createFn;
  20. DxcCreateInstance2Proc m_createFn2;
  21. HRESULT InitializeInternal(LPCWSTR dllName, LPCSTR fnName) {
  22. if (m_dll != nullptr) return S_OK;
  23. m_dll = LoadLibraryW(dllName);
  24. if (m_dll == nullptr) return HRESULT_FROM_WIN32(GetLastError());
  25. m_createFn = (DxcCreateInstanceProc)GetProcAddress(m_dll, fnName);
  26. if (m_createFn == nullptr) {
  27. HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
  28. FreeLibrary(m_dll);
  29. m_dll = nullptr;
  30. return hr;
  31. }
  32. // Only basic functions used to avoid requiring additional headers.
  33. m_createFn2 = nullptr;
  34. char fnName2[128];
  35. size_t s = strlen(fnName);
  36. if (s < sizeof(fnName2) - 2) {
  37. memcpy(fnName2, fnName, s);
  38. fnName2[s] = '2';
  39. fnName2[s + 1] = '\0';
  40. m_createFn2 = (DxcCreateInstance2Proc)GetProcAddress(m_dll, fnName2);
  41. }
  42. return S_OK;
  43. }
  44. public:
  45. DxcDllSupport() : m_dll(nullptr), m_createFn(nullptr), m_createFn2(nullptr) {
  46. }
  47. DxcDllSupport(DxcDllSupport&& other) {
  48. m_dll = other.m_dll; other.m_dll = nullptr;
  49. m_createFn = other.m_createFn; other.m_createFn = nullptr;
  50. m_createFn2 = other.m_createFn2; other.m_createFn2 = nullptr;
  51. }
  52. ~DxcDllSupport() {
  53. Cleanup();
  54. }
  55. HRESULT Initialize() {
  56. return InitializeInternal(L"dxcompiler.dll", "DxcCreateInstance");
  57. }
  58. HRESULT InitializeForDll(_In_z_ const wchar_t* dll, _In_z_ const char* entryPoint) {
  59. return InitializeInternal(dll, entryPoint);
  60. }
  61. template <typename TInterface>
  62. HRESULT CreateInstance(REFCLSID clsid, _Outptr_ TInterface** pResult) {
  63. return CreateInstance(clsid, __uuidof(TInterface), (IUnknown**)pResult);
  64. }
  65. HRESULT CreateInstance(REFCLSID clsid, REFIID riid, _Outptr_ IUnknown **pResult) {
  66. if (pResult == nullptr) return E_POINTER;
  67. if (m_dll == nullptr) return E_FAIL;
  68. HRESULT hr = m_createFn(clsid, riid, (LPVOID*)pResult);
  69. return hr;
  70. }
  71. template <typename TInterface>
  72. HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, _Outptr_ TInterface** pResult) {
  73. return CreateInstance2(pMalloc, clsid, __uuidof(TInterface), (IUnknown**)pResult);
  74. }
  75. HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid, _Outptr_ IUnknown **pResult) {
  76. if (pResult == nullptr) return E_POINTER;
  77. if (m_dll == nullptr) return E_FAIL;
  78. if (m_createFn2 == nullptr) return E_FAIL;
  79. HRESULT hr = m_createFn2(pMalloc, clsid, riid, (LPVOID*)pResult);
  80. return hr;
  81. }
  82. bool HasCreateWithMalloc() const {
  83. return m_createFn2 != nullptr;
  84. }
  85. bool IsEnabled() const {
  86. return m_dll != nullptr;
  87. }
  88. void Cleanup() {
  89. if (m_dll != nullptr) {
  90. m_createFn = nullptr;
  91. m_createFn2 = nullptr;
  92. FreeLibrary(m_dll);
  93. m_dll = nullptr;
  94. }
  95. }
  96. HMODULE Detach() {
  97. HMODULE module = m_dll;
  98. m_dll = nullptr;
  99. return module;
  100. }
  101. };
  102. inline DxcDefine GetDefine(_In_ LPCWSTR name, LPCWSTR value) {
  103. DxcDefine result;
  104. result.Name = name;
  105. result.Value = value;
  106. return result;
  107. }
  108. // Checks an HRESULT and formats an error message with the appended data.
  109. void IFT_Data(HRESULT hr, _In_opt_ LPCWSTR data);
  110. void EnsureEnabled(DxcDllSupport &dxcSupport);
  111. void ReadFileIntoBlob(DxcDllSupport &dxcSupport, _In_ LPCWSTR pFileName,
  112. _Outptr_ IDxcBlobEncoding **ppBlobEncoding);
  113. void WriteBlobToConsole(_In_opt_ IDxcBlob *pBlob, DWORD streamType = STD_OUTPUT_HANDLE);
  114. void WriteBlobToFile(_In_opt_ IDxcBlob *pBlob, _In_ LPCWSTR pFileName);
  115. void WriteBlobToHandle(_In_opt_ IDxcBlob *pBlob, HANDLE hFile, _In_opt_ LPCWSTR pFileName);
  116. void WriteUtf8ToConsole(_In_opt_count_(charCount) const char *pText,
  117. int charCount, DWORD streamType = STD_OUTPUT_HANDLE);
  118. void WriteUtf8ToConsoleSizeT(_In_opt_count_(charCount) const char *pText,
  119. size_t charCount, DWORD streamType = STD_OUTPUT_HANDLE);
  120. void WriteOperationErrorsToConsole(_In_ IDxcOperationResult *pResult,
  121. bool outputWarnings);
  122. void WriteOperationResultToConsole(_In_ IDxcOperationResult *pRewriteResult,
  123. bool outputWarnings);
  124. } // namespace dxc
  125. #endif