dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

NCrypt.NCryptEnumKeys does not update `ppEnumState` #409

Closed NZSmartie closed 5 years ago

NZSmartie commented 5 years ago

To avoid using unsafe keywords in my project, I've opted to using the ItrPtr override of NCryptEnumKeys which is available from PInvoke.NCrypt library.

The test for NCryptEnumKeys uses the unsafe void* for the ppEnumState parameter, gets updated during the invocation and passed back in order to get the next key from the storage provider during each loop.

https://github.com/AArnott/pinvoke/blob/da0071be3fca89ea12f4bf43eea1b6c7ec836f76/src/NCrypt.Tests/NCryptFacts.cs#L220-L256

The IntPtr variant with the following signature does not update the variable passed into ppEnumState

public static SECURITY_STATUS NCryptEnumKeys(SafeProviderHandle hProvider, string pszScope, out IntPtr ppKeyName, ref IntPtr ppEnumState, NCryptEnumKeysFlags dwFlags = NCryptEnumKeysFlags.None);

If i add the following to my own class, then the issue goes away:

[DllImport("ncrypt.dll")]
private static extern SECURITY_STATUS NCryptEnumKeys(SafeProviderHandle hProvider,
                                                    [In, MarshalAs(UnmanagedType.LPWStr)] string pszScope,
                                                    [Out] out IntPtr ppKeyName,
                                                    [In, Out] ref IntPtr ppEnumState,
                                                    NCryptEnumKeysFlags dwFlags);
AArnott commented 5 years ago

Thank you for drawing this to our attention, @NZSmartie