text: #include <wbemidl.h>
  #include <oleauto.h>
  #include <wmsstd.h>
  
  /* wcsstr()
  
  Returns a pointer to the first occurrence of a search string in a string
  Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str.
  
  wchar_t *wcsstr(
  wchar_t *str,                // Null-terminated string to search
  const wchar_t *strSearch     // Null-terminated string to search for
  ); // C++ only
  
  const wchar_t *wcsstr(
  const wchar_t *str,
  const wchar_t *strSearch
  ); // C++ only
  */
  
  /* swscanf()
  
  swscanf(strVid, L"VID_%4X", &dwVid)
  
  VID_%4X
  
  % = format specifier = start of format string
  
  4 = width = maximum number of characters to be read in the current reading operation
  
  X = hex digit ?
  
  */
  
  /*
  MAKELONG(a, b)      ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
  
  MAKELONG(a, b)      
  
  (LONG) 
  	(  
  		( 
  			(WORD) 
  				(
  					( 
  						(DWORD_PTR) a 
  					) 
  					& 0xffff
  				)
  		) 
  		| 
  		( 
  			(DWORD) 
  				(
  					(WORD) 
  						(
  							(
  								(DWORD_PTR) b 
  							) 
  							& 0xffff
  						)
  				)
  		) 
  		<< 16  
  	)
  */
  
  //-----------------------------------------------------------------------------
  // Enum each PNP device using WMI and check each device ID to see if it contains 
  // "IG_" (ex. "VID_045E&PID_028E&IG_00").  If it does, then it's an XInput device
  // Unfortunately this information can not be found by just using DirectInput 
  //-----------------------------------------------------------------------------
  BOOL IsXInputDevice(const GUID* pGuidProductFromDirectInput)
  {
  	IWbemLocator*           pIWbemLocator = NULL;
  	IEnumWbemClassObject*   pEnumDevices = NULL;
  	IWbemClassObject*       pDevices[20] = { 0 };
  	IWbemServices*          pIWbemServices = NULL;
  	BSTR                    bstrNamespace = NULL;
  	BSTR                    bstrDeviceID = NULL;
  	BSTR                    bstrClassName = NULL;
  	DWORD                   uReturned = 0;
  	bool                    bIsXinputDevice = false;
  	UINT                    iDevice = 0;
  	VARIANT                 var;
  	HRESULT                 hr;
  
  	// CoInit if needed
  	hr = CoInitialize(NULL);
  	bool bCleanupCOM = SUCCEEDED(hr);
  
  	// Create WMI
  	hr = CoCreateInstance(__uuidof(WbemLocator),
  		NULL,
  		CLSCTX_INPROC_SERVER,
  		__uuidof(IWbemLocator),
  		(LPVOID*)&pIWbemLocator);
  	if (FAILED(hr) || pIWbemLocator == NULL)
  		goto LCleanup;
  
  	bstrNamespace = SysAllocString(L"\\\\.\\root\\cimv2");		if (bstrNamespace == NULL) goto LCleanup;
  	bstrClassName = SysAllocString(L"Win32_PNPEntity");		if (bstrClassName == NULL) goto LCleanup;
  	bstrDeviceID = SysAllocString(L"DeviceID");				if (bstrDeviceID == NULL)  goto LCleanup;
  
  	// Connect to WMI 
  	hr = pIWbemLocator->ConnectServer(bstrNamespace, NULL, NULL, 0L,
  		0L, NULL, NULL, &pIWbemServices);
  	if (FAILED(hr) || pIWbemServices == NULL)
  		goto LCleanup;
  
  	// Switch security level to IMPERSONATE. 
  	CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  		RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
  
  	hr = pIWbemServices->CreateInstanceEnum(bstrClassName, 0, NULL, &pEnumDevices);
  	if (FAILED(hr) || pEnumDevices == NULL)
  		goto LCleanup;
  
  	// Loop over all devices
  	for (;; )
  	{
  		// Get 20 at a time
  		hr = pEnumDevices->Next(10000, 20, pDevices, &uReturned);
  		if (FAILED(hr))
  			goto LCleanup;
  		if (uReturned == 0)
  			break;
  
  		for (iDevice = 0; iDevice<uReturned; iDevice++)
  		{
  			// For each device, get its device ID
  			hr = pDevices[iDevice]->Get(bstrDeviceID, 0L, &var, NULL, NULL); // retrieve DeviceID property from device-info object into var variable
  			if (SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != NULL)
  			{
  				// Check if the device ID contains "IG_".  If it does, then it's an XInput device
  				// This information can not be found from DirectInput 
  				if (wcsstr(var.bstrVal, L"IG_"))
  				{
  					// If it does, then get the VID/PID from var.bstrVal
  					DWORD dwPid = 0, dwVid = 0;
  					WCHAR* strVid = wcsstr(var.bstrVal, L"VID_");
  					if (strVid && swscanf(strVid, L"VID_%4X", &dwVid) != 1) 
  						// swscanf
  						//		Reads data from the wide string "ws" and stores them according to parameter "format" into 
  						//		the locations given by the additional argument(s) '&dwVid'
  						//		returns the number of items in the argument list successfully filled
  						dwVid = 0;
  					WCHAR* strPid = wcsstr(var.bstrVal, L"PID_");
  					if (strPid && swscanf(strPid, L"PID_%4X", &dwPid) != 1)
  						dwPid = 0;
  
  					// Compare the VID/PID to the DInput device
  					DWORD dwVidPid = MAKELONG(dwVid, dwPid);
  						// dwVidPid is ulong. (DWORD = 32 bit int)
  						// dwVid and dwPid are ulong
  					if (dwVidPid == pGuidProductFromDirectInput->Data1) // get first 8 hex digits from GUID. as ulong
  						// A GUID is a 128-bit value consisting of one group of 
  						//	8 hexadecimal digits, 
  						//	followed by three groups of 4 hexadecimal digits each, 
  						//	followed by one group of 12 hexadecimal digits. 
  						//
  						//	Data1 - Specifies the first 8 hexadecimal digits of the GUID
  						//
  						// https://docs.microsoft.com/en-us/windows/win32/api/guiddef/ns-guiddef-guid
  					{
  						bIsXinputDevice = true;
  						goto LCleanup;
  					}
  				}
  			}
  			SAFE_RELEASE(pDevices[iDevice]);
  		}
  	}
  
  LCleanup:
  	if (bstrNamespace)
  		SysFreeString(bstrNamespace);
  	if (bstrDeviceID)
  		SysFreeString(bstrDeviceID);
  	if (bstrClassName)
  		SysFreeString(bstrClassName);
  	for (iDevice = 0; iDevice<20; iDevice++)
  		SAFE_RELEASE(pDevices[iDevice]);
  	SAFE_RELEASE(pEnumDevices);
  	SAFE_RELEASE(pIWbemLocator);
  	SAFE_RELEASE(pIWbemServices);
  
  	if (bCleanupCOM)
  		CoUninitialize();
  
  	return bIsXinputDevice;
  }
  
  
  //-----------------------------------------------------------------------------
  // Name: EnumJoysticksCallback()
  // Desc: Called once for each enumerated joystick. If we find one, create a
  //       device interface on it so we can play with it.
  //-----------------------------------------------------------------------------
  BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance,
  	VOID* pContext)
  {
  	HRESULT hr;
  
  	if (IsXInputDevice(&pdidInstance->guidProduct))
  		return DIENUM_CONTINUE;
  
  	// Device is verified not XInput, so add it to the list of DInput devices
  
  	return DIENUM_CONTINUE;
  }