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

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.