Gestione delle informazioni sui criteri

LSA fornisce funzioni che gli amministratori possono usare per eseguire query e impostare le informazioni sui criteri globali per il computer locale e il dominio.

Prima di poter gestire le informazioni sui criteri, l'applicazione deve ottenere un handle all'oggetto Criteri locale, come illustrato in Apertura di un handle oggetto criteri. Questo handle è richiesto dalle funzioni che gestiscono le informazioni sui criteri.

Per recuperare informazioni sui criteri di sicurezza locali, chiamare LsaQueryInformationPolicy. Per impostare i criteri di sicurezza locali, chiamare LsaSetInformationPolicy. La descrizione dell'enumerazione POLICY_INFORMATION_CLASS dettaglia i tipi di informazioni sui criteri che possono essere sottoposti a query o set.

Nell'esempio seguente viene illustrato come ottenere le informazioni sul dominio dell'account di un sistema, dato un handle all'oggetto Policy del sistema.

#include <windows.h>

BOOL GetAccountDomainInfo(LSA_HANDLE PolicyHandle)
{
  NTSTATUS ntsResult = STATUS_SUCCESS;
  PPOLICY_ACCOUNT_DOMAIN_INFO pPADInfo = NULL;
  PWCHAR name = NULL;
  UINT nameSize;
  
  ntsResult = LsaQueryInformationPolicy(
    PolicyHandle,                   // Open handle to a Policy object.
    PolicyAccountDomainInformation, // The information to get.
    (PVOID *)&pPADInfo              // Storage for the information.
  );

  if (ntsResult == STATUS_SUCCESS)
  {  
    // There is no guarantee that the LSA_UNICODE_STRING buffer
    // is null-terminated, so copy the name to a buffer that is.
    nameSize =  pPADInfo->DomainName.Length + 1 * sizeof(WCHAR);
    name = (WCHAR *) LocalAlloc(LPTR, nameSize);
    if (!name)
    {
        wprintf(L"Failed LocalAlloc\n");
        exit(1);
    }
    wcsncpy_s(name, nameSize, pPADInfo->DomainName.Buffer,
        pPADInfo->DomainName.Length);
    wprintf(L"The account domain name is %ws\n", name);
    LocalFree(name);
    if (STATUS_SUCCESS != LsaFreeMemory(pPADInfo))
        wprintf(L"LsaFreeMemory error\n");
  }
  else
  {
    // Show the corresponding win32 error code.
    wprintf(
        L"Error obtaining account domain information - (win32) %lu\n",
        LsaNtStatusToWinError(ntsResult));
  }

  return !ntsResult;
}