我想从 .NET 代码中找出机器上是否支持 DirectX 10,最好不使用托管 DirectX 或 XNA 程序集。
先感谢您!
这实际上非常简单:如果您使用的是 Windows Vista / Server 2008 或更高版本,则您拥有“DirectX 10”API。如果您使用的是 Windows Vista / Server 2008 Service Pack 1 或更高版本,则您拥有“DirectX 10.1”API。
这些都没有回答更有用的问题:系统是否有兼容 DirectX 10 的视频设备和驱动程序?
真正检测到这一点的唯一可靠方法是创建设备。如果您可以 (a) 找到D3D10.DLL
并且 (b) 调用D3D10CreateDevice
成功,那么您同时拥有 DirectX 10 API 和 10 兼容设备。
同样,如果您可以 (a) 找到D3D10_1.DLL
并且 (b) 调用D3D10CreateDevice1
成功,则说明您同时拥有 DirectX 10.1 API 和 10.0 或 10.1 兼容设备。
DirectX 11.0 或更高版本始终存在于 Windows 7 / Server 2008 R2 或更高版本上。同样,如果您可以 (a) 找到D3D11.DLL
并且 (b) 调用D3D11CreateDevice
成功,那么您同时拥有 DirectX 11 API 和某个功能级别的 11 兼容设备。创建设备的参数将确定允许的功能级别。此过程还可用于检测您使用的是 Windows Vista / Server 2008 Service Pack 2 系统并应用了KB97644更新的情况。
一旦你有了ID3D11Device
QueryInterfaceID3D11Device1
来查看系统是否有 DirectX 11.1(Windows 8 / Server 2012 或 Windows 7 / Server 2008 R2 with KB2670838,或者ID3D1Device2
查看系统是否有 DirectX 11.2(Windows 8.1 / Server 2012 R2)
“安装什么版本的 DirectX”的概念已经过时,可以追溯到 Windows 9x/ME 时代。运行“DirectX End-User Runtime Redist”会做一些事情,但它永远不会安装新版本的 DirectX。请参阅不那么 DirectSetup。DirectX 运行时纯粹是 OS 补丁级别的功能,自 2004 年以来一直存在。请参阅版本号中有什么?也是。
要检查 DirectX10,我们实际上是通过平台调用调用D3DX10CheckVersion 函数。
这需要在 C:\Windows\System32 或 C:\Windows\SysWow64 文件夹(取决于平台)中存在 D3DX10 DLL。如果这些不存在,那么您需要在目标 PC 上安装DirectX10 运行时。
实际支持的 DirectX 版本/DirectX SDK 版本可以通过 P/Invoke 调用的参数来确定。
这是 P/Invoke 调用:
// DX10 Supported Function
// See D3DX10CheckVersion http://msdn.microsoft.com/en-us/library/windows/desktop/bb172639(v=vs.85).aspx
[DllImport("d3dx10_43.dll", EntryPoint = "D3DX10CheckVersion", CallingConvention = CallingConvention.StdCall, SetLastError = false)]
private static extern HResult D3DX10CheckVersion(
uint D3DSdkVersion,
uint D3DX10SdkVersion);
public enum HResult : int
{
S_OK = 0x0000,
S_FALSE = 0x0001,
E_NOTIMPL = 0x0002,
E_INVALIDARG = 0x0003,
E_FAIL = 0x0004,
};
bool SupportsDirectX10()
{
// Values taken from d3d10.h to check DirectX version
const uint D3D10_SDK_VERSION = 29;
const uint D3DX10_SDK_VERSION = 43;
// Checks DirectX 10 GPU compatibility with the DirectX10 runtime
// Assumes D3DX10 DLLs present in C:\Windows\System32 or
// C:\Windows\SysWow64 folders (platform dependent)
HResult hResult = D3DX10CheckVersion(D3D10_SDK_VERSION, D3DX10_SDK_VERSION);
return hResult == HResult.S_OK;
}
您可以使用此密钥配置单元查看计算机上安装的 DirectX 版本
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectX
这是一个示例
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,All = "HKEY_LOCAL_MACHINE")]
class Test
{
public int DxLevel
{
get
{
using(RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\DirectX"))
{
string versionStr = key.GetValue("Version") as string;
if (!string.IsNullOrEmpty(versionStr))
{
var versionComponents = versionStr.Split('.');
if (versionComponents.Length > 1)
{
int directXLevel;
if (int.TryParse(versionComponents[1], out directXLevel))
return directXLevel;
}
}
return -1;
}
}
}
}
现在,如果您想知道您的视频卡是否支持 DirectX,这将需要 XNA 或 DirectX 互操作。
请注意,我现在无法在我的机器上测试该代码,但这应该让你开始:)