Condividi tramite


Accesso al Registro di sistema per i driver WIA

Gli sviluppatori di driver devono conoscere le autorizzazioni per le chiavi del Registro di sistema a cui devono accedere. Gran parte del Registro di sistema è disponibile per la lettura del driver. Tuttavia, i driver WIA devono scrivere solo nella chiave del Registro di sistema passata nel metodo IStiUSD::Initialize .

Anche se la scrittura in altre chiavi del Registro di sistema è possibile in Windows XP, poiché il servizio WIA viene eseguito con l'account LocalSystem con privilegi elevati, questo non è più possibile con l'account LocalService con privilegi limitati in Microsoft Windows Server 2003 e versioni successive.

I driver richiedono spesso l'accesso in scrittura alla chiave del Registro di sistema all'esterno di IStiUSD::Initialize. Poiché la maggior parte dei driver archivia i dati nella sottochiave DeviceData , è facile aprire la sottochiave DeviceData e archiviare l'handle per la chiave aperta da usare in un secondo momento. Il driver deve chiudere la chiave del Registro di sistema solo quando non richiede più la chiave.

L'esempio di codice seguente illustra l'uso della sottochiave del Registro di sistema DeviceData .

STDMETHODIMP CWIADevice::Initialize(
  PSTIDEVICECONTROL   pIStiDevControl,
  DWORD               dwStiVersion,
  HKEY                hParametersKey)
{
  .
  .
  .
  //
  // Open the DeviceData key since this is where the
  // driver-specific settings will be stored.
  //
  DWORD dwError = RegOpenKeyEx(
                 hParametersKey,     // handle to open key
                 TEXT("DeviceData"), // subkey to open
                 0,                  // options (must be NULL)
                 KEY_READ|KEY_WRITE, // requesting read/write access
                 &m_hMyWritableRegistryKey);
  if (dwError == ERROR_SUCCESS)
  {
      //
      //  m_hMyWritableRegistryKey now contains a handle to the
      //  DeviceData subkey which can be used to store information
      //  in the registry.
      //  Notice that it isn't closed here, but instead,
      //  kept open because it is needed later.
     //
  }
  else 
  {
      // Handle error
      .
      .
      .
  }
  .
  .
  .
}

STDMETHODIMP CWIADevice::SomeDriverMethod()
{
  .
  .
  .
  //
  //  We need to store some setting in the registry here.
  //
  DWORD dwError = RegSetValueEx(
                     m_hMyWritableRegistryKey,
                     TEXT("MyDriverValueName"),
                     0,
                     REG_DWORD,
                     (BYTE*)&dwValue,
                     sizeof(dwValue));
  if (dwError == ERROR_SUCCESS)
  {
      //
      //  We successfully stored dwValue in the registry
      //
  }
  else 
  {
      // Handle error
      .
      .
      .
  }
  .
  .
  .
}

CWIADevice:: CWIADevice () :
  m_hMyWritableRegistryKey(NULL),
  .
  .
  .
{
  //  Rest of constructor goes here.  Ensure that the
  //   handle to the registry key is initialized to NULL.
}

CWIADevice::~CWIADevice(void)
{
  .
  .
  .
  //
  // If the writable registry key isn't closed  yet, do it now,
  // because the driver is about to be unloaded.
  //
  if (m_hMyWritableRegistryKey) 
  {
      RegCloseKey(m_hMyWritableRegistryKey);
      m_hMyWritableRegistryKey = NULL;
  }

  .
  .
  .
}

La sottochiave del Registro di sistema DeviceData è aperta per l'accesso in lettura/scrittura al driver in Windows Me e Windows XP e versioni successive. La chiave del dispositivo stessa (ad esempio, la chiave del Registro di sistema padre a DeviceData) può essere aperta o meno per l'accesso in scrittura dal driver, a seconda della versione del sistema operativo.

Nota Il driver deve chiudere tutte le chiavi del Registro di sistema aperte quando non sono più necessarie e deve chiudere tutte le chiavi del Registro di sistema prima dello scaricamento.