Retrieve serial number from USB memory (Windows environment c++) -
i need retrieve serial number usb memory, namely hard disk's serial number manufacturer assigns. reason cannot use getvolumeinformation() suggested in other threads. need have "unique" number
i kindly ask if can share example in c++ , windows environment (visual c++)
thanks!
you can check out article:- http://oroboro.com/usb-serial-number/
#include <winioctl.h> #include <api/usbioctl.h> #include <setupapi.h> define_guid( guid_devinterface_usb_disk, 0x53f56307l, 0xb6bf, 0x11d0, 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b ); void getdeviceinfo( int vol ) { usbdeviceinfo info; // device handle char devicepath[7] = "\\\\.\\@:"; devicepath[4] = (char)( vol + 'a' ); handle devicehandle = createfile( devicepath, 0, file_share_read | file_share_write, null, open_existing, 0, null ); if ( devicehandle == invalid_handle_value ) return; // device number dword volumedevicenumber = getdevicenumber( devicehandle ); closehandle( devicehandle ); // device interface info set handle // devices attached system hdevinfo hdevinfo = setupdigetclassdevs( &guid_devinterface_usb_disk, null, null, digcf_present | digcf_deviceinterface ); if ( hdevinfo == invalid_handle_value ) return; // context structure device interface // of device information set. byte buf[1024]; psp_device_interface_detail_data pspdidd = (psp_device_interface_detail_data)buf; sp_device_interface_data spdid; sp_devinfo_data spdd; spdid.cbsize = sizeof( spdid ); dword dwindex = 0; while ( true ) { if ( ! setupdienumdeviceinterfaces( hdevinfo, null, &guid_devinterface_usb_disk, dwindex, &spdid )) break; dword dwsize = 0; setupdigetdeviceinterfacedetail( hdevinfo, &spdid, null, 0, &dwsize, null ); if (( dwsize != 0 ) && ( dwsize <= sizeof( buf ))) { pspdidd->cbsize = sizeof( *pspdidd ); // 5 bytes! zeromemory((pvoid)&spdd, sizeof(spdd)); spdd.cbsize = sizeof(spdd); long res = setupdigetdeviceinterfacedetail( hdevinfo, &spdid, pspdidd, dwsize, &dwsize, &spdd ); if ( res ) { handle hdrive = createfile( pspdidd->devicepath,0, file_share_read | file_share_write, null, open_existing, 0, null ); if ( hdrive != invalid_handle_value ) { dword usbdevicenumber = getdevicenumber( hdrive ); if ( usbdevicenumber == volumedevicenumber ) { fprintf( "%s", pspdidd->devicepath ); } } closehandle( hdrive ); } } dwindex++; } setupdidestroydeviceinfolist(hdevinfo); return; }
you device number calling deviceiocontrol()
handle device:
dword getdevicenumber( handle devicehandle ) { storage_device_number sdn; sdn.devicenumber = -1; dword dwbytesreturned = 0; if ( !deviceiocontrol( devicehandle, ioctl_storage_get_device_number, null, 0, &sdn, sizeof( sdn ), &dwbytesreturned, null ) ) { // handle error - bad handle. return u32_max; } return sdn.devicenumber; }
next here method recognize if volume removable media (e.g. usb or firewire disk):
bool isremovablemedia( s32 vol ) { char rootpath[5] = "@:\\"; rootpath[0] = (char)( vol + 'a' ); char szdosdevicename[max_path]; char dosdevicepath[3] = "@:"; // drive type uint drivetype = getdrivetype( rootpath ); if ( drivetype != drive_removable ) return false; dosdevicepath[0] = (char)( vol + 'a' ); querydosdevice( dosdevicepath, szdosdevicename, max_path ); if ( strstr( szdosdevicename,"\\floppy") != null ) { // floppy return false; } return true; }
Comments
Post a Comment