That will work to get rid of the virtual devices, but it won't get rid of disconnected devices.
Is CreateFile supposed to return INVALID_HANDLE_VALUE for disconnected devices? Because it's not working that way for me. For a Bluetooth keyboard (it's actually a keyboard app on my phone). When I shutdown the app, the phone's bluetooth goes off, but the keyboard still shows in Device Manager. It only goes away after a few minutes, but I assume in this case, my application will receive a WM_INPUT_DEVICE_CHANGE message if I register for them.
Also, PS/2 does not have plug and play capability, so if you remove a PS/2 mouse or keyboard, it will probably not be detected by Windows. But if you do this, then do not plug the keyboard or mouse back in while the computer is still running - you might ruin the PS/2 controller; you have to shutdown the computer, then plug in the keyboard /mouse.