SHARE
TWEET

Untitled

a guest Nov 25th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. boolean ConnectToBoard(HWND hWnd)
  2. {
  3.     bool isConnected = false;
  4.     struct _GUID GUID;
  5.     HANDLE PnPHandle;
  6.     SP_DEVICE_INTERFACE_DATA interface_data;
  7.     PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
  8.     DWORD last_error;
  9.     DWORD RequiredSize;
  10.     char cBuffer[256];
  11.  
  12.  
  13.     HidD_GetHidGuid(&GUID);
  14.     PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
  15.         DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  16.  
  17.  
  18.     if (PnPHandle == INVALID_HANDLE_VALUE)
  19.     {
  20.         wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
  21.         isConnected = false;
  22.     }
  23.  
  24.     for (int i = 0; i < 20; i++)
  25.     {
  26.         interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  27.         SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
  28.  
  29.         last_error = GetLastError();
  30.  
  31.         if (last_error == ERROR_NO_MORE_ITEMS)
  32.         {
  33.             break;
  34.             isConnected = false;
  35.         }
  36.  
  37.         SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
  38.         detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
  39.         detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  40.         BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
  41.             RequiredSize, &RequiredSize, NULL);
  42.  
  43.         if (retCode == FALSE)
  44.         {
  45.             free(detail_data);
  46.             isConnected = false;
  47.             continue;
  48.         }
  49.  
  50.         hFile = CreateFile(detail_data->DevicePath, GENERIC_READ | GENERIC_WRITE,
  51.             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  52.  
  53.         if (INVALID_HANDLE_VALUE == hFile)
  54.         {
  55.             wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
  56.             free(detail_data);
  57.             isConnected = false;
  58.             continue;
  59.         }
  60.  
  61.         //step 6
  62.         HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
  63.  
  64.         //step 7
  65.         free(detail_data);
  66.  
  67.         bool equal = true;
  68.         const char board[] = "Keil MCB2140 HID";
  69.         for (int i = 0; i < strlen(cBuffer); i++)
  70.         {
  71.             if (cBuffer[2 * i] != board[i])
  72.             {
  73.                 equal = false;
  74.                 break;
  75.             }
  76.         }
  77.  
  78.         if (equal)
  79.         {
  80.             isConnected = true;
  81.         }
  82.  
  83.         //CloseHandle(hFile);
  84.     }
  85.  
  86.     SetupDiDestroyDeviceInfoList(PnPHandle);
  87.     return isConnected;
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top