【发布时间】:2015-02-20 16:08:36
【问题描述】:
我想从 .NET 代码中了解机器是否支持 DirectX 10,最好不使用托管 DirectX 或 XNA 程序集。
提前谢谢你!
【问题讨论】:
标签: .net directx-10
我想从 .NET 代码中了解机器是否支持 DirectX 10,最好不使用托管 DirectX 或 XNA 程序集。
提前谢谢你!
【问题讨论】:
标签: .net directx-10
实际上非常简单:如果您使用的是 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 和某些 feature level 的 11 兼容设备。 create device 的参数将确定允许的功能级别。此过程还可用于检测您使用的是 Windows Vista / Server 2008 Service Pack 2 系统并应用了KB97644 更新的情况。
一旦您拥有ID3D11Device,您可以查询ID3D11Device1 的接口以查看系统是否具有 DirectX 11.1(Windows 8 / Server 2012 或 Windows 7 / Server 2008 R2 与KB2670838,或ID3D1Device2 以查看是否系统有 DirectX 11.2 (Windows 8.1 / Server 2012 R2)
见Direct3D 11 Deployment for Game Developers
“安装什么版本的 DirectX”的概念已经过时,可以追溯到 Windows 9x/ME 时代。运行“DirectX End-User Runtime Redist”会做一些事情,但它从不安装新版本的 DirectX。见Not So DirectSetup。 DirectX 运行时纯粹是 OS 补丁级别的功能,自 2004 年以来一直存在。参见What's in a version number?。
【讨论】:
要检查 DirectX10,我们实际上是通过平台调用调用 D3DX10CheckVersion function。
这要求 C:\Windows\System32 或 C:\Windows\SysWow64 文件夹中存在 D3DX10 DLL(取决于平台)。如果这些不存在,那么您需要在目标 PC 上安装 DirectX10 Runtime。
实际支持的 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 互操作。
请注意,我现在无法在我的机器上测试该代码,但这应该可以帮助您入门 :)
【讨论】: