From 6b50fe22619c775ec82cae2a6d64161621c1796a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 29 Dec 2020 16:46:20 +0100 Subject: [PATCH] Fixed error handling for SetDllDirectoryW --- .../Translation/ShaderCompiler.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 02a1e56e..6eb47106 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; @@ -37,12 +38,7 @@ namespace ComputeSharp.Shaders.Translation /// static ShaderCompiler() { - FX.ResolveLibrary += ResolveLibrary; - - fixed (char* p = @"runtimes\win-x64\native") - { - FX.SetDllDirectoryW((ushort*)p).Assert(); - } + InitializeDxcLibrariesLoading(); using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; @@ -61,6 +57,21 @@ namespace ComputeSharp.Shaders.Translation DxcIncludeHandler = dxcIncludeHandler.Move(); } + /// + /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. + /// + private static void InitializeDxcLibrariesLoading() + { + FX.ResolveLibrary += ResolveLibrary; + + fixed (char* p = @"runtimes\win-x64\native") + { + int result = FX.SetDllDirectoryW((ushort*)p); + + if (result == 0) ThrowHelper.ThrowWin32Exception(); + } + } + /// /// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries. ///