Windows::Devices::Usb::UsbDevice
サードパーティのプラグインに渡すために、私が持っている特定の USB デバイスを参照するオブジェクトを取得しようとしています。プロジェクトの制限により、C++/CX
拡張機能を使用できません。
無数のスレッド、回答、参照を調べた後、必要な WinRT クラスで静的メソッドを呼び出すための黒魔術を行う初期実装を思いつきました。唯一の問題は、呼び出しが失敗した場合でも、HRESULT
への最後の呼び出しがFromIdAsync
機能せず、の結果としてERROR_INVALID_HANDLE
( 6
) が返されることですGetLastError()
。
エラー名を読むだけで、デバイスのIDを取得するのが間違いだと思います。これは、その呼び出しで渡す唯一のハンドルですが、代わりに定数文字列を渡そうとしました(正しいことはわかっていました)。同じ結果が得られました。
これが私が呼び出す方法です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());
そして、これが私が得ている方法です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());
また、次のように WinRT を初期化します。
RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
if (FAILED(initialize)) return nullptr;
*if (FAILED(hr)) return nullptr;
簡潔にするために複数を削除しました。