diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs index 39b03a093ec9c..4e21c7b81b49c 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/ForestTrustRelationshipInformation.cs @@ -187,7 +187,6 @@ public void Save() { throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error()); } - record.DomainInfo = new LSA_FOREST_TRUST_DOMAIN_INFO(); record.DomainInfo.sid = pSid; sidList.Add(pSid); record.DomainInfo.DNSNameBuffer = Marshal.StringToHGlobalUni(tmp.DnsName); @@ -214,7 +213,6 @@ public void Save() record.Time = (LARGE_INTEGER)_binaryDataTime[i]!; record.Data.Length = ((byte[])_binaryData[i]!).Length; record.ForestTrustType = (LSA_FOREST_TRUST_RECORD_TYPE)_binaryRecordType[i]!; - record.Data = new LSA_FOREST_TRUST_BINARY_DATA(); if (record.Data.Length == 0) { record.Data.Buffer = (IntPtr)0; @@ -317,7 +315,7 @@ public void Save() catch { throw; } } - private void GetForestTrustInfoHelper() + private unsafe void GetForestTrustInfoHelper() { IntPtr forestTrustInfo = (IntPtr)0; SafeLsaPolicyHandle? handle = null; @@ -381,7 +379,7 @@ private void GetForestTrustInfoHelper() if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustTopLevelName) { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.TopLevelName); + record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr; TopLevelName TLN = new TopLevelName(record.Flags, record.TopLevelName, record.Time); tmpTLNs.Add(TLN); } @@ -389,7 +387,7 @@ private void GetForestTrustInfoHelper() { // get the excluded TLN and put it in our collection IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.TopLevelName); + record.TopLevelName = *(global::Interop.UNICODE_STRING*)myPtr; string excludedName = Marshal.PtrToStringUni(record.TopLevelName.Buffer, record.TopLevelName.Length / 2); tmpExcludedTLNs.Add(excludedName); tmpExcludedNameTime.Add(excludedName, record.Time); @@ -397,14 +395,14 @@ private void GetForestTrustInfoHelper() else if (record.ForestTrustType == LSA_FOREST_TRUST_RECORD_TYPE.ForestTrustDomainInfo) { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.DomainInfo!); + record.DomainInfo = *(LSA_FOREST_TRUST_DOMAIN_INFO*)myPtr; ForestTrustDomainInformation dom = new ForestTrustDomainInformation(record.Flags, record.DomainInfo!, record.Time); tmpDomainInformation.Add(dom); } else { IntPtr myPtr = IntPtr.Add(addr, 16); - Marshal.PtrToStructure(myPtr, record.Data); + record.Data = *(LSA_FOREST_TRUST_BINARY_DATA*)myPtr; int length = record.Data.Length; byte[] byteArray = new byte[length]; if ((record.Data.Buffer != (IntPtr)0) && (length != 0)) diff --git a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs index 2b490f41c755b..82b09367f78ef 100644 --- a/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs +++ b/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/UnsafeNativeMethods.cs @@ -414,9 +414,9 @@ internal sealed class LSA_FOREST_TRUST_RECORD [FieldOffset(16)] public global::Interop.UNICODE_STRING TopLevelName; [FieldOffset(16)] - public LSA_FOREST_TRUST_BINARY_DATA Data = null!; + public LSA_FOREST_TRUST_BINARY_DATA Data; [FieldOffset(16)] - public LSA_FOREST_TRUST_DOMAIN_INFO? DomainInfo; + public LSA_FOREST_TRUST_DOMAIN_INFO DomainInfo; } [StructLayout(LayoutKind.Sequential)] @@ -432,8 +432,7 @@ public LARGE_INTEGER() } } - [StructLayout(LayoutKind.Sequential)] - internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO + internal struct LSA_FOREST_TRUST_DOMAIN_INFO { public IntPtr sid; public short DNSNameLength; @@ -444,8 +443,7 @@ internal sealed class LSA_FOREST_TRUST_DOMAIN_INFO public IntPtr NetBIOSNameBuffer; } - [StructLayout(LayoutKind.Sequential)] - internal sealed class LSA_FOREST_TRUST_BINARY_DATA + internal struct LSA_FOREST_TRUST_BINARY_DATA { public int Length; public IntPtr Buffer; diff --git a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs index 78c7e96a9fc3e..94e923e8f9d51 100644 --- a/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs +++ b/src/libraries/System.DirectoryServices/tests/System/DirectoryServices/DirectoryServicesTests.cs @@ -6,6 +6,7 @@ using System.Collections; using Xunit; using Xunit.Sdk; +using System.Reflection; namespace System.DirectoryServices.Tests { @@ -14,6 +15,13 @@ public partial class DirectoryServicesTests internal static bool IsLdapConfigurationExist => LdapConfiguration.Configuration != null; internal static bool IsActiveDirectoryServer => IsLdapConfigurationExist && LdapConfiguration.Configuration.IsActiveDirectoryServer; + [Fact] + public void TestGetAllTypes() + { + Type[] allTypes = typeof(DirectoryEntry).Assembly.GetTypes(); + Assert.Contains(typeof(DirectoryEntry), allTypes); + } + [ConditionalFact(nameof(IsLdapConfigurationExist))] public void TestOU() // adding and removing organization unit {