CM_Query_And_Remove_SubTree_Ex function

[Beginning with Windows 8 and Windows Server 2012, this function has been deprecated. Please use CM_Query_And_Remove_SubTree instead.]

The CM_Query_And_Remove_SubTree_Ex function checks whether a device instance and its children can be removed and, if so, it removes them.


WINAPI CM_Query_And_Remove_SubTree_Ex(
  _In_      DEVINST        dnAncestor,
  _Out_opt_ PPNP_VETO_TYPE pVetoType,
  _Out_opt_ LPTSTR         pszVetoName,
  _In_      ULONG          ulNameLength,
  _In_      ULONG          ulFlags,
  _In_opt_  HMACHINE       hMachine


dnAncestor [in]

Caller-supplied device instance handle to the device at the root of the subtree to be removed. This device instance handle is bound to the machine handle supplied by hMachine.

pVetoType [out, optional]

(Optional.) If not NULL, this points to a location that, if the removal request fails, receives a PNP_VETO_TYPE-typed value indicating the reason for the failure.

pszVetoName [out, optional]

(Optional.) If not NULL, this is a caller-supplied pointer to a string buffer that receives a text string. The type of information this string provides is dependent on the value received by pVetoType. For information about these strings, see PNP_VETO_TYPE.

ulNameLength [in]

(Optional.) Caller-supplied value representing the length (number of characters) of the string buffer supplied by pszVetoName. This should be set to MAX_PATH.

ulFlags [in]

A bitwise OR of the caller-supplied flag constants that are described in the Remarks section.

hMachine [in, optional]

Caller-supplied machine handle to which the caller-supplied device instance handle is bound.

Note  Using this function to access remote machines is not supported beginning with Windows 8 and Windows Server 2012, as this functionality has been removed.

Return value

If the operation succeeds, the function returns CR_SUCCESS. Otherwise, it returns one of the CR_-prefixed error codes defined in Cfgmgr32.h.


The purpose of the CM_Query_And_Remove_SubTree_Ex function is to allow an application to prepare a device for safe removal from a remote machine. Use this function to remove devices only if a driver has not set the SurpriseRemovalOK member of DEVICE_CAPABILITIES. If a driver has set SurpriseRemovalOK, the application should call CM_Request_Device_Eject_Ex instead of CM_Query_And_Remove_SubTree_Ex.

CM_Query_And_Remove_SubTree_Ex supports setting the flags parameter ulFlags with one of the following two flags; these flags apply only if Windows or an installer vetoes the removal of a device:


The function allows a user dialog box to be displayed to indicate the reason for the veto. This is the default flag setting.


The function suppresses the display of a user dialog box that indicates the reason for the veto.

Beginning with Windows XP, CM_Query_And_Remove_SubTree_Ex also supports setting the following additional flag; this flag applies only if the function successfully removes the device instance:


If this flag is set, the function configures the device status such that the device cannot be restarted until after the device status is reset. The device status is reset by one of the following:

  • Calling CM_Setup_DevNode for the device and specifying CM_SETUP_DEVNODE_RESET. After the device status is reset in this manner, the device can be restarted by calling CM_Reenumerate_DevNode for the device instance. After resetting the device status, any other operation that enumerates the device will also restart the device, for example, if the Device Manager is used to reenumerate devices.

  • The device is detached and reattached, or the computer is restarted. In this case, the device status will be reset and the device will be restarted.

If this flag is not set, the device status does not have to be reset and the removed device can be restarted by calling CM_Setup_DevNode for the device and specifying CM_SETUP_DEVNODE_READY. Any other operation that reenumerates the device will also restart the device. An example of an operation that reenumerates the device is calling CM_Reenumerate_DevNode for the device, detaching and reattaching the device, or restarting the computer.

Device installation applications that do not require the low-level operation of CM_Query_And_Remove_SubTree_Ex should use the DIF_PROPERTYCHANGE request to disable a device instead of using CM_Query_And_Remove_SubTree_Ex to remove a device. The DIF_PROPERTYCHANGE request can be used to enable, disable, restart, stop, or change the properties of a device.

Callers of this function must have SeLoadDriverPrivilege. (Privileges are described in the Microsoft Windows SDK documentation.)

For information about using device instance handles that are bound to a local or a remote machine, see CM_Get_Child_Ex.

Functionality to access remote machines has been removed in Windows 8 and Windows Server 2012 and later operating systems thus you cannot access remote machines when running on these versions of Windows.


Target platform



Available in Microsoft Windows 2000 and later versions of Windows.


Cfgmgr32.h (include Cfgmgr32.h)



Unicode and ANSI names

CM_Query_And_Remove_SubTree_ExW (Unicode)

See also




Send comments about this topic to Microsoft