It’s been posted before, but here’s how you’d enumerate all the devices
using the SetupDi routines. I’ll use CDROM guids, which use identical
GUIDS for both the class and interface. The code below is pseudo-code,
and requires significant alteration for a shipping product, but should
be sufficient for you to find everything you need on MSDN.
========================================================================
// SetupDiGetClassDevs – gets the set of all devices of a specified
class,
// such as CdRomClassGuid (ntddstor.h).
GUID cdromGuid = CdRomClassGuid;
deviceInfoHandle =
SetupDiGetClassDevs(&cdromGuid, NULL, NULL,
(DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));
for (int i = 0; i < 0x40000; i++)
{
SP_DEVICE_INTERFACE_DATA diData;
ULONG requiredSize = 0;
DWORD error;
RtlZeroMemory(&diData, sizeof(SP_DEVICE_INTERFACE_DATA));
diData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
// SetupDiEnumDeviceInterfaces() – enumerates devices with a
specific
// interface GUID, such as CdRomClassGuid (ntddstor.h).
// request the Nth recorder – nonzero return on success
if (!SetupDiEnumDeviceInterfaces(deviceInfoHandle, 0,
&cdromGuid,
currentDeviceIndex, &diData))
{
DWORD error = GetLastError();
if (error == ERROR_NO_MORE_ITEMS)
{
break;
}
}
// SetupDiGetDeviceInterfaceDetail() – using the
DeviceInterfaceDetail
// returned above, call this twice (first to determine size
required,
// then to get info) to get the PNP path to the device.
// should never succeed with NULL buffer…
if (SetupDiGetDeviceInterfaceDetails(deviceInfoHandle, &diData,
NULL,
0, &requiredSize, NULL))
{
// throw Exception
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
// throw Exception – the error must always be
INSUFFICIENT_BUFFER
}
if (requiredSize == 0)
{
// throw Exception – size should never, ever be zero!
}
// allocate diDetails. Add sizeof(TCHAR) to ensure NULL
termination.
diDetails = Alloc(requiredSize+sizeof(WCHAR));
if (!SetupDiGetDeviceInterfaceDetails(deviceInfoHandle, &diData,
diDetails, requiredSize,
&updatedSize, NULL))
{
// thow Exception - should succeed this time around
}
// diDetails->DevicePath is a NULL-terminated unicode string
// save (or use) that string for opening the device directly
}
========
How to enumerate all devices of a class (such as CDROM) that have a
specific interface (such as IMAPI):
As above, but modify the interface GUID in SetupDiEnumDeviceInterfaces()
to use the IMAPI-specific GUID.
========================================================================
This posting is provided “AS IS” with no warranties, and confers no
rights. Use of included script samples are subject to the terms
specified at
http://www.microsoft.com/info/cpyright.htm
-----Original Message-----
From: Kedar [mailto:xxxxx@hotmail.com]
Sent: Friday, April 09, 2004 8:55 PM
Subject: Getting Device object of the CD ROM disk driver
Hi All,
What is the way to get the device object of the CD ROM disk driver, I
need
to get that so that I can start filtering that.
Can I use ZwCreateFile(‘’\DosDevice\CDROM,…), to get the handle and
from
the handle can I ge the device object of the CDROM driver.
Any information is helpful.
Thanks,
Kedar.