views:

34

answers:

1

hello experts....

Any suggstions on getting the interface type for a drive letter ( G: ) ( not using wmi )

Thank you

A: 

You can use GetDriveType to get the basic interface type(ie: removable device, CDROM, RAMDisk) for the drive letter, also see the final comment at the bottom of that page for a little more info on removable devices. Also check out SetupDiGetDeviceRegistryProperty and DeviceIoControl

Her is the best example I can come up with(untested as I don't have the WDK/DDK)

bool IsUSBDevice(const char* szDrivePath, bool& bRemovable)
{
    if(GetDriveType(szDrivePath) != DRIVE_REMOVABLE)
        return false;

    HANDLE hDevice = CreateFile(szDrivePath,0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
    if(hDevice == INVALID_HANDLE_VALUE)
        return false;

    STORAGE_PROPERTY_QUERY pQuery = {0};
    pQuery.PropertyId = StorageDeviceProperty;
    pQuery.QueryType = PropertyStandardQuery;

    STORAGE_DEVICE_DESCRIPTOR pDeviceDesc = {0};
    pDeviceDesc.Size = sizeof(pDeviceDesc);
    DWORD dwWritten = 0;
    if(DeviceIoControl(hDevice,IOCTL_STORAGE_QUERY_PROPERTY,&pQuery,sizeof(STORAGE_PROPERTY_QUERY),pDeviceDesc,sizeof(pDeviceDesc),&dwWritten,NULL))
    {
        CloseHandle(hDevice);
        return ((bRemovable = pDeviceDesc.RemovableMedia) && pDeviceDesc.BusType == BusTypeUsb);
    }
    else
        CloseHandle(hDevice);

    return false; 
}
Necrolis
hi...i need to filter the usb device interface type
barbgal
see the comments at the bottom and/or SetupDiGetDeviceRegistryProperty for the usb drives, DeviceIoControl will also give some info on removal policy etc
Necrolis
can you pls show me an example with drive letter
barbgal
A USB device may have 0, 1 or more drive letters so that's far from trivial.
MSalters
an example is quite a length process, especially, if one doesn't have the WDK(which is needed to access a few of the query function, unless you go the ultra long way with GetProcAddress, but one still needs the defines..), anyways first post is updated
Necrolis