aardio编程,查询主板是否支持Nvme协议
- User
aardio编程,查询主板是否支持Nvme协议
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
aardio编程,查询主板是否支持Nvme协议
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
在 Aardio 中查询主板是否支持 NVMe 协议,主要可以通过以下几种方式,这些方式都是通过查询系统信息间接判断的,因为没有直接的 API 能告诉你“主板支持NVMe”。
我们将主要使用 WMI (Windows Management Instrumentation) 来获取这些信息。
下面是一个 Aardio 示例代码,它会尝试通过上述两种方法来判断:
import com;
import console;
// 函数:检查主板是否支持NVMe
function isNvmeSupported() {
var wmi;
var nvmeDriveDetected = false;
var nvmeControllerDetected = false;
var detectionReason = "";
// 方法1: 检查已安装的物理磁盘 (通过 MSFT_PhysicalDisk,这个更准确些)
// 这个类在 `root\microsoft\windows\storage` 命名空间
try {
wmi = com.Connect("winmgmts:\\\\.\\root\\microsoft\\windows\\storage");
if (wmi) {
var physicalDisks = wmi.ExecQuery("SELECT * FROM MSFT_PhysicalDisk");
if (physicalDisks && physicalDisks.Count > 0) {
for disk in physicalDisks {
// BusType 17 通常表示 NVMe
if (disk.BusType == 17) {
nvmeDriveDetected = true;
detectionReason = "检测到已安装的NVMe驱动器 (MSFT_PhysicalDisk.BusType = 17)";
break;
}
}
}
}
} catch (e) {
console.log("查询 MSFT_PhysicalDisk 出错: ", e.message);
}
// 如果方法1没检测到,尝试方法1.1: 检查 Win32_DiskDrive
if (!nvmeDriveDetected) {
try {
wmi = com.Connect("winmgmts:\\\\.\\root\\cimv2"); // 默认命名空间
if (wmi) {
var diskDrives = wmi.ExecQuery("SELECT * FROM Win32_DiskDrive");
if (diskDrives && diskDrives.Count > 0) {
for drive in diskDrives {
var interfaceType = string.lower(tostring(drive.InterfaceType));
var pnpDeviceID = string.lower(tostring(drive.PNPDeviceID));
// 检查 InterfaceType 是否为 "NVMe"
// 或者 PNPDeviceID 中是否包含 "NVME" (更通用一些)
if (interfaceType == "nvme" || (pnpDeviceID != null && pnpDeviceID.find("nvme"))) {
nvmeDriveDetected = true;
detectionReason = "检测到已安装的NVMe驱动器 (Win32_DiskDrive)";
break;
}
}
}
}
} catch (e) {
console.log("查询 Win32_DiskDrive 出错: ", e.message);
}
}
// 方法2: 检查是否存在NVMe控制器 (即使没有安装NVMe盘)
// 这个方法可以检测主板是否具备NVMe能力
if (!nvmeDriveDetected) { // 只有在没检测到NVMe硬盘时,才去检查控制器本身
try {
wmi = com.Connect("winmgmts:\\\\.\\root\\cimv2"); // 确保使用正确的WMI连接
if (wmi) {
// "Standard NVM Express Controller" 是 Windows 下标准的 NVMe 控制器名称
// 服务名 stornvme.sys 也是一个关键标识
var query = "SELECT * FROM Win32_PNPEntity WHERE " +
"(Name LIKE '%NVM Express Controller%' AND Name LIKE '%Standard%') OR " +
"(Description LIKE '%NVM Express Controller%' AND Description LIKE '%Standard%') OR " +
"Service = 'stornvme'";
var pnpEntities = wmi.ExecQuery(query);
if (pnpEntities && pnpEntities.Count > 0) {
// 只要找到一个符合条件的PNP实体,就认为存在NVMe控制器
nvmeControllerDetected = true;
detectionReason = "检测到NVMe控制器硬件 (Win32_PNPEntity)";
}
}
} catch (e) {
console.log("查询 Win32_PNPEntity 出错: ", e.message);
}
}
if (nvmeDriveDetected) {
return true, detectionReason;
} elseif (nvmeControllerDetected) {
return true, detectionReason + " (可能未安装NVMe硬盘,但主板具备能力)";
}
return false, "未能通过WMI查询明确检测到NVMe支持。";
}
// 调用函数并打印结果
var supported, reason = isNvmeSupported();
if (supported) {
console.log("主板支持NVMe协议。");
console.log("依据: " + reason);
} else {
console.log("主板可能不支持NVMe协议,或当前系统状态下无法确认。");
console.log("说明: " + reason);
console.log("注意:此检测依赖当前Windows系统对硬件的识别。BIOS中禁用或驱动问题可能导致误判。");
}
console.pause();
代码解释:
import com; 和 import console;: 导入COM库(用于WMI)和控制台库。isNvmeSupported() 函数:
root\microsoft\windows\storage WMI 命名空间。MSFT_PhysicalDisk 类。BusType 属性。如果 BusType 为 17,则表示该磁盘是 NVMe 设备。这是比较可靠的方法。root\cimv2 WMI 命名空间。Win32_DiskDrive 类。InterfaceType 属性是否为 "NVMe"(不区分大小写)。PNPDeviceID 是否包含 "NVME" 字符串。这是因为有些系统可能 InterfaceType 不直接标为 "NVMe",但其PNP ID会暴露信息。Win32_PNPEntity (Plug and Play实体)。Name 或 Description 包含 "Standard NVM Express Controller" 的设备,或者其 Service 属性为 stornvme (NVMe驱动服务名)。isNvmeSupported() 函数。重要注意事项:
stornvme.sys),则此脚本可能无法检测到支持。这段代码提供了一个比较全面的运行时检测方法。