As per the title, how can I check which version of DirectX a user has installed? Checking the FeatureLevel isn\'t enough, as my application can run on feature level 10.0, bu
Another possibility is using the IDxDiagProvider
COM object directly and browse through the IDxDiagContainer
hierarchy it yields - this is what dxdiag.exe
does internally. It requires a moment to complete too, so it's not a fast solution either, but at least you don't need to create or parse a raw file.
Apparently, this functionality was previously wrapped in the managed DirectX assemblies in Microsoft.DirectX.Diagnostics, as they have a very similar interface to what the COM objects provide, but these assemblies are outdated and not working in .NET Core, so let's wrap those COM objects ourselves! For documentation of the methods, you can still refer to the documentation linked above.
First, you need the IDxDiagProvider
interface and the DxDiagProvider
coclass together with the DXDIAG_INIT_PARAMS
passed to the provider:
[ComImport]
[Guid("A65B8071-3BFE-4213-9A5B-491DA4461CA7")]
public class DxDiagProvider { }
[Guid("9C6B4CB0-23F8-49CC-A3ED-45A55000A6D2")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDxDiagProvider
{
void Initialize(ref DXDIAG_INIT_PARAMS pParams);
void GetRootContainer(out IDxDiagContainer ppInstance);
}
[StructLayout(LayoutKind.Sequential)]
public struct DXDIAG_INIT_PARAMS
{
public int dwSize;
public uint dwDxDiagHeaderVersion;
public bool bAllowWHQLChecks;
public IntPtr pReserved;
};
You also need to wrap the IDxDiagContainer
class:
[Guid("7D0F462F-4064-4862-BC7F-933E5058C10F")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDxDiagContainer
{
void EnumChildContainerNames(uint dwIndex, string pwszContainer, uint cchContainer);
void EnumPropNames(uint dwIndex, string pwszPropName, uint cchPropName);
void GetChildContainer(string pwszContainer, out IDxDiagContainer ppInstance);
void GetNumberOfChildContainers(out uint pdwCount);
void GetNumberOfProps(out uint pdwCount);
void GetProp(string pwszPropName, out object pvarProp);
}
Now we get to use our wrappers and have to do the following to retrieve the version info:
Code which properly cleans up the COM resources can look like this:
IDxDiagProvider provider = null;
IDxDiagContainer rootContainer = null;
IDxDiagContainer systemInfoContainer = null;
try
{
// Instantiate and initialize the provider.
provider = (IDxDiagProvider)new DxDiagProvider();
DXDIAG_INIT_PARAMS initParams = new DXDIAG_INIT_PARAMS
{
dwSize = Marshal.SizeOf(),
dwDxDiagHeaderVersion = 111
};
provider.Initialize(ref initParams);
// Get the Root\SystemInfo container.
provider.GetRootContainer(out rootContainer);
rootContainer.GetChildContainer("DxDiag_SystemInfo", out systemInfoContainer);
// Read the DirectX version info.
int versionMajor = GetProperty(container, "dwDirectXVersionMajor");
int versionMinor = GetProperty(container, "dwDirectXVersionMinor");
string versionLetter = GetProperty(container, "szDirectXVersionLetter");
bool isDebug = GetProperty(container, "bDebug");
}
finally
{
if (provider != null)
Marshal.ReleaseComObject(provider);
if (rootContainer != null)
Marshal.ReleaseComObject(rootContainer);
if (systemInfoContainer != null)
Marshal.ReleaseComObject(systemInfoContainer);
}
As you can see there's a small utility GetProperty
method I created to retrieve a correctly typed property from the VARIANT
values the COM interface returns:
private static T GetProperty(IDxDiagContainer container, string propName)
{
container.GetProp(propName, out object variant);
return (T)Convert.ChangeType(variant, typeof(T));
}