How to check USB is enable or not in c++ programing.
Plz give me some hints or sample code or links

Dani AI

Generated

The original question from is ambiguous: "check USB is enable or not" can mean at least three different things — (A) whether the OS/USB controllers have any USB devices present, (B) whether a particular USB device (by VID/PID) is attached, or (C) whether USB mass-storage access has been administratively disabled. 's suggestion to search is reasonable, and 's sample demonstrates the common technique of walking the Windows device tree to detect a particular CDC (virtual COM) device; that approach is valid when the goal is (B).

For a quick inventory or script-friendly check, WMI exposes USB objects (for example Win32_USBHub / related associations). For robust C/C++ work that needs a device path or handle, enumerate Windows device interfaces via the SetupAPI device-installation functions and read the device instance identifier strings (those contain the familiar "USB\VID_xxxx&PID_yyyy" patterns). These two approaches cover general presence and enumeration scenarios. (learn.microsoft.com)

When the target is a single VID/PID and attachment/removal needs to be tracked at runtime, subscribe to device-interface notifications and handle WM_DEVICECHANGE (DBT_DEVICEARRIVAL / DBT_DEVICEREMOVECOMPLETE) or use RegisterDeviceNotification from a service/window to receive interface events. Match the incoming interface against the USB device-interface class (GUID_DEVINTERFACE_USB_DEVICE) or the driver-supplied GUID for the device class. Typical workflow: enumerate interfaces, match VID/PID, get the device path and open a handle if needed. (learn.microsoft.com)

If the intent is to detect whether USB mass-storage has been disabled by policy, the usual Windows control point is the UsbStor service registry key at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UsbStor — setting the Start value to 4 prevents USB storage from working; Group Policy can enforce similar restrictions. Reading or changing that key requires appropriate privileges and a reboot may be required for changes to take effect. (learn.microsoft.com)

Cautions: BIOS/UEFI-level USB disablement may not be visible to the OS in user-friendly terms; service/registry checks require admin rights; device enumeration shows what the OS has loaded (not necessarily power/BIOS state). Choose the approach above that matches which of (A),(B),(C) was intended.

Recommended Answers

All 2 Replies

Try Googleing it out. Or try on WIKIing it out then you will get a good idea abt it.

some times ago I wrote following code to find if present precise device.
In my case that was USB device of CDC class (or simply virtual com port).
But the principle of enumerating devices the same in every case.

// detects device presence on predefined hardware-node
// Params:
// LPSTR pszPortName[OUT]  device name which can be used to open communication device (valid only if device detected)))
// Return value:
// TRUE- device detected; FALSE-otherwise
//==================================================================================================
BOOL CUsb::DetectDevice(LPSTR pszPortName/*out*/)
{
  USES_CONVERSION;

  DWORD   dwPropertyType;
  char    szPropertyBuffer[100];

  GUID	DeviceGuid;//guid of device class
  SP_DEVINFO_DATA devInfoData;
  // its part of a whole hardware node that looks like  
  //   "USB\\VID_04D8&PID_0005\\5&35BC3A4B&0&2"
  //vendor-id,product-id of our device to search(descriptors inbedded in hardware)
  char szDeviceID[50]="USB\\VID_04D8&PID_0002";
  char szBufferTemp[50];
  char szBufferID[100];//real vendor-id,product-id of our device
  LPOLESTR lpOleStr;//           OLECHAR we don't need with using T2OLE/OLE2T
  char* pStrTemp;

  int i;
  BOOL  bResult;

  if(NULL == pszPortName) return FALSE;//bad param
  *pszPortName = 0;

  //predefined installer-clsid (as defined in INI for our driver) (for ought i know its common 
  //for all communication devices)
  ::CLSIDFromString(T2OLE(("{4D36E978-E325-11CE-BFC1-08002BE10318}")),
                                                                                                    &DeviceGuid);
  //get handler of all device-classes presented on system
  HDEVINFO  hDevInfo = ::SetupDiGetClassDevs(NULL,/* Enumerator*/0,0,
                            DIGCF_PRESENT | DIGCF_ALLCLASSES );//to get only PCI enumerator 
                                                                                // should be REGSTR_KEY_PCIENUM
	
  BOOL bFound=FALSE;

  devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  //enum all devices in a set (our case is on whole system)
  for (i=0; ::SetupDiEnumDeviceInfo(hDevInfo,i,&devInfoData); i++)
  {
     ::StringFromCLSID(devInfoData.ClassGuid,&lpOleStr);
		
     bResult= ::IsEqualCLSID(DeviceGuid,devInfoData.ClassGuid);
     if(!bResult) continue;//this device on system has not our installer-clsid (try next)

     //its device on system with our installer-clsid
     szBufferID[0]=0;
     //get its hardware-node
     bResult= ::SetupDiGetDeviceInstanceId(hDevInfo,&devInfoData,szBufferID,100,NULL);
     if(!bResult) continue;//failed to get string of device-instance hardware-node

     //check if device has our vendor-id,product-id 
     if(strlen(szDeviceID)>=strlen(szBufferID)) continue;//handware-node str must be bigger 
                                                                    // 'cause it contains additionaly id of hub/root
     szBufferTemp[0]=0;
     strncpy(szBufferTemp,szBufferID,strlen(szDeviceID));
     szBufferTemp[strlen(szDeviceID)]=0;
     if(0 != strcmp(szBufferTemp,szDeviceID)) continue;//not our vendor-id,product-id 

     //extract name of com port
    ::SetupDiGetDeviceRegistryProperty(hDevInfo,&devInfoData,SPDRP_FRIENDLYNAME,
                                   &dwPropertyType/*out*/,(BYTE*) &szPropertyBuffer[0]/*out*/,
                                   100/*buffer-size*/,NULL);
     pStrTemp=strchr(szPropertyBuffer,')');
     if(NULL==pStrTemp) continue;
     *pStrTemp=0;
     pStrTemp=strchr(szPropertyBuffer,'(');
     if(NULL==pStrTemp) continue;
     pStrTemp++;
     strcpy(pszPortName,pStrTemp);
     bFound=TRUE; 
     break; 
  }
  ::SetupDiDestroyDeviceInfoList(hDevInfo);//free resources
	
  if(bFound) return TRUE;
  return FALSE;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.