/// <summary> /// Sets the value of a named property for a CNG object. /// </summary> /// <typeparam name="T">The type of value being set.</typeparam> /// <param name="hObject">A handle that represents the CNG object to set the property value for.</param> /// <param name="propertyName"> /// The name of the property to set. This can be one of the predefined <see cref="KeyStoragePropertyIdentifiers"/> or a custom property identifier. /// </param> /// <param name="propertyValue">The new property value.</param> /// <param name="flags">Flags to pass to <see cref="NCryptSetProperty(SafeHandle, string, byte*, int, NCryptSetPropertyFlags)"/></param> public static unsafe void NCryptSetProperty <T>(SafeHandle hObject, string propertyName, T propertyValue, NCryptSetPropertyFlags flags = NCryptSetPropertyFlags.None) { int bufferSize = Marshal.SizeOf(propertyValue); fixed(byte *valueBuffer = new byte[bufferSize]) { Marshal.StructureToPtr(propertyValue, new IntPtr(valueBuffer), false); try { NCryptSetProperty(hObject, propertyName, valueBuffer, bufferSize, flags).ThrowOnError(); } finally { #if NETFX_CORE Marshal.DestroyStructure <T>(new IntPtr(valueBuffer)); #else Marshal.DestroyStructure(new IntPtr(valueBuffer), typeof(T)); #endif } } }
public static extern SECURITY_STATUS NCryptSetProperty( SafeHandle hObject, string pszProperty, string pbInput, int cbInput, NCryptSetPropertyFlags dwFlags);
public static extern unsafe SECURITY_STATUS NCryptSetProperty( SafeHandle hObject, string pszProperty, [Friendly(FriendlyFlags.Array | FriendlyFlags.In, ArrayLengthParameter = 3)] byte *pbInput, int cbInput, NCryptSetPropertyFlags dwFlags);
public SECURITY_STATUS NCryptSetProperty( SafeHandle hObject, string pszProperty, byte[] pbInput, int cbInput, NCryptSetPropertyFlags dwFlags) => NCryptSetProperty(hObject, pszProperty, pbInput, cbInput, dwFlags);