diff --git a/eng/generators.targets b/eng/generators.targets index e20fa992012aa..757a91ac9d90c 100644 --- a/eng/generators.targets +++ b/eng/generators.targets @@ -18,7 +18,7 @@ + @@ -35,8 +41,10 @@ and '$(IncludeDllImportGeneratorSources)' == 'true'"> - - + + + + $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) true annotations + true $(DefineConstants);NO_SUPPRESS_GC_TRANSITION false diff --git a/src/libraries/System.Data.OleDb/src/DbPropSet.cs b/src/libraries/System.Data.OleDb/src/DbPropSet.cs index 0adeb55998688..6d462d9970c24 100644 --- a/src/libraries/System.Data.OleDb/src/DbPropSet.cs +++ b/src/libraries/System.Data.OleDb/src/DbPropSet.cs @@ -24,13 +24,13 @@ public DBPropSet() : base(IntPtr.Zero, true) internal DBPropSet(int propertysetCount) : this() { this.propertySetCount = propertysetCount; - IntPtr countOfBytes = (IntPtr)(propertysetCount * ODB.SizeOf_tagDBPROPSET); + nuint countOfBytes = (nuint)(propertysetCount * ODB.SizeOf_tagDBPROPSET); RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { - base.handle = SafeNativeMethods.CoTaskMemAlloc(countOfBytes); + base.handle = Interop.Ole32.CoTaskMemAlloc(countOfBytes); if (ADP.PtrZero != base.handle) { SafeNativeMethods.ZeroMemory(base.handle, (int)countOfBytes); @@ -145,12 +145,12 @@ protected override bool ReleaseHandle() IntPtr vptr = ADP.IntPtrOffset(rgProperties, ODB.OffsetOf_tagDBPROP_Value); for (int k = 0; k < cProperties; ++k, vptr = ADP.IntPtrOffset(vptr, ODB.SizeOf_tagDBPROP)) { - SafeNativeMethods.VariantClear(vptr); + Interop.OleAut32.VariantClear(vptr); } - SafeNativeMethods.CoTaskMemFree(rgProperties); + Interop.Ole32.CoTaskMemFree(rgProperties); } } - SafeNativeMethods.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } return true; } @@ -227,7 +227,7 @@ internal void SetPropertySet(int index, Guid propertySet, ItagDBPROP[] propertie Debug.Assert(Guid.Empty != propertySet, "invalid propertySet"); Debug.Assert((null != properties) && (0 < properties.Length), "invalid properties"); - IntPtr countOfBytes = (IntPtr)(properties.Length * ODB.SizeOf_tagDBPROP); + nuint countOfBytes = (nuint)(properties.Length * ODB.SizeOf_tagDBPROP); tagDBPROPSET propset = new tagDBPROPSET(properties.Length, propertySet); bool mustRelease = false; @@ -244,7 +244,7 @@ internal void SetPropertySet(int index, Guid propertySet, ItagDBPROP[] propertie finally { // must allocate and clear the memory without interruption - propset.rgProperties = SafeNativeMethods.CoTaskMemAlloc(countOfBytes); + propset.rgProperties = Interop.Ole32.CoTaskMemAlloc(countOfBytes); if (ADP.PtrZero != propset.rgProperties) { // clearing is important so that we don't treat existing diff --git a/src/libraries/System.Data.OleDb/src/NativeMethods.cs b/src/libraries/System.Data.OleDb/src/NativeMethods.cs index c6ecc82dd0a16..51038511a801b 100644 --- a/src/libraries/System.Data.OleDb/src/NativeMethods.cs +++ b/src/libraries/System.Data.OleDb/src/NativeMethods.cs @@ -5,7 +5,7 @@ namespace System.Data.Common { - internal static class NativeMethods + internal static partial class NativeMethods { [Guid("0c733a1e-2a1c-11ce-ade5-00aa0044773d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComImport] internal interface ISourcesRowset @@ -32,67 +32,5 @@ void JoinTransaction( [In] int isoFlags, [In] IntPtr pOtherOptions); } - - [DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - internal static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, IntPtr dwNumberOfBytesToMap); - - [DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress); - - [DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool CloseHandle(IntPtr handle); - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool AllocateAndInitializeSid( - IntPtr pIdentifierAuthority, // authority - byte nSubAuthorityCount, // count of subauthorities - int dwSubAuthority0, // subauthority 0 - int dwSubAuthority1, // subauthority 1 - int dwSubAuthority2, // subauthority 2 - int dwSubAuthority3, // subauthority 3 - int dwSubAuthority4, // subauthority 4 - int dwSubAuthority5, // subauthority 5 - int dwSubAuthority6, // subauthority 6 - int dwSubAuthority7, // subauthority 7 - ref IntPtr pSid); // SID - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern int GetLengthSid( - IntPtr pSid); // SID to query - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool InitializeAcl( - IntPtr pAcl, // ACL - int nAclLength, // size of ACL - int dwAclRevision); // revision level of ACL - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool AddAccessDeniedAce( - IntPtr pAcl, // access control list - int dwAceRevision, // ACL revision level - int AccessMask, // access mask - IntPtr pSid); // security identifier - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool AddAccessAllowedAce( - IntPtr pAcl, // access control list - int dwAceRevision, // ACL revision level - uint AccessMask, // access mask - IntPtr pSid); // security identifier - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool InitializeSecurityDescriptor( - IntPtr pSecurityDescriptor, // SD - int dwRevision); // revision level - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern bool SetSecurityDescriptorDacl( - IntPtr pSecurityDescriptor, // SD - bool bDaclPresent, // DACL presence - IntPtr pDacl, // DACL - bool bDaclDefaulted); // default DACL - - [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] - internal static extern IntPtr FreeSid( - IntPtr pSid); // SID to free } } diff --git a/src/libraries/System.Data.OleDb/src/OleDbError.cs b/src/libraries/System.Data.OleDb/src/OleDbError.cs index 333cdb3128a79..9fa39bc0472e1 100644 --- a/src/libraries/System.Data.OleDb/src/OleDbError.cs +++ b/src/libraries/System.Data.OleDb/src/OleDbError.cs @@ -25,7 +25,7 @@ internal OleDbError(UnsafeNativeMethods.IErrorRecords errorRecords, int index) if (OleDbHResult.DB_E_NOLOCALE == hr) { Marshal.ReleaseComObject(errorInfo); - lcid = SafeNativeMethods.GetUserDefaultLCID(); + lcid = Interop.Kernel32.GetUserDefaultLCID(); errorInfo = errorRecords.GetErrorInfo(index, lcid); if (null != errorInfo) @@ -44,7 +44,7 @@ internal OleDbError(UnsafeNativeMethods.IErrorRecords errorRecords, int index) if (OleDbHResult.DB_E_NOLOCALE == hr) { Marshal.ReleaseComObject(errorInfo); - lcid = SafeNativeMethods.GetUserDefaultLCID(); + lcid = Interop.Kernel32.GetUserDefaultLCID(); errorInfo = errorRecords.GetErrorInfo(index, lcid); if (null != errorInfo) diff --git a/src/libraries/System.Data.OleDb/src/PropertyInfoSet.cs b/src/libraries/System.Data.OleDb/src/PropertyInfoSet.cs index 67f95789eb04a..c17c30cc7b3db 100644 --- a/src/libraries/System.Data.OleDb/src/PropertyInfoSet.cs +++ b/src/libraries/System.Data.OleDb/src/PropertyInfoSet.cs @@ -127,19 +127,19 @@ protected override bool ReleaseHandle() for (int k = 0; k < infoCount; ++k) { IntPtr valuePtr = ADP.IntPtrOffset(infoPtr, (k * ODB.SizeOf_tagDBPROPINFO) + ODB.OffsetOf_tagDBPROPINFO_Value); - SafeNativeMethods.VariantClear(valuePtr); + Interop.OleAut32.VariantClear(valuePtr); } - SafeNativeMethods.CoTaskMemFree(infoPtr); // was allocated by provider + Interop.Ole32.CoTaskMemFree(infoPtr); // was allocated by provider } } - SafeNativeMethods.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } ptr = this.descBuffer; this.descBuffer = IntPtr.Zero; if (IntPtr.Zero != ptr) { - SafeNativeMethods.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } return true; } diff --git a/src/libraries/System.Data.OleDb/src/RowBinding.cs b/src/libraries/System.Data.OleDb/src/RowBinding.cs index 75312475fcdd7..80eb928349067 100644 --- a/src/libraries/System.Data.OleDb/src/RowBinding.cs +++ b/src/libraries/System.Data.OleDb/src/RowBinding.cs @@ -257,7 +257,7 @@ internal void SetBstrValue(int offset, string value) { } finally { - ptr = SafeNativeMethods.SysAllocStringLen(value, value.Length); + ptr = Interop.OleAut32.SysAllocStringLen(value, (uint)value.Length); // safe to copy ptr, even if SysAllocStringLen failed Marshal.WriteIntPtr(base.handle, offset, ptr); @@ -499,11 +499,11 @@ private static void FreeBstr(IntPtr buffer, int valueOffset) if ((ADP.PtrZero != currentValue) && (currentValue != originalValue)) { - SafeNativeMethods.SysFreeString(currentValue); + Interop.OleAut32.SysFreeString(currentValue); } if (ADP.PtrZero != originalValue) { - SafeNativeMethods.SysFreeString(originalValue); + Interop.OleAut32.SysFreeString(originalValue); } // for debugability - delay clearing memory until after FreeBSTR @@ -529,7 +529,7 @@ private static void FreeCoTaskMem(IntPtr buffer, int valueOffset) // originalValue is pinned managed memory or pointer to emptyStringOffset if ((ADP.PtrZero != currentValue) && (currentValue != originalValue)) { - SafeNativeMethods.CoTaskMemFree(currentValue); + Interop.Ole32.CoTaskMemFree(currentValue); } // for debugability - delay clearing memory until after CoTaskMemFree @@ -556,11 +556,11 @@ private static void FreeVariant(IntPtr buffer, int valueOffset) finally { // always clear the first structure - SafeNativeMethods.VariantClear(currentHandle); + Interop.OleAut32.VariantClear(currentHandle); if (different) { // second structure different from the first - SafeNativeMethods.VariantClear(originalHandle); + Interop.OleAut32.VariantClear(originalHandle); } else { @@ -587,11 +587,11 @@ private static unsafe void FreePropVariant(IntPtr buffer, int valueOffset) finally { // always clear the first structure - SafeNativeMethods.PropVariantClear(currentHandle); + Interop.Ole32.PropVariantClear(currentHandle); if (different) { // second structure different from the first - SafeNativeMethods.PropVariantClear(originalHandle); + Interop.Ole32.PropVariantClear(originalHandle); } else { diff --git a/src/libraries/System.Data.OleDb/src/SafeHandles.cs b/src/libraries/System.Data.OleDb/src/SafeHandles.cs index edd87806063a0..1157f3e3a0580 100644 --- a/src/libraries/System.Data.OleDb/src/SafeHandles.cs +++ b/src/libraries/System.Data.OleDb/src/SafeHandles.cs @@ -66,14 +66,14 @@ protected override bool ReleaseHandle() base.handle = IntPtr.Zero; if (IntPtr.Zero != ptr) { - SafeNativeMethods.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } ptr = this.handle2; this.handle2 = IntPtr.Zero; if (IntPtr.Zero != ptr) { - SafeNativeMethods.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } return true; } diff --git a/src/libraries/System.Data.OleDb/src/SafeNativeMethods.cs b/src/libraries/System.Data.OleDb/src/SafeNativeMethods.cs index e2e5766615a8b..a89a5c30a40c4 100644 --- a/src/libraries/System.Data.OleDb/src/SafeNativeMethods.cs +++ b/src/libraries/System.Data.OleDb/src/SafeNativeMethods.cs @@ -10,17 +10,8 @@ namespace System.Data.Common { [SuppressUnmanagedCodeSecurity] - internal static class SafeNativeMethods + internal static partial class SafeNativeMethods { - [DllImport(Interop.Libraries.Ole32, SetLastError = false)] - internal static extern IntPtr CoTaskMemAlloc(IntPtr cb); - - [DllImport(Interop.Libraries.Ole32, SetLastError = false)] - internal static extern void CoTaskMemFree(IntPtr handle); - - [DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode, PreserveSig = true)] - internal static extern int GetUserDefaultLCID(); - internal static void ZeroMemory(IntPtr ptr, int length) { var zeroes = new byte[length]; @@ -44,40 +35,14 @@ internal static unsafe IntPtr InterlockedExchangePointer( return actualPtr; } - [DllImport(Interop.Libraries.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto)] - internal static extern int GetCurrentProcessId(); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern IntPtr LocalAlloc(int flags, IntPtr countOfBytes); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern IntPtr LocalFree(IntPtr handle); - - [DllImport(Interop.Libraries.OleAut32, CharSet = CharSet.Unicode)] - internal static extern IntPtr SysAllocStringLen(string src, int len); // BSTR - - [DllImport(Interop.Libraries.OleAut32)] - internal static extern void SysFreeString(IntPtr bstr); - - // only using this to clear existing error info with null - [DllImport(Interop.Libraries.OleAut32, CharSet = CharSet.Unicode, PreserveSig = false)] - // TLS values are preserved between threads, need to check that we use this API to clear the error state only. - private static extern void SetErrorInfo(int dwReserved, IntPtr pIErrorInfo); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern int ReleaseSemaphore(IntPtr handle, int releaseCount, IntPtr previousCount); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern int WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, bool bWaitAll, uint dwMilliseconds, bool bAlertable); - - [DllImport(Interop.Libraries.Kernel32/*, SetLastError=true*/)] - internal static extern int WaitForSingleObjectEx(IntPtr lpHandles, uint dwMilliseconds, bool bAlertable); + [GeneratedDllImport(Interop.Libraries.Kernel32, SetLastError = true)] + internal static partial int ReleaseSemaphore(IntPtr handle, int releaseCount, IntPtr previousCount); - [DllImport(Interop.Libraries.Ole32, PreserveSig = false)] - internal static extern void PropVariantClear(IntPtr pObject); + [GeneratedDllImport(Interop.Libraries.Kernel32, SetLastError = true)] + internal static partial int WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, bool bWaitAll, uint dwMilliseconds, bool bAlertable); - [DllImport(Interop.Libraries.OleAut32, PreserveSig = false)] - internal static extern void VariantClear(IntPtr pObject); + [GeneratedDllImport(Interop.Libraries.Kernel32/*, SetLastError=true*/)] + internal static partial int WaitForSingleObjectEx(IntPtr lpHandles, uint dwMilliseconds, bool bAlertable); internal sealed class Wrapper { @@ -86,7 +51,7 @@ private Wrapper() { } // SxS: clearing error information is considered safe internal static void ClearErrorInfo() { - SafeNativeMethods.SetErrorInfo(0, ADP.PtrZero); + Interop.OleAut32.SetErrorInfo(0, ADP.PtrZero); } } } diff --git a/src/libraries/System.Data.OleDb/src/System.Data.OleDb.csproj b/src/libraries/System.Data.OleDb/src/System.Data.OleDb.csproj index 912414debb420..62b3ffd01287c 100644 --- a/src/libraries/System.Data.OleDb/src/System.Data.OleDb.csproj +++ b/src/libraries/System.Data.OleDb/src/System.Data.OleDb.csproj @@ -34,6 +34,24 @@ System.Data.OleDb.OleDbTransaction + + + + + + + + + @@ -143,6 +161,7 @@ System.Data.OleDb.OleDbTransaction + diff --git a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbBuffer.cs b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbBuffer.cs index dc0db72fa5c6c..7ffbdea2af69c 100644 --- a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbBuffer.cs +++ b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbBuffer.cs @@ -12,17 +12,13 @@ namespace System.Data.ProviderBase // so that when debugging, we can tell the difference between one DbBuffer and another internal abstract class DbBuffer : SafeHandle { - internal const int LMEM_FIXED = 0x0000; - internal const int LMEM_MOVEABLE = 0x0002; - internal const int LMEM_ZEROINIT = 0x0040; - private readonly int _bufferLength; private DbBuffer(int initialSize, bool zeroBuffer) : base(IntPtr.Zero, true) { if (0 < initialSize) { - int flags = ((zeroBuffer) ? LMEM_ZEROINIT : LMEM_FIXED); + uint flags = ((zeroBuffer) ? Interop.Kernel32.LMEM_ZEROINIT : Interop.Kernel32.LMEM_FIXED); _bufferLength = initialSize; RuntimeHelpers.PrepareConstrainedRegions(); @@ -30,7 +26,7 @@ private DbBuffer(int initialSize, bool zeroBuffer) : base(IntPtr.Zero, true) { } finally { - base.handle = SafeNativeMethods.LocalAlloc(flags, (IntPtr)initialSize); + base.handle = Interop.Kernel32.LocalAlloc(flags, (nuint)initialSize); } if (IntPtr.Zero == base.handle) { @@ -358,7 +354,7 @@ protected override bool ReleaseHandle() base.handle = IntPtr.Zero; if (IntPtr.Zero != ptr) { - SafeNativeMethods.LocalFree(ptr); + Interop.Kernel32.LocalFree(ptr); } return true; } diff --git a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolCounters.cs b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolCounters.cs index 5a96e7d05f837..245209ce74842 100644 --- a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolCounters.cs +++ b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolCounters.cs @@ -236,7 +236,6 @@ protected DbConnectionPoolCounters(string? categoryName, string? categoryHelp) } // SxS: this method uses GetCurrentProcessId to construct the instance name. - // TODO: remove the Resource* attributes if you do not use GetCurrentProcessId after the fix private string GetInstanceName() { string? result = null; @@ -252,8 +251,7 @@ private string GetInstanceName() } } - // TODO: If you do not use GetCurrentProcessId after fixing VSDD 534795, please remove Resource* attributes from this method - int pid = SafeNativeMethods.GetCurrentProcessId(); + uint pid = Interop.Kernel32.GetCurrentProcessId(); // there are several characters which have special meaning // to PERFMON. They recommend that we translate them as shown below, to diff --git a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolIdentity.cs b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolIdentity.cs index 9fe3910b372a2..c0f8270865f52 100644 --- a/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolIdentity.cs +++ b/src/libraries/System.Data.OleDb/src/System/Data/ProviderBase/DbConnectionPoolIdentity.cs @@ -51,7 +51,7 @@ private static byte[] CreateWellKnownSid(WellKnownSidType sidType) // NOTE - We copied this code from System.Security.Principal.Win32.CreateWellKnownSid... - if (0 == UnsafeNativeMethods.CreateWellKnownSid((int)sidType, null, resultSid, ref length)) + if (0 == UnsafeNativeMethods.CreateWellKnownSid((int)sidType, null, out resultSid, ref length)) { IntegratedSecurityError(Win32_CreateWellKnownSid); } diff --git a/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.COMWrappers.cs b/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.COMWrappers.cs index 259b856952d31..d874526e2d97d 100644 --- a/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.COMWrappers.cs +++ b/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.COMWrappers.cs @@ -12,8 +12,8 @@ internal static partial class UnsafeNativeMethods // Oleaut32 // - [DllImport(Interop.Libraries.OleAut32)] - internal static unsafe extern OleDbHResult GetErrorInfo( + [GeneratedDllImport(Interop.Libraries.OleAut32)] + internal static unsafe partial OleDbHResult GetErrorInfo( int dwReserved, System.IntPtr* ppIErrorInfo); diff --git a/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.cs b/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.cs index 2453d6930a119..55d7aee68d182 100644 --- a/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.cs +++ b/src/libraries/System.Data.OleDb/src/UnsafeNativeMethods.cs @@ -832,11 +832,11 @@ internal Trustee(string name) } } - [DllImport(Interop.Libraries.Advapi32, EntryPoint = "CreateWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)] - internal static extern int CreateWellKnownSid( + [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "CreateWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)] + internal static partial int CreateWellKnownSid( int sidType, byte[]? domainSid, - [Out] byte[] resultSid, + [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] out byte[] resultSid, ref uint resultSidLength); } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ArrayMarshaller.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ArrayMarshaller.cs index 4e6980a80250f..ec42fcd838f84 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ArrayMarshaller.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/ArrayMarshaller.cs @@ -26,7 +26,7 @@ public ArrayMarshaller(IMarshallingGenerator manualMarshallingGenerator, TypeSyn public bool IsSupported(TargetFramework target, Version version) { - return target is TargetFramework.Net && version.Major >= 6; + return target is TargetFramework.Net && version.Major >= 7; } public ArgumentSyntax AsArgument(TypePositionInfo info, StubCodeContext context) diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs index 8ea0a12e1125c..844899ef7f247 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/AttributedMarshallingModelGeneratorFactory.cs @@ -47,6 +47,7 @@ public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext conte NativeMarshallingAttributeInfo marshalInfo => CreateCustomNativeTypeMarshaller(info, context, marshalInfo), BlittableTypeAttributeInfo => s_blittable, GeneratedNativeMarshallingAttributeInfo => s_forwarder, + MissingSupportMarshallingInfo => s_forwarder, _ => _innerMarshallingGenerator.Create(info, context) }; } diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs index 6924088f311fd..f5376fda0a85b 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/MarshallingAttributeInfo.cs @@ -31,6 +31,9 @@ private protected MarshallingInfo() { } } + /// + /// No marshalling information exists for the type. + /// public sealed record NoMarshallingInfo : MarshallingInfo { public static readonly MarshallingInfo Instance = new NoMarshallingInfo(); @@ -38,6 +41,21 @@ public sealed record NoMarshallingInfo : MarshallingInfo private NoMarshallingInfo() { } } + /// + /// Marshalling information is lacking because of support not because it is + /// unknown or non-existent. + /// + /// + /// An indication of "missing support" will trigger the fallback logic, which is + /// the forwarder marshaler. + /// + public sealed record MissingSupportMarshallingInfo : MarshallingInfo + { + public static readonly MarshallingInfo Instance = new MissingSupportMarshallingInfo(); + + private MissingSupportMarshallingInfo() { } + } + /// /// Character encoding enumeration. /// @@ -137,6 +155,7 @@ public sealed record SafeHandleMarshallingInfo(bool AccessibleDefaultConstructor /// /// User-applied System.Runtime.InteropServices.NativeMarshallingAttribute /// with a contiguous collection marshaller + /// public sealed record NativeContiguousCollectionMarshallingInfo( ManagedTypeInfo NativeMarshallingType, ManagedTypeInfo? ValuePropertyType, @@ -543,8 +562,8 @@ private MarshallingInfo CreateInfoFromMarshalAs( if (arrayMarshaller is null) { - // If the array marshaler type is not available, then we cannot marshal arrays. - return NoMarshallingInfo.Instance; + // If the array marshaler type is not available, then we cannot marshal arrays but indicate it is missing. + return MissingSupportMarshallingInfo.Instance; } ITypeSymbol? valuePropertyType = ManualTypeMarshallingHelper.FindValueProperty(arrayMarshaller)?.Type; @@ -741,9 +760,9 @@ private bool TryCreateTypeBasedMarshallingInfo( if (arrayMarshaller is null) { - // If the array marshaler type is not available, then we cannot marshal arrays. - marshallingInfo = NoMarshallingInfo.Instance; - return false; + // If the array marshaler type is not available, then we cannot marshal arrays but indicate it is missing. + marshallingInfo = MissingSupportMarshallingInfo.Instance; + return true; } ITypeSymbol? valuePropertyType = ManualTypeMarshallingHelper.FindValueProperty(arrayMarshaller)?.Type; diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs index aee1904683418..69148afc1da78 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs @@ -323,6 +323,15 @@ public static IEnumerable CodeSnippetsToValidateFallbackForwarder() yield return new object[] { code, TestTargetFramework.Framework, false }; } + // Confirm that all unsupported target frameworks fallback to a forwarder. + { + string code = CodeSnippets.BasicParametersAndModifiers(CodeSnippets.GeneratedDllImportAttributeDeclaration); + yield return new object[] { code, TestTargetFramework.Net5, true }; + yield return new object[] { code, TestTargetFramework.Core, true }; + yield return new object[] { code, TestTargetFramework.Standard, true }; + yield return new object[] { code, TestTargetFramework.Framework, true }; + } + // Confirm that all unsupported target frameworks fallback to a forwarder. { string code = CodeSnippets.BasicParametersAndModifiersWithCharSet(CharSet.Unicode, CodeSnippets.GeneratedDllImportAttributeDeclaration); diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs index ac5a28e780132..dd275fb1e6ebb 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs @@ -32,24 +32,27 @@ public static string ToStringAnsi(this IntPtr psz) } // Used for binary blobs without internal pointers. - public static byte[] GetMsgParamAsByteArray(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0) + public static unsafe byte[] GetMsgParamAsByteArray(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0) { int cbData = 0; - if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, null, ref cbData)) + if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, IntPtr.Zero, ref cbData)) throw Marshal.GetLastWin32Error().ToCryptographicException(); - byte[] pvData = new byte[cbData]; - if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData, ref cbData)) - throw Marshal.GetLastWin32Error().ToCryptographicException(); + byte[] data = new byte[cbData]; + fixed (byte* pvData = data) + { + if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData, ref cbData)) + throw Marshal.GetLastWin32Error().ToCryptographicException(); + } - return pvData.Resize(cbData); + return data.Resize(cbData); } // Used for binary blobs with internal pointers. public static SafeHandle GetMsgParamAsMemory(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0) { int cbData = 0; - if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, null, ref cbData)) + if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, IntPtr.Zero, ref cbData)) throw Marshal.GetLastWin32Error().ToCryptographicException(); SafeHandle pvData = SafeHeapAllocHandle.Alloc(cbData); @@ -330,7 +333,7 @@ public static CryptographicAttributeObjectCollection GetUnprotectedAttributes(th // For some reason, you can't ask how many attributes there are - you have to ask for the attributes and // get a CRYPT_E_ATTRIBUTES_MISSING failure if the count is 0. int cbUnprotectedAttr = 0; - if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_UNPROTECTED_ATTR_PARAM, 0, null, ref cbUnprotectedAttr)) + if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, CryptMsgParamType.CMSG_UNPROTECTED_ATTR_PARAM, 0, IntPtr.Zero, ref cbUnprotectedAttr)) { int lastError = Marshal.GetLastWin32Error(); if (lastError == (int)ErrorCode.CRYPT_E_ATTRIBUTES_MISSING) diff --git a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Interop.crypt32.cs b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Interop.crypt32.cs index 7ffd1bbce3277..7d0516af4af68 100644 --- a/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Interop.crypt32.cs +++ b/src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Windows/Native/Interop.crypt32.cs @@ -141,8 +141,8 @@ internal static SafeChainEngineHandle CertCreateCertificateChainEngine(ref CERT_ [GeneratedDllImport(Libraries.Crypt32, CharSet = CharSet.Unicode, SetLastError = true)] private static partial bool CertCreateCertificateChainEngine(ref CERT_CHAIN_ENGINE_CONFIG pConfig, out SafeChainEngineHandle hChainEngineHandle); - [DllImport(Libraries.Crypt32)] - public static extern void CertFreeCertificateChainEngine(IntPtr hChainEngine); + [GeneratedDllImport(Libraries.Crypt32)] + public static partial void CertFreeCertificateChainEngine(IntPtr hChainEngine); [GeneratedDllImport(Libraries.Crypt32, SetLastError = true)] public static unsafe partial bool CertGetCertificateChain(IntPtr hChainEngine, SafeCertContextHandle pCertContext, Interop.Crypt32.FILETIME* pTime, SafeCertStoreHandle hStore, ref CERT_CHAIN_PARA pChainPara, CertChainFlags dwFlags, IntPtr pvReserved, out SafeX509ChainHandle ppChainContext);