Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: detecting USB device

184 views
Skip to first unread message

Maxim S. Shatskih

unread,
Feb 8, 2006, 9:15:05 PM2/8/06
to
Look at SetupDiGetClassDevs and around.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
ma...@storagecraft.com
http://www.storagecraft.com

"icodeurs2" <icod...@discussions.microsoft.com> wrote in message
news:C696A348-0B7B-4B65...@microsoft.com...
> I have a need using C#.NET (Any .NET sample would help) to determine if a
> particular USB device is plugged in. The same way Windows XP/2000 detects the
> new device and displays the balloon indicating the new hardware has been
> found. Any samples or direction to the API would be greatly appreciated.

icodeurs2

unread,
Feb 8, 2006, 5:46:23 PM2/8/06
to

Stefan de Buhr

unread,
Feb 12, 2006, 4:16:26 PM2/12/06
to
Moin,

a few moons ago I wrote a few lines to programmatically remove a USB device.
Maybe you can convert some of it for your needs.
You can also have a look at http://pinvoke.net to see how to call the
SetupAPI functions
from .NET.

Bis gleich ...
Stefan


------------------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <setupapi.h>
#include <devguid.h>

/*
* -- Definitions:
*/
const char* TelkoID = "USB\\VID_090A&PID_1540";


/*
* The buffer receives the hardware ID.
*/
#define ID_BUFFER_SIZE 512
BYTE gBuffer[ID_BUFFER_SIZE];


/*
* -- Local protoypes:
*/
BOOL RemoveDevice(const char*);

/*
*
*/
int main(void)
{
BOOL status;


status = RemoveDevice(TelkoID);


if (status == FALSE)
{
printf("Failed to remove Telko\n");
}


return 0;
}


/*
* -- RemoveDevice
*
* This function removes the device specified by the supplied ID.
* Returns TRUE if the device was removed, otherwise FALSE.
*
* First we collect all currently present USB devices in a device info set.
* Then we enumerate those devices and get their hardware IDs. Then we check
* if "our" device is present. If so then we remove the device.
*/
BOOL RemoveDevice(const char* DeviceID)
{
BOOL deviceRemoved = FALSE;
HDEVINFO devInfoSet = INVALID_HANDLE_VALUE;
BOOL status = FALSE;


/*
* Retrieve a device info set with currently present USB devices:
*/
devInfoSet =
SetupDiGetClassDevs(&GUID_DEVCLASS_USB,NULL,INVALID_HANDLE_VALUE,DIGCF_PRESENT);


if (devInfoSet != INVALID_HANDLE_VALUE)
{
SP_DEVINFO_DATA devInfoData;
DWORD requiredSize;
DWORD index = 0;


/*
* Enumerate the USB devices:
*/
do
{
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
status = SetupDiEnumDeviceInfo(devInfoSet,index,&devInfoData);
if (status == 0) break;


status =
SetupDiGetDeviceRegistryProperty(devInfoSet,&devInfoData,SPDRP_HARDWAREID,NULL,gBuffer,ID_BUFFER_SIZE,&requiredSize);
if (status == 0) break;


/*
* Windows 98 returns the hardware ID in upper case.
* Windows XP returns it in mixed case.
* So we convert the ID to upper case to have a defined
* behaviour so that we can compare with the upper case
* hardware ID:
*/
_strupr(gBuffer);


if (strncmp(DeviceID,gBuffer,strlen(DeviceID)) == 0)
{
/* We found our device, so remove it: */
status = SetupDiRemoveDevice(devInfoSet,&devInfoData);
if (status == 0)
{
deviceRemoved = TRUE;
}


/* We are done removing the device. */
break;
}


index += 1;


} while (status != 0);


/* Clean up the device info set: */
SetupDiDestroyDeviceInfoList(devInfoSet);
}


return deviceRemoved;
}


icodeurs2

unread,
Feb 28, 2006, 9:08:26 PM2/28/06
to
Thanks to both of you.

Dennis

0 new messages