Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Questo argomento illustra come creare un'istanza dell'oggetto di accesso al dispositivo e usarlo per accedere a un dispositivo. La classe istanziata implementa le interfacce IDeviceIoControl e ICreateDeviceAccessAsync.
Disposizioni
Passaggio 1
Per istanziare l'oggetto di accesso al dispositivo, è necessario prima chiamare la funzione CreateDeviceAccessInstance. Se CreateDeviceAccessInstance ha esito positivo, è possibile chiamare il metodo Wait per attendere il completamento dell'operazione asincrona. Se Wait ha esito positivo, è possibile recuperare un oggettoIDeviceIoControl(o l'errore appropriato) dal metodo GetResult.
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;
}
Passaggio 2
Questo è un esempio di chiamata al metodo 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;
}
Osservazioni
È anche possibile inviare un IOCTL in modo asincrono usando il metodoDeviceIoControlAsync. In tal caso, è necessario implementare l'interfaccia IDeviceRequestCompletionCallback.
Argomenti correlati
esempio di accesso ai driver personalizzati, app UWP per i dispositivi interni, Centro sviluppo hardware