Reputation: 4429
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, but requires that DirectX 11.1 be installed.
Why this is not a duplicate:
I need an answer that applies to DirectX 10 installs and up. That means determining if their version is 10, 10.1, 11 or 11.1.
Upvotes: 4
Views: 6775
Reputation: 8881
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<DXDIAG_INIT_PARAMS>(),
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<int>(container, "dwDirectXVersionMajor");
int versionMinor = GetProperty<int>(container, "dwDirectXVersionMinor");
string versionLetter = GetProperty<string>(container, "szDirectXVersionLetter");
bool isDebug = GetProperty<bool>(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<T>(IDxDiagContainer container, string propName)
{
container.GetProp(propName, out object variant);
return (T)Convert.ChangeType(variant, typeof(T));
}
Upvotes: 4
Reputation: 4733
EDIT: Removed registry check method because it works only for Dx <=9 (thx @Telanor)
This method is very, very slow, but only one I figured out that is 100% accurate
private static int checkdxversion_dxdiag()
{
Process.Start("dxdiag", "/x dxv.xml");
while (!File.Exists("dxv.xml"))
Thread.Sleep(1000);
XmlDocument doc = new XmlDocument();
doc.Load("dxv.xml");
XmlNode dxd = doc.SelectSingleNode("//DxDiag");
XmlNode dxv = dxd.SelectSingleNode("//DirectXVersion");
return Convert.ToInt32(dxv.InnerText.Split(' ')[1]);
}
Upvotes: 3