在 Windows Vista 和更新版本的 Windows 中,統一裝置屬性模型 支援代表裝置實例識別碼的裝置屬性。 統一裝置屬性模型會使用 DEVPKEY_Device_InstanceId屬性索引鍵 來表示此屬性。
Windows Server 2003、Windows XP 和 Windows 2000 也支持此屬性。 不過,這些舊版 Windows 不支援統一裝置屬性模型的屬性索引鍵。 相反地,您可以藉由呼叫 CM_Get_Device_ID 或 SetupDiGetDeviceInstanceId來擷取這些舊版 Windows 上的裝置實例標識符。 為了維持與這些舊版 Windows 的相容性,Windows Vista 和更新版本也支援 CM_Get_Device_ID 和 SetupDiGetDeviceInstanceId。 不過,您應該使用對應的屬性索引鍵來存取 Windows Vista 和更新版本上的這個屬性。
如需如何使用屬性金鑰來存取 Windows Vista 和更新版本上的裝置驅動器屬性的詳細資訊,請參閱 存取裝置實例屬性 (Windows Vista 和更新版本)。
若要擷取 Windows Server 2003、Windows XP 和 Windows 2000 上的裝置實例識別符,請參閱下列範例。
裝置實例標識符字串必須小於 MAX_DEVICE_ID_LEN
字元(包括 NULL),其定義於 cfgmgr32.h中。 您可以使用該假設來使用下列程式代碼來查詢裝置實例識別碼:
WCHAR DeviceInstancePath[MAX_DEVICE_ID_LEN];
cr = CM_Get_Device_ID(DevInst,
DeviceInstancePath,
sizeof(DeviceInstancePath)/sizeof(DeviceInstancePath[0]),
0);
if (cr != CR_SUCCESS) {
printf("Error 0x%08x retrieving device instance path.\n", cr);
} else {
printf("Device instance path is %ws.\n", DeviceInstancePath);
}
或者,如果您想要動態調整緩衝區大小:
ULONG DeviceInstancePathLength = 0;
PWSTR DeviceInstancePath = NULL;
cr = CM_Get_Device_ID_Size(&DeviceInstancePathLength,
DevInst,
0);
if (cr != CR_SUCCESS) {
printf("Error 0x%08x retrieving device instance path size.\n", cr);
} else {
DeviceInstancePath = (PWSTR)malloc(DeviceInstancePathLength * sizeof(WCHAR));
if (DeviceInstancePath != NULL) {
cr = CM_Get_Device_ID(DevInst,
DeviceInstancePath,
DeviceInstancePathLength,
0);
if (cr != CR_SUCCESS) {
printf("Error 0x%08x retrieving device instance path.\n", cr);
} else {
printf("Device instance path is %ws.\n", DeviceInstancePath);
}
}
}