Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boolean ConnectToBoard(HWND hWnd)
- {
- bool isConnected = false;
- struct _GUID GUID;
- HANDLE PnPHandle;
- SP_DEVICE_INTERFACE_DATA interface_data;
- PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data;
- DWORD last_error;
- DWORD RequiredSize;
- char cBuffer[256];
- HidD_GetHidGuid(&GUID);
- PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL,
- DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
- if (PnPHandle == INVALID_HANDLE_VALUE)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Comm cannot be established!\n");
- isConnected = false;
- }
- for (int i = 0; i < 20; i++)
- {
- interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, &interface_data);
- last_error = GetLastError();
- if (last_error == ERROR_NO_MORE_ITEMS)
- {
- break;
- isConnected = false;
- }
- SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, NULL, 0, &RequiredSize, NULL);
- detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(RequiredSize);
- detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- BOOLEAN retCode = SetupDiGetDeviceInterfaceDetail(PnPHandle, &interface_data, detail_data,
- RequiredSize, &RequiredSize, NULL);
- if (retCode == FALSE)
- {
- free(detail_data);
- isConnected = false;
- continue;
- }
- hFile = CreateFile(detail_data->DevicePath, GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
- if (INVALID_HANDLE_VALUE == hFile)
- {
- wsprintf(szBuffer[cLine++], "[ERROR] Error creating file\n");
- free(detail_data);
- isConnected = false;
- continue;
- }
- //step 6
- HidD_GetProductString(hFile, cBuffer, sizeof(cBuffer));
- //step 7
- free(detail_data);
- bool equal = true;
- const char board[] = "Keil MCB2140 HID";
- for (int i = 0; i < strlen(cBuffer); i++)
- {
- if (cBuffer[2 * i] != board[i])
- {
- equal = false;
- break;
- }
- }
- if (equal)
- {
- isConnected = true;
- }
- //CloseHandle(hFile);
- }
- SetupDiDestroyDeviceInfoList(PnPHandle);
- return isConnected;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement