From f3716aefb721bb3dfa434fe1a73d01cf7dd0dbd4 Mon Sep 17 00:00:00 2001 From: David Hall Date: Wed, 13 Nov 2019 23:05:25 -0500 Subject: [PATCH] Added generic helper functions for query and set. --- PInvoke/Security/AdvApi32/NTSecApi.Lsa.cs | 54 +++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/PInvoke/Security/AdvApi32/NTSecApi.Lsa.cs b/PInvoke/Security/AdvApi32/NTSecApi.Lsa.cs index 50a7df7e..3b984ff6 100644 --- a/PInvoke/Security/AdvApi32/NTSecApi.Lsa.cs +++ b/PInvoke/Security/AdvApi32/NTSecApi.Lsa.cs @@ -1640,6 +1640,24 @@ namespace Vanara.PInvoke [PInvokeData("ntsecapi.h", MSDNShortId = "2d543500-f639-4ef7-91f4-cdc5060dd567")] public static extern NTStatus LsaQueryInformationPolicy(LSA_HANDLE PolicyHandle, POLICY_INFORMATION_CLASS InformationClass, out SafeLsaMemoryHandle Buffer); + /// The LsaQueryInformationPolicy function retrieves information about a Policy object. + /// + /// A handle to a Policy object. The required access rights for this handle depend on the value of the InformationClass parameter. + /// For more information, see Opening a Policy Object Handle. + /// + /// The structure containing the requested information. + /// + /// For an example that demonstrates calling this function see Managing Policy Information. + [PInvokeData("ntsecapi.h", MSDNShortId = "2d543500-f639-4ef7-91f4-cdc5060dd567")] + public static T LsaQueryInformationPolicy(LSA_HANDLE PolicyHandle) where T : struct + { + if (!CorrespondingTypeAttribute.CanGet(out var ic)) + throw new ArgumentException($"Type mismatch between {ic} and {typeof(T).Name}."); + LsaQueryInformationPolicy(PolicyHandle, ic, out var mem).ThrowIfFailed(); + using (mem) + return mem.DangerousGetHandle().Convert(uint.MaxValue, CharSet.Unicode); + } + /// /// The LsaQueryTrustedDomainInfo function retrieves information about a trusted domain. /// @@ -1932,8 +1950,6 @@ namespace Vanara.PInvoke public static extern NTStatus LsaRetrievePrivateData(LSA_HANDLE PolicyHandle, [In, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaUnicodeStringMarshaler))] string KeyName, out SafeLsaMemoryHandle PrivateData); - //[Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LsaUnicodeStringMarshaler))] out string PrivateData); - /// The LsaSetDomainInformationPolicy function sets domain information to the Policyobject. /// A handle to the Policy object for the system. /// @@ -1984,6 +2000,23 @@ namespace Vanara.PInvoke [PInvokeData("ntsecapi.h", MSDNShortId = "77af6fdc-a52e-476c-9de2-36ee48133a87")] public static extern NTStatus LsaSetDomainInformationPolicy(LSA_HANDLE PolicyHandle, POLICY_DOMAIN_INFORMATION_CLASS InformationClass, IntPtr Buffer); + /// The LsaSetDomainInformationPolicy function sets domain information to the Policyobject. + /// The type of the structure being passed as . + /// A handle to the Policy object for the system. + /// The information to set to the Policyobject. + /// + /// The POLICY_TRUST_ADMIN access type is required to set domain information to the Policyobject. For more information, see Policy + /// Object Access Rights. + /// + [PInvokeData("ntsecapi.h", MSDNShortId = "77af6fdc-a52e-476c-9de2-36ee48133a87")] + public static void LsaSetDomainInformationPolicy(LSA_HANDLE PolicyHandle, in T value) where T : struct + { + if (!CorrespondingTypeAttribute.CanSet(out var ic)) + throw new ArgumentException($"Unable to use {typeof(T).Name} with this function."); + using var mem = SafeHGlobalHandle.CreateFromStructure(value); + LsaSetDomainInformationPolicy(PolicyHandle, ic, mem).ThrowIfFailed(); + } + /// /// /// The LsaSetForestTrustInformation function sets the forest trust information for a specified Local Security Authority @@ -2116,6 +2149,23 @@ namespace Vanara.PInvoke [PInvokeData("ntsecapi.h", MSDNShortId = "2aa3b09e-2cd9-4a09-bfd6-b37c97266dcb")] public static extern NTStatus LsaSetInformationPolicy(LSA_HANDLE PolicyHandle, POLICY_INFORMATION_CLASS InformationClass, IntPtr Buffer); + /// + /// The LsaSetInformationPolicy function modifies information in a Policy object. + /// + /// The type of the structure being passed as . + /// A handle to a Policy object. The required access rights for this handle depend on the value of the InformationClass parameter. + /// For more information, see Opening a Policy Object Handle. + /// A structure containing the information to set. + /// Unable to use {typeof(T).Name} with this function. + [PInvokeData("ntsecapi.h", MSDNShortId = "2aa3b09e-2cd9-4a09-bfd6-b37c97266dcb")] + public static void LsaSetInformationPolicy(LSA_HANDLE PolicyHandle, in T value) where T : struct + { + if (!CorrespondingTypeAttribute.CanSet(out var ic)) + throw new ArgumentException($"Unable to use {typeof(T).Name} with this function."); + using var mem = SafeHGlobalHandle.CreateFromStructure(value); + LsaSetInformationPolicy(PolicyHandle, ic, mem).ThrowIfFailed(); + } + /// Undocumented. /// The account handle. /// The system access.