I'm trying to obtain a Windows::Devices::Usb::UsbDevice
object referencing a specific USB device I have in order to pass it to a 3rd party plugin. Due to project limitations, I cannot use the C++/CX
extension.
After looking at countless threads, answers and references, I came up with a initial implementation that does some black magic to invoke static methods on the WinRT classes I need. The only issue is that, even though no call results a failed HRESULT
, the last call to FromIdAsync
doesn't work, giving me ERROR_INVALID_HANDLE
(6
) as a result of GetLastError()
.
Simply reading the error name makes me thing the mistake is in getting the device's ID, as that's the only handle I pass on that call, but I tried passing a constant string instead (which I knew was correct) and it yielded the same results.
This is how I'm invoking FromIdAsync
*:
// Retrieves static methods for UsbDevice class
ComPtr<IUsbDeviceStatics> usbDevSt;
hr = GetActivationFactory(
HStringReference(RuntimeClass_Windows_Devices_Usb_UsbDevice).Get(),
&usbDevSt
);
// Creates an event to work as a 'semaphore', for waiting for the 'FromIdAsync'
// call to be completed
Event openEvent(CreateEventEx(
nullptr,
nullptr,
CREATE_EVENT_MANUAL_RESET,
WRITE_OWNER | EVENT_ALL_ACCESS
));
if (!openEvent.IsValid()) return nullptr;
// Setups a callback for when the device enumeration is done
auto asyncOpenCb = Callback<IAsyncOperationCompletedHandler<UsbDevice*>>(
[&openEvent](IAsyncOperation<UsbDevice*> *opHandler, AsyncStatus status) -> HRESULT {
if (!opHandler || status != AsyncStatus::Completed) {
DWORD x = GetLastError(); // ERROR_INVALID_HANDLE (6)
}
SetEvent(openEvent.Get());
return S_OK;
}
);
// Invokes the 'asyncOpenOp' method, equivalent to UsbDevice::FromIdAsync(String)
ComPtr<IAsyncOperation<UsbDevice*>> asyncOpenOp;
hr = usbDevSt->FromIdAsync(
devId.Get(),
asyncOpenOp.GetAddressOf()
);
// Registers completed callback
hr = asyncOpenOp->put_Completed(asyncOpenCb.Get());
// Waits for open operation to complete before continuing
WaitForSingleObjectEx(openEvent.Get(), INFINITE, false);
// Retrieves the result from the asynchronous call
ComPtr<IUsbDevice> dev;
hr = asyncOpenOp->GetResults(dev.GetAddressOf());
And this is how I'm getting devId
*:
// Retrieve static methods for DeviceInformation class
ComPtr<IDeviceInformationStatics> devInfoSt;
HRESULT hr = GetActivationFactory(
HStringReference(RuntimeClass_Windows_Devices_Enumeration_DeviceInformation).Get(),
&devInfoSt
);
// Create an event to work as a 'semaphore', for waiting for the 'FindAllAsyncAqsFilter' call to be completed
Event findEvent(CreateEventEx(
nullptr,
nullptr,
CREATE_EVENT_MANUAL_RESET,
WRITE_OWNER | EVENT_ALL_ACCESS
));
if (!findEvent.IsValid()) return nullptr;
// Setup a callback for when the device enumeration is done
auto asyncFindCb = Callback<IAsyncOperationCompletedHandler<DeviceInformationCollection*>>(
[&findEvent](IAsyncOperation<DeviceInformationCollection*> *opHandler, AsyncStatus status) -> HRESULT {
SetEvent(findEvent.Get());
return S_OK;
}
);
// Invoke the 'FindAllAsyncAqsFilter' method, equivalent to DeviceInformation::FindAllAsync(String)
ComPtr<IAsyncOperation<DeviceInformationCollection*>> asyncFindOp;
hr = devInfoSt->FindAllAsyncAqsFilter(
HStringReference(DEVICE_FILTER).Get(),
asyncFindOp.GetAddressOf()
);
// Registers completed callback
hr = asyncFindOp->put_Completed(asyncFindCb.Get());
// Waits for enumeration to complete before continuing
WaitForSingleObjectEx(findEvent.Get(), INFINITE, FALSE);
// Retrieves the result from the asynchronous call
ComPtr<IVectorView<DeviceInformation*>> devColl;
hr = asyncFindOp->GetResults(devColl.GetAddressOf());
// Checks for collection size
unsigned int collSize;
hr = devColl->get_Size(&collSize);
if (collSize == 0) {
return nullptr;
}
// Retrieves the first DeviceInformation object from the collection
ComPtr<IDeviceInformation> devInfo;
hr = devColl->GetAt(0, devInfo.GetAddressOf());
// Retrieves the device's id
HString devId;
hr = devInfo->get_Id(devId.GetAddressOf());
Also, I do initialize WinRT, this way:
RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize)) return nullptr;
*Multiple if (FAILED(hr)) return nullptr;
removed for brevity.
S_OK
except for the last one (asyncOpenOp->getResults(...)
), which gives me0x8000000E
(A method was called at an unexpected time), which makes completely sense because the operation has already failed. – Subcontract