Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <winusb.h>
- #include <cfgmgr32.h>
- #include <tchar.h>
- #include <strsafe.h>
- #include <stdio.h>
- // Define the correct device interface GUID
- DEFINE_GUID(GUID_DEVINTERFACE_WinUSBApplication,
- 0x99DA2DEC, 0xC919, 0x4F9D, 0x93, 0x84, 0xDA, 0xA7, 0xEB, 0xBD, 0x40, 0xEC);
- typedef struct _DEVICE_DATA {
- WINUSB_INTERFACE_HANDLE WinusbHandle;
- HANDLE DeviceHandle;
- TCHAR DevicePath[MAX_PATH];
- UCHAR IsochronousInPipe;
- } DEVICE_DATA, *PDEVICE_DATA;
- HRESULT OpenDevice(PDEVICE_DATA DeviceData, PBOOL FailureDeviceNotFound);
- VOID CloseDevice(PDEVICE_DATA DeviceData);
- BOOL ReadFromIsochronousEndpoint(PDEVICE_DATA DeviceData, UCHAR* buffer, ULONG bufferLength);
- HRESULT RetrieveDevicePath(LPTSTR DevicePath, ULONG BufLen, PBOOL FailureDeviceNotFound);
- BOOL QueryDeviceEndpoints(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR* IsochronousInPipe);
- HRESULT OpenDevice(PDEVICE_DATA DeviceData, PBOOL FailureDeviceNotFound)
- {
- HRESULT hr = RetrieveDevicePath(DeviceData->DevicePath, sizeof(DeviceData->DevicePath), FailureDeviceNotFound);
- if (FAILED(hr)) {
- _tprintf(_T("Failed to retrieve device path. HRESULT: 0x%x\n"), hr);
- return hr;
- }
- _tprintf(_T("Device Path: %s\n"), DeviceData->DevicePath);
- DeviceData->DeviceHandle = CreateFile(DeviceData->DevicePath, GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
- if (INVALID_HANDLE_VALUE == DeviceData->DeviceHandle) {
- _tprintf(_T("Failed to open device. Error: %d\n"), GetLastError());
- return HRESULT_FROM_WIN32(GetLastError());
- }
- if (!WinUsb_Initialize(DeviceData->DeviceHandle, &DeviceData->WinusbHandle)) {
- hr = HRESULT_FROM_WIN32(GetLastError());
- CloseHandle(DeviceData->DeviceHandle);
- _tprintf(_T("WinUsb_Initialize failed. Error: %d\n"), GetLastError());
- return hr;
- }
- return S_OK;
- }
- VOID CloseDevice(PDEVICE_DATA DeviceData)
- {
- if (DeviceData->WinusbHandle) {
- WinUsb_Free(DeviceData->WinusbHandle);
- DeviceData->WinusbHandle = NULL;
- }
- if (DeviceData->DeviceHandle) {
- CloseHandle(DeviceData->DeviceHandle);
- DeviceData->DeviceHandle = NULL;
- }
- }
- BOOL ReadFromIsochronousEndpoint(PDEVICE_DATA DeviceData, UCHAR* buffer, ULONG bufferLength)
- {
- ULONG frameNumber = 0;
- ULONG numberOfPackets = 128; // Adjust size if needed
- USBD_ISO_PACKET_DESCRIPTOR isoPacketDescriptors[128] = { 0 }; // Adjust size if needed
- OVERLAPPED overlapped = { 0 };
- overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- if (!overlapped.hEvent) {
- printf("Failed to create event for overlapped I/O.\n");
- return FALSE;
- }
- WINUSB_ISOCH_BUFFER_HANDLE isochBuffer;
- BOOL result = WinUsb_RegisterIsochBuffer(DeviceData->WinusbHandle, DeviceData->IsochronousInPipe,
- buffer, bufferLength, &isochBuffer);
- if (!result) {
- printf("Error registering isochronous buffer: %d.\n", GetLastError());
- CloseHandle(overlapped.hEvent);
- return FALSE;
- }
- result = WinUsb_ReadIsochPipe(isochBuffer, 0, bufferLength, &frameNumber,
- numberOfPackets, isoPacketDescriptors, &overlapped);
- if (!result && GetLastError() != ERROR_IO_PENDING) {
- printf("Error reading from isochronous endpoint: %d.\n", GetLastError());
- WinUsb_UnregisterIsochBuffer(isochBuffer);
- CloseHandle(overlapped.hEvent);
- return FALSE;
- }
- // Wait for the read operation to complete
- result = GetOverlappedResult(DeviceData->DeviceHandle, &overlapped, &frameNumber, TRUE);
- if (!result) {
- printf("Error in overlapped result: %d.\n", GetLastError());
- WinUsb_UnregisterIsochBuffer(isochBuffer);
- CloseHandle(overlapped.hEvent);
- return FALSE;
- }
- WinUsb_UnregisterIsochBuffer(isochBuffer);
- CloseHandle(overlapped.hEvent);
- // Print the data read from the isochronous endpoint
- for (ULONG i = 0; i < frameNumber; i++) {
- printf("Packet %lu: Status = %d, Length = %lu\n", i, isoPacketDescriptors[i].Status, isoPacketDescriptors[i].Length);
- }
- return TRUE;
- }
- HRESULT RetrieveDevicePath(LPTSTR DevicePath, ULONG BufLen, PBOOL FailureDeviceNotFound)
- {
- if (FailureDeviceNotFound) *FailureDeviceNotFound = FALSE;
- ULONG length = 0;
- CONFIGRET cr = CM_Get_Device_Interface_List_Size(&length, (LPGUID)&GUID_DEVINTERFACE_WinUSBApplication,
- NULL, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
- if (cr != CR_SUCCESS) return HRESULT_FROM_WIN32(CM_MapCrToWin32Err(cr, ERROR_INVALID_DATA));
- PTSTR DeviceInterfaceList = (PTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, length * sizeof(TCHAR));
- if (!DeviceInterfaceList) return E_OUTOFMEMORY;
- cr = CM_Get_Device_Interface_List((LPGUID)&GUID_DEVINTERFACE_WinUSBApplication, NULL, DeviceInterfaceList,
- length, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
- if (cr != CR_SUCCESS) {
- HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
- return HRESULT_FROM_WIN32(CM_MapCrToWin32Err(cr, ERROR_INVALID_DATA));
- }
- if (*DeviceInterfaceList == TEXT('\0')) {
- if (FailureDeviceNotFound) *FailureDeviceNotFound = TRUE;
- HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
- return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
- }
- HRESULT hr = StringCbCopy(DevicePath, BufLen, DeviceInterfaceList);
- HeapFree(GetProcessHeap(), 0, DeviceInterfaceList);
- return hr;
- }
- BOOL QueryDeviceEndpointsDirect(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR* IsochronousInPipe, UCHAR desiredAltSetting, UCHAR endpointIndex)
- {
- USB_INTERFACE_DESCRIPTOR InterfaceDescriptor;
- // Set the specified alternate setting
- if (!WinUsb_SetCurrentAlternateSetting(hDeviceHandle, desiredAltSetting)) {
- printf("Failed to set alternate setting %d: %d\n", desiredAltSetting, GetLastError());
- return FALSE;
- }
- // Query the interface descriptor for the specified alternate setting
- if (!WinUsb_QueryInterfaceSettings(hDeviceHandle, desiredAltSetting, &InterfaceDescriptor)) {
- printf("Failed to query interface settings for alternate setting %d: %d\n", desiredAltSetting, GetLastError());
- return FALSE;
- }
- printf("Alternate Setting %d:\n", desiredAltSetting);
- printf(" Interface Number: %d\n", InterfaceDescriptor.bInterfaceNumber);
- printf(" Number of Endpoints: %d\n", InterfaceDescriptor.bNumEndpoints);
- printf(" Interface Class: %02X\n", InterfaceDescriptor.bInterfaceClass);
- printf(" Interface SubClass: %02X\n", InterfaceDescriptor.bInterfaceSubClass);
- printf(" Interface Protocol: %02X\n", InterfaceDescriptor.bInterfaceProtocol);
- WINUSB_PIPE_INFORMATION Pipe;
- // Query the pipe for the known endpoint index
- if (!WinUsb_QueryPipe(hDeviceHandle, desiredAltSetting, endpointIndex, &Pipe)) {
- printf(" Failed to query pipe %d: %d\n", endpointIndex, GetLastError());
- return FALSE;
- }
- printf(" Endpoint %d: PipeType = %d, PipeId = %d\n", endpointIndex, Pipe.PipeType, Pipe.PipeId);
- // Ensure the endpoint is Isochronous and IN direction
- if (Pipe.PipeType == UsbdPipeTypeIsochronous && USB_ENDPOINT_DIRECTION_IN(Pipe.PipeId)) {
- *IsochronousInPipe = Pipe.PipeId;
- return TRUE;
- }
- printf("No suitable isochronous IN endpoint found.\n");
- return FALSE;
- }
- int _tmain()
- {
- DEVICE_DATA deviceData = {};
- BOOL noDevice = FALSE;
- HRESULT hr = OpenDevice(&deviceData, &noDevice);
- if (FAILED(hr)) {
- if (noDevice) wprintf(L"Device not connected or driver not installed\n");
- else wprintf(L"Failed to open device. HRESULT: 0x%x\n", hr);
- return 0;
- }
- UCHAR buffer[1024];
- if (!QueryDeviceEndpointsDirect(deviceData.WinusbHandle, &deviceData.IsochronousInPipe, /*desiredAltSetting*/ 1, /*endpointIndex*/ 0)) {
- wprintf(L"Failed to query device endpoints\n");
- CloseDevice(&deviceData);
- return 0;
- }
- if (ReadFromIsochronousEndpoint(&deviceData, buffer, sizeof(buffer))) {
- printf("Data successfully read from isochronous endpoint\n");
- }
- CloseDevice(&deviceData);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment