이 항목에서는 디바이스 액세스 개체를 인스턴스화하고 이를 사용하여 디바이스에 액세스하는 방법을 설명합니다. 인스턴스화된 클래스는 IDeviceIoControl 및 ICreateDeviceAccessAsync 인터페이스를 구현합니다.
지시
1단계
디바이스 액세스 개체를 인스턴스화하려면 먼저 CreateDeviceAccessInstance 함수를 호출해야 합니다. CreateDeviceAccessInstance 성공하면 Wait 메서드를 호출하여 비동기 작업이 완료되기를 기다릴 수 있습니다. Wait 성공하면 GetResult 메서드에서 IDeviceIoControl 개체(또는 적절한 오류)를 검색할 수 있습니다.
HRESULT
CMyServer::Initialize(
PCWSTR pszDeviceInterfacePath
)
/*++
Routine Description:
This routine is called to initialize the Device Access object.
It's not part of the constructor as the initialization can fail.
It opens the device and gets the IDeviceIoControl interface to the device instance
via the broker.
Arguments:
pszDeviceInterfacePath - the device interface string that needs to be opened
Return Value:
HRESULT
--*/
{
HRESULT hr;
ICreateDeviceAccessAsync *pDeviceAccess;
//
// Here's the actual open call. This will *fail* if your lowbox does
// not have the capability mapped to the interface class specified.
// If you are running this as normal user, it will just pass through to
// create file.
//
hr = CreateDeviceAccessInstance(pszDeviceInterfacePath,
GENERIC_READ|GENERIC_WRITE,
&pDeviceAccess);
if (FAILED(hr)) {
return hr;
}
if (SUCCEEDED(hr)) {
hr = pDeviceAccess->Wait(INFINITE);
}
if (SUCCEEDED(hr)) {
hr = pDeviceAccess->GetResult(IID_IDeviceIoControl,
(void **)&m_pDeviceIoControl);
}
pDeviceAccess->Release();
return hr;
}
2단계
이는 DeviceIoControlSync 메서드 호출의 예입니다.
IFACEMETHODIMP
CMyServer::put_SevenSegmentDisplay(
BYTE value
)
/*++
Routine Description:
This routine puts the display value into the device.
Arguments:
value - The value to be written to the device.
Return Value:
HRESULT
--*/
{
BYTE sevenSegment = 0;
HRESULT hr;
if (value >= ARRAYSIZE(g_NumberToMask)) {
return E_INVALIDARG;
}
sevenSegment = g_NumberToMask[value];
hr = m_pDeviceIoControl->DeviceIoControlSync(
IOCTL_OSRUSBFX2_SET_7_SEGMENT_DISPLAY,
&sevenSegment,
sizeof(BYTE),
NULL,
0,
NULL
);
return hr;
}
발언
DeviceIoControlAsync 메서드를 사용하여 IOCTL을 비동기적으로 보낼 수도 있습니다. 이 경우 IDeviceRequestCompletionCallback 인터페이스를 구현해야 합니다.
관련 항목
사용자 지정 드라이버 액세스 샘플, 내부 디바이스 대한 UWP 디바이스 앱, 하드웨어 개발자 센터