在 Win32 API 的 Cfgmgr32.h 头文件中,CM_Get_Class_Registry_PropertyW 函数用于获取指定设备类的注册表属性。以下是该函数的原型:
CMAPI CONFIGRET WINAPI CM_Get_Class_Registry_PropertyW(
  _In_     LPGUID      ClassGuid,
  _In_     ULONG       ulProperty,
  _Out_    PULONG      pulRegDataType, // 可选
  _Out_writes_bytes_opt_(*pulLength) PVOID       Buffer,
  _Inout_  PULONG      pulLength,
  _In_     ULONG       ulFlags,
  _In_opt_ HMACHINE    hMachine
);

函数参数说明如下:

  •  ClassGuid: 指向设备类的 GUID 的指针。

  •  ulProperty: 指定要获取的注册表属性,如 CM_DRP_DEVICEDESC。

  •  pulRegDataType: 可选参数,用于接收注册表数据类型。可以为 NULL。

  •  Buffer: 用于接收注册表属性值的缓冲区指针。可以为 NULL,用于查询所需缓冲区大小。

  •  pulLength: 输入时为缓冲区大小,输出时为实际写入的缓冲区大小。

  •  ulFlags: 保留,必须为0。

  •  hMachine: 机器句柄,指示函数在哪台机器上执行。可以为 NULL,表示本地机器。


如果函数成功执行,将返回 CR_SUCCESS;否则,将返回相应的错误码,可以使用 CM_Get_Last_Error 函数获取详细的错误信息。

以下是一个示例代码:
#include <windows.h>
#include <Cfgmgr32.h>

int main() {
    CONFIGRET cr;
    GUID classGuid = { /* your class GUID here */ };
    ULONG registryProperty = CM_DRP_DEVICEDESC; // 例如,获取设备描述
    ULONG regDataType;
    ULONG bufferLength = 0;

    // Query the required buffer size
    cr = CM_Get_Class_Registry_PropertyW(&classGuid, registryProperty, &regDataType, NULL, &bufferLength, 0, NULL);
    if (cr != CR_SUCCESS && cr != CR_BUFFER_SMALL) {
        // Handle error
        return 1;
    }

    // Allocate buffer
    PVOID buffer = malloc(bufferLength);
    if (buffer == NULL) {
        // Handle memory allocation error
        return 1;
    }

    // Get the registry property value
    cr = CM_Get_Class_Registry_PropertyW(&classGuid, registryProperty, &regDataType, buffer, &bufferLength, 0, NULL);
    if (cr == CR_SUCCESS) {
        // Use the registry property value
        // ...
    } else {
        // Handle error
    }

    // Cleanup
    free(buffer);

    return 0;
}

请确保在使用时将 classGuid 替换为实际的设备类 GUID,并根据需要调整 registryProperty。


转载请注明出处:http://www.pingtaimeng.com/article/detail/24298/Win32 API/Cfgmgr32.h/CM_Get_Class_Registry_PropertyW