Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Dieses Thema enthält Codebeispiele, die zeigen, wie Sie Code portieren, der Setupapi.dll Funktionalität verwendet, damit stattdessen Cfgmgr32.dll verwendet wird. Durch das Portieren Ihres Codes können Sie Ihren Code auf der Universelle Windows-Plattform (UWP) ausführen, die SetupApi nicht unterstützt. Eine Teilmenge von CfgMgr32 wird für UWP unterstützt, insbesondere funktionen, die über den api-ms-win-devices-config-l1-1-0.dll API-Satz (Windows 8 und höher) oder den api-ms-win-devices-config-l1-1-1.dll API-Satz (Windows 8.1 und höher) verfügbar gemacht werden. In Windows 10 und höher einfach mit onecore.lib verknüpfen.
Eine Liste der Funktionen in den obigen API-Sätzen finden Sie unter Windows API Sets oder Onecore.lib: APIs aus api-ms-win-devices-config-l1-1-1.dll.
Die folgenden Abschnitte enthalten Codebeispiele, die anwendungen normalerweise verwenden würden.
- Rufen Sie eine Liste der vorhandenen Geräte ab und rufen Sie für jedes Gerät eine Eigenschaft ab
- Rufen Sie eine Liste der Schnittstellen ab, rufen Sie das Gerät ab, das jede Schnittstelle bereitstellt, und rufen Sie eine Eigenschaft vom Gerät ab.
- Abrufen einer Eigenschaft von einem bestimmten Gerät
- Gerät deaktivieren
- Gerät aktivieren
- Gerät neu starten
Eine Liste der vorhandenen Geräte abrufen und für jedes Gerät eine Eigenschaft abrufen
In diesem Beispiel wird eine Liste aller vorhandenen Geräte mithilfe von SetupDiGetClassDevs abgerufen und durchlaufen, um die Gerätebeschreibung jedes Geräts abzurufen.
VOID
GetDevicePropertiesSetupapi(
VOID
)
{
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
DWORD Index;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
DeviceInfoSet = SetupDiGetClassDevs(NULL,
NULL,
NULL,
DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
goto Exit;
}
ZeroMemory(&DeviceInfoData, sizeof(DeviceInfoData));
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
for (Index = 0;
SetupDiEnumDeviceInfo(DeviceInfoSet,
Index,
&DeviceInfoData);
Index++)
{
// Query a property on the device. For example, the device description.
if (!SetupDiGetDeviceProperty(DeviceInfoSet,
&DeviceInfoData,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
sizeof(DeviceDesc),
NULL,
0))
{
// The error can be retrieved with GetLastError();
continue;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
continue;
}
}
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
goto Exit;
}
Exit:
if (DeviceInfoSet != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return;
}
In diesem Beispiel wird eine Liste aller vorhandenen Geräte mithilfe von CM_Get_Device_ID_List abgerufen und durchlaufen, um die Gerätebeschreibung jedes Geräts abzurufen.
VOID
GetDevicePropertiesCfgmgr32(
VOID
)
{
CONFIGRET cr = CR_SUCCESS;
PWSTR DeviceList = NULL;
ULONG DeviceListLength = 0;
PWSTR CurrentDevice;
DEVINST Devinst;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
ULONG PropertySize;
DWORD Index = 0;
cr = CM_Get_Device_ID_List_Size(&DeviceListLength,
NULL,
CM_GETIDLIST_FILTER_PRESENT);
if (cr != CR_SUCCESS)
{
goto Exit;
}
DeviceList = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
DeviceListLength * sizeof(WCHAR));
if (DeviceList == NULL) {
goto Exit;
}
cr = CM_Get_Device_ID_List(NULL,
DeviceList,
DeviceListLength,
CM_GETIDLIST_FILTER_PRESENT);
if (cr != CR_SUCCESS)
{
goto Exit;
}
for (CurrentDevice = DeviceList;
*CurrentDevice;
CurrentDevice += wcslen(CurrentDevice) + 1)
{
// If the list of devices also includes non-present devices,
// CM_LOCATE_DEVNODE_PHANTOM should be used in place of
// CM_LOCATE_DEVNODE_NORMAL.
cr = CM_Locate_DevNode(&Devinst,
CurrentDevice,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS)
{
goto Exit;
}
// Query a property on the device. For example, the device description.
PropertySize = sizeof(DeviceDesc);
cr = CM_Get_DevNode_Property(Devinst,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
&PropertySize,
0);
if (cr != CR_SUCCESS)
{
Index++;
continue;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
Index++;
continue;
}
Index++;
}
Exit:
if (DeviceList != NULL)
{
HeapFree(GetProcessHeap(),
0,
DeviceList);
}
return;
}
Rufen Sie eine Liste der Schnittstellen ab, rufen Sie das Gerät ab, das jede Schnittstelle bereitstellt, und rufen Sie eine Eigenschaft vom Gerät ab.
In diesem Beispiel wird eine Liste aller Schnittstellen in der Klasse GUID_DEVINTERFACE_VOLUME mithilfe von SetupDiGetClassDevs abgerufen. Für jede Schnittstelle ruft es das Gerät ab, das die Schnittstelle verfügbar macht, und ruft eine Eigenschaft dieses Geräts ab.
VOID
GetInterfacesAndDevicePropertySetupapi(
VOID
)
{
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
SP_DEVINFO_DATA DeviceInfoData;
DWORD Index;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
DeviceInfoSet = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME,
NULL,
NULL,
DIGCF_DEVICEINTERFACE);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
goto Exit;
}
ZeroMemory(&DeviceInterfaceData, sizeof(DeviceInterfaceData));
DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
for (Index = 0;
SetupDiEnumDeviceInterfaces(DeviceInfoSet,
NULL,
&GUID_DEVINTERFACE_VOLUME,
Index,
&DeviceInterfaceData);
Index++)
{
ZeroMemory(&DeviceInfoData, sizeof(DeviceInfoData));
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if ((!SetupDiGetDeviceInterfaceDetail(DeviceInfoSet,
&DeviceInterfaceData,
NULL,
0,
NULL,
&DeviceInfoData)) &&
(GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
// The error can be retrieved with GetLastError();
goto Exit;
}
// Query a property on the device. For example, the device description.
if (!SetupDiGetDeviceProperty(DeviceInfoSet,
&DeviceInfoData,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
sizeof(DeviceDesc),
NULL,
0))
{
// The error can be retrieved with GetLastError();
goto Exit;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
goto Exit;
}
}
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
goto Exit;
}
Exit:
if (DeviceInfoSet != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return;
}
In diesem Beispiel wird mithilfe von CM_Get_Device_Interface_List eine Liste aller Schnittstellen in der Klasse GUID_DEVINTERFACE_VOLUME abgerufen. Für jede Schnittstelle ruft es das Gerät ab, das die Schnittstelle verfügbar macht, und ruft eine Eigenschaft dieses Geräts ab.
VOID
GetInterfacesAndDevicePropertyCfgmgr32(
VOID
)
{
CONFIGRET cr = CR_SUCCESS;
PWSTR DeviceInterfaceList = NULL;
ULONG DeviceInterfaceListLength = 0;
PWSTR CurrentInterface;
WCHAR CurrentDevice[MAX_DEVICE_ID_LEN];
DEVINST Devinst;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
ULONG PropertySize;
DWORD Index = 0;
do {
cr = CM_Get_Device_Interface_List_Size(&DeviceInterfaceListLength,
(LPGUID)&GUID_DEVINTERFACE_VOLUME,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_ALL_DEVICES);
if (cr != CR_SUCCESS)
{
break;
}
if (DeviceInterfaceList != NULL) {
HeapFree(GetProcessHeap(),
0,
DeviceInterfaceList);
}
DeviceInterfaceList = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
DeviceInterfaceListLength * sizeof(WCHAR));
if (DeviceInterfaceList == NULL)
{
cr = CR_OUT_OF_MEMORY;
break;
}
cr = CM_Get_Device_Interface_List((LPGUID)&GUID_DEVINTERFACE_VOLUME,
NULL,
DeviceInterfaceList,
DeviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_ALL_DEVICES);
} while (cr == CR_BUFFER_SMALL);
if (cr != CR_SUCCESS)
{
goto Exit;
}
for (CurrentInterface = DeviceInterfaceList;
*CurrentInterface;
CurrentInterface += wcslen(CurrentInterface) + 1)
{
PropertySize = sizeof(CurrentDevice);
cr = CM_Get_Device_Interface_Property(CurrentInterface,
&DEVPKEY_Device_InstanceId,
&PropertyType,
(PBYTE)CurrentDevice,
&PropertySize,
0);
if (cr != CR_SUCCESS)
{
goto Exit;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
goto Exit;
}
// Since the list of interfaces includes all interfaces, enabled or not, the
// device that exposed that interface may currently be non-present, so
// CM_LOCATE_DEVNODE_PHANTOM should be used.
cr = CM_Locate_DevNode(&Devinst,
CurrentDevice,
CM_LOCATE_DEVNODE_PHANTOM);
if (cr != CR_SUCCESS)
{
goto Exit;
}
// Query a property on the device. For example, the device description.
PropertySize = sizeof(DeviceDesc);
cr = CM_Get_DevNode_Property(Devinst,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
&PropertySize,
0);
if (cr != CR_SUCCESS)
{
goto Exit;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
goto Exit;
}
Index++;
}
Exit:
if (DeviceInterfaceList != NULL)
{
HeapFree(GetProcessHeap(),
0,
DeviceInterfaceList);
}
return;
}
Abrufen einer Eigenschaft von einem bestimmten Gerät
In diesem Beispiel wird ein Geräteinstanzpfad für ein bestimmtes Gerät verwendet und eine Eigenschaft mithilfe von SetupDiGetDeviceProperty abgerufen.
VOID
GetDevicePropertySpecificDeviceSetupapi(
VOID
)
{
HDEVINFO DeviceInfoSet = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
if (DeviceInfoSet == INVALID_HANDLE_VALUE)
{
goto Exit;
}
ZeroMemory(&DeviceInfoData, sizeof(DeviceInfoData));
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
if (!SetupDiOpenDeviceInfo(DeviceInfoSet,
MY_DEVICE,
NULL,
0,
&DeviceInfoData))
{
// The error can be retrieved with GetLastError();
goto Exit;
}
// Query a property on the device. For example, the device description.
if (!SetupDiGetDeviceProperty(DeviceInfoSet,
&DeviceInfoData,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
sizeof(DeviceDesc),
NULL,
0)) {
// The error can be retrieved with GetLastError();
goto Exit;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
goto Exit;
}
Exit:
if (DeviceInfoSet != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
}
return;
}
In diesem Beispiel wird ein Geräteinstanzpfad für ein bestimmtes Gerät verwendet und mithilfe von CM_Get_DevNode_Property eine Eigenschaft daraus abgerufen.
void
GetDevicePropertySpecificDeviceCfgmgr32(
VOID
)
{
CONFIGRET cr = CR_SUCCESS;
DEVINST Devinst;
WCHAR DeviceDesc[2048];
DEVPROPTYPE PropertyType;
ULONG PropertySize;
// If MY_DEVICE could be a non-present device, CM_LOCATE_DEVNODE_PHANTOM
// should be used in place of CM_LOCATE_DEVNODE_NORMAL.
cr = CM_Locate_DevNode(&Devinst,
MY_DEVICE,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS)
{
goto Exit;
}
// Query a property on the device. For example, the device description.
PropertySize = sizeof(DeviceDesc);
cr = CM_Get_DevNode_Property(Devinst,
&DEVPKEY_Device_DeviceDesc,
&PropertyType,
(PBYTE)DeviceDesc,
&PropertySize,
0);
if (cr != CR_SUCCESS)
{
goto Exit;
}
if (PropertyType != DEVPROP_TYPE_STRING)
{
goto Exit;
}
Exit:
return;
}
Gerät deaktivieren
In diesem Beispiel wird gezeigt, wie Sie ein Gerät mit CfgMgr32 deaktivieren. Dazu verwenden Sie SetupDiCallClassInstaller mit InstallFunction von DIF_PROPERTYCHANGE, wobei DICS_DISABLE angegeben wird.
Hinweis Standardmäßig führt das Aufrufen von SetupDiCallClassInstaller dazu, dass das Gerät über Neustarts deaktiviert bleibt. Um das Gerät beim Aufrufen von CM_Disable_DevNode über Neustarts zu deaktivieren, müssen Sie das CM_DISABLE_PERSIST Flag angeben.
cr = CM_Locate_DevNode(&devinst,
(DEVINSTID_W)DeviceInstanceId,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS) {
goto Exit;
}
cr = CM_Disable_DevNode(devinst, 0);
if (cr != CR_SUCCESS) {
goto Exit;
}
Gerät aktivieren
In diesem Beispiel wird gezeigt, wie Sie ein Gerät mit CfgMgr32 aktivieren. Dazu verwenden Sie SetupDiCallClassInstaller mit InstallFunction von DIF_PROPERTYCHANGE und geben DICS_ENABLE an.
cr = CM_Locate_DevNode(&devinst,
(DEVINSTID_W)DeviceInstanceId,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS) {
goto Exit;
}
cr = CM_Enable_DevNode(devinst, 0);
if (cr != CR_SUCCESS) {
goto Exit;
}
Neustarten eines Geräts
In diesem Beispiel wird gezeigt, wie Sie ein Gerät mit CfgMgr32 neu starten. Dazu verwenden Sie SetupDiCallClassInstaller mit InstallFunction von DIF_PROPERTYCHANGE und geben DICS_PROPCHANGE an.
cr = CM_Locate_DevNode(&devinst,
(DEVINSTID_W)DeviceInstanceId,
CM_LOCATE_DEVNODE_NORMAL);
if (cr != CR_SUCCESS) {
goto Exit;
}
cr = CM_Query_And_Remove_SubTree(devinst,
NULL,
NULL,
0,
CM_REMOVE_NO_RESTART);
if (cr != CR_SUCCESS) {
goto Exit;
}
cr = CM_Setup_DevNode(devinst,
CM_SETUP_DEVNODE_READY);
if (cr != CR_SUCCESS) {
goto Exit;
}