Thread: Getting a List of the USB devices listed on the system

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Getting a List of the USB devices listed on the system

    Hi

    I'm trying to get a list of the USB devices attached to my system. I thought using the Raw Input API would se them (GetRawInputDevice and co) but so far Zilch. Can someone point me in the right direction pls?

    Thanks.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Take a look into..

    GetRawInputDeviceList()
    GetRawInputDeviceInfo()

    You can get info from USB Keyboard, Mouse and all other HID's connected.


    Use MSDN it goes into all the detail you need, its pretty easy!
    Last edited by Marc; 04-04-2009 at 09:20 AM.

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I have that's why I posted. I can Pick up my usb Keyboard (only Keyboard attached) and my PS/2 mouse but none of the other USB devices attached to the system.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    I did something myself similar to this.
    I've just cut it into a console app for you, should compile straight up.

    I also commented it so you can see whats going on.


    Code:
    #include <windows.h>
    #include <iostream>
    
    // Namespace
    using namespace std;
    
    // Main
    int main()
    {
    	// Program
    	cout << "USB Device Lister." << endl;
    
    	// Get Number Of Devices
    	UINT nDevices = 0;
    	GetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
    
    	// Got Any?
    	if( nDevices < 1 )
    	{
    		// Exit
    		cout << "ERR: 0 Devices?";
    		cin.get();
    		return 0;
    	}
    	
    	// Allocate Memory For Device List
    	PRAWINPUTDEVICELIST pRawInputDeviceList;
    	pRawInputDeviceList = new RAWINPUTDEVICELIST[ sizeof( RAWINPUTDEVICELIST ) * nDevices ];
    
    	// Got Memory?
    	if( pRawInputDeviceList == NULL )
    	{
    		// Error
    		cout << "ERR: Could not allocate memory for Device List.";
    		cin.get();
    		return 0;
    	}
    	
    	// Fill Device List Buffer
    	int nResult;
    	nResult = GetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
    
    	// Got Device List?
    	if( nResult < 0 )
    	{
    		// Clean Up
    		delete [] pRawInputDeviceList;
    
    		// Error
    		cout << "ERR: Could not get device list.";
    		cin.get();
    		return 0;
    	}
    
    	// Loop Through Device List
    	for( UINT i = 0; i < nDevices; i++ )
    	{
    		// Get Character Count For Device Name
    		UINT nBufferSize = 0;
    		nResult = GetRawInputDeviceInfo( pRawInputDeviceList[i].hDevice, // Device
    										 RIDI_DEVICENAME,				 // Get Device Name
    										 NULL,							 // NO Buff, Want Count!
    										 &nBufferSize );				 // Char Count Here!
    
    		// Got Device Name?
    		if( nResult < 0 )
    		{
    			// Error
    			cout << "ERR: Unable to get Device Name character count.. Moving to next device." << endl << endl;
    
    			// Next
    			continue;
    		}
    
    		// Allocate Memory For Device Name
    		WCHAR* wcDeviceName = new WCHAR[ nBufferSize + 1 ];
    		
    		// Got Memory
    		if( wcDeviceName == NULL )
    		{
    			// Error
    			cout << "ERR: Unable to allocate memory for Device Name.. Moving to next device." << endl << endl;
    
    			// Next
    			continue;
    		}
    
    		// Get Name
    		nResult = GetRawInputDeviceInfo( pRawInputDeviceList[i].hDevice, // Device
    										 RIDI_DEVICENAME,				 // Get Device Name
    										 wcDeviceName,					 // Get Name!
    										 &nBufferSize );				 // Char Count
    
    		// Got Device Name?
    		if( nResult < 0 )
    		{
    			// Error
    			cout << "ERR: Unable to get Device Name.. Moving to next device." << endl << endl;
    
    			// Clean Up
    			delete [] wcDeviceName;
    
    			// Next
    			continue;
    		}
    
    		// Set Device Info & Buffer Size
    		RID_DEVICE_INFO rdiDeviceInfo;
    		rdiDeviceInfo.cbSize = sizeof( RID_DEVICE_INFO );
    		nBufferSize = rdiDeviceInfo.cbSize;
    
    		// Get Device Info
    		nResult = GetRawInputDeviceInfo( pRawInputDeviceList[i].hDevice,
    										 RIDI_DEVICEINFO,
    										 &rdiDeviceInfo,
    										 &nBufferSize );
    
    		// Got All Buffer?
    		if( nResult < 0 )
    		{
    			// Error
    			cout << "ERR: Unable to read Device Info.. Moving to next device." << endl << endl;
    
    			// Next
    			continue;
    		}
    
    		// Mouse
    		if( rdiDeviceInfo.dwType == RIM_TYPEMOUSE )
    		{
    			// Current Device
    			cout << endl << "Displaying device " << i+1 << " information. (MOUSE)" << endl;
    			wcout << L"Device Name: " << wcDeviceName << endl;
    			cout << "Mouse ID: " << rdiDeviceInfo.mouse.dwId << endl;
    			cout << "Mouse buttons: " << rdiDeviceInfo.mouse.dwNumberOfButtons << endl;
    			cout << "Mouse sample rate (Data Points): " << rdiDeviceInfo.mouse.dwSampleRate << endl;
    			if( rdiDeviceInfo.mouse.fHasHorizontalWheel )
    			{
    				cout << "Mouse has horizontal wheel" << endl;
    			}
    			else
    			{
    				cout << "Mouse does not have horizontal wheel" << endl;
    			}
    		}
    
    		// Keyboard
    		else if( rdiDeviceInfo.dwType == RIM_TYPEKEYBOARD )
    		{
    			// Current Device
    			cout << endl << "Displaying device " << i+1 << " information. (KEYBOARD)" << endl;
    			wcout << L"Device Name: " << wcDeviceName << endl;
    			cout << "Keyboard mode: " << rdiDeviceInfo.keyboard.dwKeyboardMode << endl;
    			cout << "Number of function keys: " << rdiDeviceInfo.keyboard.dwNumberOfFunctionKeys << endl;
    			cout << "Number of indicators: " << rdiDeviceInfo.keyboard.dwNumberOfIndicators << endl;
    			cout << "Number of keys total: " << rdiDeviceInfo.keyboard.dwNumberOfKeysTotal << endl;
    			cout << "Type of the keyboard: " << rdiDeviceInfo.keyboard.dwType << endl;
    			cout << "Subtype of the keyboard: " << rdiDeviceInfo.keyboard.dwSubType << endl;
    		}
    
    		// Some HID
    		else // (rdi.dwType == RIM_TYPEHID)
    		{
    			// Current Device
    			cout << endl << "Displaying device " << i+1 << " information. (HID)" << endl;
    			wcout << L"Device Name: " << wcDeviceName << endl;
    			cout << "Vendor Id:" << rdiDeviceInfo.hid.dwVendorId << endl;
    			cout << "Product Id:" << rdiDeviceInfo.hid.dwProductId << endl;
    			cout << "Version No:" << rdiDeviceInfo.hid.dwVersionNumber << endl;
    			cout << "Usage for the device: " << rdiDeviceInfo.hid.usUsage << endl;
    			cout << "Usage Page for the device: " << rdiDeviceInfo.hid.usUsagePage << endl;
    		}
    
    		// Delete Name Memory!
    		delete [] wcDeviceName;
    	}
    
    	// Clean Up - Free Memory
    	delete [] pRawInputDeviceList;
    
    	// Exit
    	cout << endl << "Finnished.";
    	cin.get();
    	return 0;
    }

    I hope you find it useful!

    Marc

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Do you want any USB device or some particular device like USB webcams or USB thumb drives?

  6. #6
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I'm heading towards writing drivers for custom made boards. They'll be connected via USB. But for now I need to be able to pick up the list of USB devices attached to the system. Reason being I'm working on an interface for direct access to these usb devices for my own purposes.
    Marc thx for the info but my code is more or less the same as yours albeit mine's more bloated because I'm doing it in C# V Studio Express 2008.
    I get a list of 6 devices connected to my system terminal devices included. It picks up my USB and PS/2 mice, keyboard and another HID device which could b my docked windows phone or my Microphone where as both should also be listed.
    I would post my code but given it's bloatness... It would just be plain rude to expect someone to read all that.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I have attached my code instead. I had to copy it into a .txt file because .cs aren't allowed.... YET
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by WDT View Post
    But for now I need to be able to pick up the list of USB devices attached to the system.
    It's a Win32 FAQ
    See Google Groups ( Win32 api, MS sample)

  9. #9
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Win32_USBControllerDevice Class. What'd you think? I'll go ahead and experiment with this until I hit the metaphorical wall.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    1
    I wantto get usb devices names that seeing at windows device manager.
    GetRawInputDeviceList is just giving MOUSE,KEYBOARD AND Hid devices names.
    How can i get all usb device names.With c++

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See the well-known MS sample for USB devices list in DDK

  12. #12
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by cmon View Post
    I wantto get usb devices names that seeing at windows device manager.
    GetRawInputDeviceList is just giving MOUSE,KEYBOARD AND Hid devices names.
    How can i get all usb device names.With c++
    Intel example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM