未验证 提交 6d6a3738 编写于 作者: J Jan Kotas 提交者: GitHub

Replace a few instances of PtrToStructure with more efficient marshalling (#70866)

上级 a78211be
...@@ -48,7 +48,7 @@ internal enum CryptOidInfoKeyType : int ...@@ -48,7 +48,7 @@ internal enum CryptOidInfoKeyType : int
CRYPT_OID_INFO_CNG_SIGN_KEY = 6, CRYPT_OID_INFO_CNG_SIGN_KEY = 6,
} }
internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups) internal static unsafe CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string key, OidGroup group, bool fallBackToAllGroups)
{ {
const OidGroup CRYPT_OID_DISABLE_SEARCH_DS_FLAG = unchecked((OidGroup)0x80000000); const OidGroup CRYPT_OID_DISABLE_SEARCH_DS_FLAG = unchecked((OidGroup)0x80000000);
Debug.Assert(key != null); Debug.Assert(key != null);
...@@ -75,28 +75,28 @@ internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string k ...@@ -75,28 +75,28 @@ internal static CRYPT_OID_INFO FindOidInfo(CryptOidInfoKeyType keyType, string k
if (!OidGroupWillNotUseActiveDirectory(group)) if (!OidGroupWillNotUseActiveDirectory(group))
{ {
OidGroup localGroup = group | CRYPT_OID_DISABLE_SEARCH_DS_FLAG; OidGroup localGroup = group | CRYPT_OID_DISABLE_SEARCH_DS_FLAG;
IntPtr localOidInfo = CryptFindOIDInfo(keyType, rawKey, localGroup); CRYPT_OID_INFO* localOidInfo = CryptFindOIDInfo(keyType, rawKey, localGroup);
if (localOidInfo != IntPtr.Zero) if (localOidInfo != null)
{ {
return Marshal.PtrToStructure<CRYPT_OID_INFO>(localOidInfo); return *localOidInfo;
} }
} }
// Attempt to query with a specific group, to make try to avoid an AD lookup if possible // Attempt to query with a specific group, to make try to avoid an AD lookup if possible
IntPtr fullOidInfo = CryptFindOIDInfo(keyType, rawKey, group); CRYPT_OID_INFO* fullOidInfo = CryptFindOIDInfo(keyType, rawKey, group);
if (fullOidInfo != IntPtr.Zero) if (fullOidInfo != null)
{ {
return Marshal.PtrToStructure<CRYPT_OID_INFO>(fullOidInfo); return *fullOidInfo;
} }
if (fallBackToAllGroups && group != OidGroup.All) if (fallBackToAllGroups && group != OidGroup.All)
{ {
// Finally, for compatibility with previous runtimes, if we have a group specified retry the // Finally, for compatibility with previous runtimes, if we have a group specified retry the
// query with no group // query with no group
IntPtr allGroupOidInfo = CryptFindOIDInfo(keyType, rawKey, OidGroup.All); CRYPT_OID_INFO* allGroupOidInfo = CryptFindOIDInfo(keyType, rawKey, OidGroup.All);
if (allGroupOidInfo != IntPtr.Zero) if (allGroupOidInfo != null)
{ {
return Marshal.PtrToStructure<CRYPT_OID_INFO>(allGroupOidInfo); return *allGroupOidInfo;
} }
} }
...@@ -125,6 +125,6 @@ private static bool OidGroupWillNotUseActiveDirectory(OidGroup group) ...@@ -125,6 +125,6 @@ private static bool OidGroupWillNotUseActiveDirectory(OidGroup group)
} }
[LibraryImport(Interop.Libraries.Crypt32)] [LibraryImport(Interop.Libraries.Crypt32)]
private static partial IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group); private static unsafe partial CRYPT_OID_INFO* CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, IntPtr pvKey, OidGroup group);
} }
} }
...@@ -13,23 +13,18 @@ internal static partial class Crypt32 ...@@ -13,23 +13,18 @@ internal static partial class Crypt32
/// Version used for a buffer containing a scalar integer (not an IntPtr) /// Version used for a buffer containing a scalar integer (not an IntPtr)
/// </summary> /// </summary>
[LibraryImport(Libraries.Crypt32)] [LibraryImport(Libraries.Crypt32)]
private static unsafe partial IntPtr CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, int* pvKey, OidGroup group); private static unsafe partial CRYPT_OID_INFO* CryptFindOIDInfo(CryptOidInfoKeyType dwKeyType, void* pvKey, OidGroup group);
public static CRYPT_OID_INFO FindAlgIdOidInfo(Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM algId) public static unsafe CRYPT_OID_INFO FindAlgIdOidInfo(Interop.BCrypt.ECC_CURVE_ALG_ID_ENUM algId)
{ {
int intAlgId = (int)algId; CRYPT_OID_INFO* fullOidInfo = CryptFindOIDInfo(
IntPtr fullOidInfo;
unsafe
{
fullOidInfo = CryptFindOIDInfo(
CryptOidInfoKeyType.CRYPT_OID_INFO_ALGID_KEY, CryptOidInfoKeyType.CRYPT_OID_INFO_ALGID_KEY,
&intAlgId, &algId,
OidGroup.HashAlgorithm); OidGroup.HashAlgorithm);
}
if (fullOidInfo != IntPtr.Zero) if (fullOidInfo != null)
{ {
return Marshal.PtrToStructure<CRYPT_OID_INFO>(fullOidInfo); return *fullOidInfo;
} }
// Otherwise the lookup failed. // Otherwise the lookup failed.
......
...@@ -76,24 +76,22 @@ internal IPAddress MarshalIPAddress() ...@@ -76,24 +76,22 @@ internal IPAddress MarshalIPAddress()
// IP_ADAPTER_WINS_SERVER_ADDRESS // IP_ADAPTER_WINS_SERVER_ADDRESS
// IP_ADAPTER_GATEWAY_ADDRESS // IP_ADAPTER_GATEWAY_ADDRESS
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct IpAdapterAddress internal unsafe struct IpAdapterAddress
{ {
internal uint length; internal uint length;
internal AdapterAddressFlags flags; internal AdapterAddressFlags flags;
internal IntPtr next; internal IpAdapterAddress* next;
internal IpSocketAddress address; internal IpSocketAddress address;
internal static InternalIPAddressCollection MarshalIpAddressCollection(IntPtr ptr) internal static InternalIPAddressCollection MarshalIpAddressCollection(IntPtr ptr)
{ {
InternalIPAddressCollection addressList = new InternalIPAddressCollection(); InternalIPAddressCollection addressList = new InternalIPAddressCollection();
while (ptr != IntPtr.Zero) IpAdapterAddress* pIpAdapterAddress = (IpAdapterAddress*)ptr;
while (pIpAdapterAddress != null)
{ {
IpAdapterAddress addressStructure = Marshal.PtrToStructure<IpAdapterAddress>(ptr); addressList.InternalAdd(pIpAdapterAddress->address.MarshalIPAddress());
IPAddress address = addressStructure.address.MarshalIPAddress(); pIpAdapterAddress = pIpAdapterAddress->next;
addressList.InternalAdd(address);
ptr = addressStructure.next;
} }
return addressList; return addressList;
...@@ -103,13 +101,12 @@ internal static IPAddressInformationCollection MarshalIpAddressInformationCollec ...@@ -103,13 +101,12 @@ internal static IPAddressInformationCollection MarshalIpAddressInformationCollec
{ {
IPAddressInformationCollection addressList = new IPAddressInformationCollection(); IPAddressInformationCollection addressList = new IPAddressInformationCollection();
while (ptr != IntPtr.Zero) IpAdapterAddress* pIpAdapterAddress = (IpAdapterAddress*)ptr;
while (pIpAdapterAddress != null)
{ {
IpAdapterAddress addressStructure = Marshal.PtrToStructure<IpAdapterAddress>(ptr); addressList.InternalAdd(new SystemIPAddressInformation(
IPAddress address = addressStructure.address.MarshalIPAddress(); pIpAdapterAddress->address.MarshalIPAddress(), pIpAdapterAddress->flags));
addressList.InternalAdd(new SystemIPAddressInformation(address, addressStructure.flags)); pIpAdapterAddress = pIpAdapterAddress->next;
ptr = addressStructure.next;
} }
return addressList; return addressList;
...@@ -117,11 +114,11 @@ internal static IPAddressInformationCollection MarshalIpAddressInformationCollec ...@@ -117,11 +114,11 @@ internal static IPAddressInformationCollection MarshalIpAddressInformationCollec
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
internal struct IpAdapterUnicastAddress internal unsafe struct IpAdapterUnicastAddress
{ {
internal uint length; internal uint length;
internal AdapterAddressFlags flags; internal AdapterAddressFlags flags;
internal IntPtr next; internal IpAdapterUnicastAddress* next;
internal IpSocketAddress address; internal IpSocketAddress address;
internal PrefixOrigin prefixOrigin; internal PrefixOrigin prefixOrigin;
internal SuffixOrigin suffixOrigin; internal SuffixOrigin suffixOrigin;
...@@ -132,37 +129,59 @@ internal struct IpAdapterUnicastAddress ...@@ -132,37 +129,59 @@ internal struct IpAdapterUnicastAddress
internal byte prefixLength; internal byte prefixLength;
} }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] [StructLayout(LayoutKind.Sequential)]
internal struct IpAdapterAddresses internal unsafe struct IpAdapterAddresses
{ {
internal const int MAX_ADAPTER_ADDRESS_LENGTH = 8; internal const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
internal uint length; internal uint length;
internal uint index; internal uint index;
internal IntPtr next; internal IpAdapterAddresses* next;
// Needs to be ANSI. private IntPtr _adapterName; // ANSI string
[MarshalAs(UnmanagedType.LPStr)] internal string AdapterName => Marshal.PtrToStringAnsi(_adapterName)!;
internal string AdapterName;
internal IntPtr firstUnicastAddress; internal IntPtr firstUnicastAddress;
internal IntPtr firstAnycastAddress; internal IntPtr firstAnycastAddress;
internal IntPtr firstMulticastAddress; internal IntPtr firstMulticastAddress;
internal IntPtr firstDnsServerAddress; internal IntPtr firstDnsServerAddress;
internal string dnsSuffix; private IntPtr _dnsSuffix;
internal string description; internal string DnsSuffix => Marshal.PtrToStringUni(_dnsSuffix)!;
internal string friendlyName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_ADAPTER_ADDRESS_LENGTH)] private IntPtr _description;
internal byte[] address; internal string Description => Marshal.PtrToStringUni(_description)!;
internal uint addressLength;
private IntPtr _friendlyName;
internal string FriendlyName => Marshal.PtrToStringUni(_friendlyName)!;
private fixed byte _address[MAX_ADAPTER_ADDRESS_LENGTH];
private uint _addressLength;
internal byte[] Address
{
get
{
fixed (byte* pAddress = _address)
return new ReadOnlySpan<byte>(pAddress, (int)_addressLength).ToArray();
}
}
internal AdapterFlags flags; internal AdapterFlags flags;
internal uint mtu; internal uint mtu;
internal NetworkInterfaceType type; internal NetworkInterfaceType type;
internal OperationalStatus operStatus; internal OperationalStatus operStatus;
internal uint ipv6Index; internal uint ipv6Index;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
internal uint[] zoneIndices; private fixed uint _zoneIndices[16];
internal uint[] ZoneIndices
{
get
{
fixed (uint* pZoneIndices = _zoneIndices)
return new ReadOnlySpan<uint>(pZoneIndices, 16).ToArray();
}
}
internal IntPtr firstPrefix; internal IntPtr firstPrefix;
internal ulong transmitLinkSpeed; internal ulong transmitLinkSpeed;
...@@ -174,13 +193,11 @@ internal struct IpAdapterAddresses ...@@ -174,13 +193,11 @@ internal struct IpAdapterAddresses
internal ulong luid; internal ulong luid;
internal IpSocketAddress dhcpv4Server; internal IpSocketAddress dhcpv4Server;
internal uint compartmentId; internal uint compartmentId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] internal fixed byte networkGuid[16];
internal byte[] networkGuid;
internal InterfaceConnectionType connectionType; internal InterfaceConnectionType connectionType;
internal InterfaceTunnelType tunnelType; internal InterfaceTunnelType tunnelType;
internal IpSocketAddress dhcpv6Server; // Never available in Windows. internal IpSocketAddress dhcpv6Server; // Never available in Windows.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 130)] internal fixed byte dhcpv6ClientDuid[130];
internal byte[] dhcpv6ClientDuid;
internal uint dhcpv6ClientDuidLength; internal uint dhcpv6ClientDuidLength;
internal uint dhcpV6Iaid; internal uint dhcpV6Iaid;
...@@ -211,11 +228,11 @@ internal enum InterfaceTunnelType : int ...@@ -211,11 +228,11 @@ internal enum InterfaceTunnelType : int
/// <summary> /// <summary>
/// IP_PER_ADAPTER_INFO - per-adapter IP information such as DNS server list. /// IP_PER_ADAPTER_INFO - per-adapter IP information such as DNS server list.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] [StructLayout(LayoutKind.Sequential)]
internal struct IpPerAdapterInfo internal struct IpPerAdapterInfo
{ {
internal bool autoconfigEnabled; internal uint autoconfigEnabled;
internal bool autoconfigActive; internal uint autoconfigActive;
internal IntPtr currentDnsServer; /* IpAddressList* */ internal IntPtr currentDnsServer; /* IpAddressList* */
internal IpAddrString dnsServerList; internal IpAddrString dnsServerList;
}; };
...@@ -224,14 +241,12 @@ internal struct IpPerAdapterInfo ...@@ -224,14 +241,12 @@ internal struct IpPerAdapterInfo
/// Store an IP address with its corresponding subnet mask, /// Store an IP address with its corresponding subnet mask,
/// both as dotted decimal strings. /// both as dotted decimal strings.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] [StructLayout(LayoutKind.Sequential)]
internal struct IpAddrString internal unsafe struct IpAddrString
{ {
internal IntPtr Next; /* struct _IpAddressList* */ internal IpAddrString* Next; /* struct _IpAddressList* */
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] internal fixed byte IpAddress[16];
internal string IpAddress; internal fixed byte IpMask[16];
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
internal string IpMask;
internal uint Context; internal uint Context;
} }
......
...@@ -68,11 +68,11 @@ private static string ConvertTo8DigitHex(uint value) ...@@ -68,11 +68,11 @@ private static string ConvertTo8DigitHex(uint value)
return value.ToString("X8"); return value.ToString("X8");
} }
private static Interop.Version.VS_FIXEDFILEINFO GetFixedFileInfo(IntPtr memPtr) private static unsafe Interop.Version.VS_FIXEDFILEINFO GetFixedFileInfo(IntPtr memPtr)
{ {
if (Interop.Version.VerQueryValue(memPtr, "\\", out IntPtr memRef, out _)) if (Interop.Version.VerQueryValue(memPtr, "\\", out IntPtr memRef, out _))
{ {
return (Interop.Version.VS_FIXEDFILEINFO)Marshal.PtrToStructure<Interop.Version.VS_FIXEDFILEINFO>(memRef); return *(Interop.Version.VS_FIXEDFILEINFO*)memRef;
} }
return default; return default;
......
...@@ -29,7 +29,7 @@ internal sealed class SystemIPInterfaceProperties : IPInterfaceProperties ...@@ -29,7 +29,7 @@ internal sealed class SystemIPInterfaceProperties : IPInterfaceProperties
internal SystemIPInterfaceProperties(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) internal SystemIPInterfaceProperties(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses)
{ {
_adapterFlags = ipAdapterAddresses.flags; _adapterFlags = ipAdapterAddresses.flags;
_dnsSuffix = ipAdapterAddresses.dnsSuffix; _dnsSuffix = ipAdapterAddresses.DnsSuffix;
_dnsEnabled = fixedInfo.enableDns; _dnsEnabled = fixedInfo.enableDns;
_dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0); _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0);
...@@ -64,7 +64,7 @@ internal SystemIPInterfaceProperties(in Interop.IpHlpApi.FIXED_INFO fixedInfo, i ...@@ -64,7 +64,7 @@ internal SystemIPInterfaceProperties(in Interop.IpHlpApi.FIXED_INFO fixedInfo, i
if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0) if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0)
{ {
_ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index,
ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); ipAdapterAddresses.mtu, ipAdapterAddresses.ZoneIndices);
} }
} }
......
...@@ -91,11 +91,10 @@ private unsafe void GetPerAdapterInfo(uint index) ...@@ -91,11 +91,10 @@ private unsafe void GetPerAdapterInfo(uint index)
result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, &size); result = Interop.IpHlpApi.GetPerAdapterInfo(index, buffer, &size);
if (result == Interop.IpHlpApi.ERROR_SUCCESS) if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{ {
Interop.IpHlpApi.IpPerAdapterInfo ipPerAdapterInfo = Interop.IpHlpApi.IpPerAdapterInfo* ipPerAdapterInfo = (Interop.IpHlpApi.IpPerAdapterInfo*)buffer;
Marshal.PtrToStructure<Interop.IpHlpApi.IpPerAdapterInfo>(buffer);
_autoConfigEnabled = ipPerAdapterInfo.autoconfigEnabled; _autoConfigEnabled = ipPerAdapterInfo->autoconfigEnabled != 0;
_autoConfigActive = ipPerAdapterInfo.autoconfigActive; _autoConfigActive = ipPerAdapterInfo->autoconfigActive != 0;
} }
} }
finally finally
......
...@@ -15,7 +15,6 @@ internal sealed class SystemNetworkInterface : NetworkInterface ...@@ -15,7 +15,6 @@ internal sealed class SystemNetworkInterface : NetworkInterface
private readonly string _id; private readonly string _id;
private readonly string _description; private readonly string _description;
private readonly byte[] _physicalAddress; private readonly byte[] _physicalAddress;
private readonly uint _addressLength;
private readonly NetworkInterfaceType _type; private readonly NetworkInterfaceType _type;
private readonly OperationalStatus _operStatus; private readonly OperationalStatus _operStatus;
private readonly long _speed; private readonly long _speed;
...@@ -110,14 +109,12 @@ internal static unsafe NetworkInterface[] GetNetworkInterfaces() ...@@ -110,14 +109,12 @@ internal static unsafe NetworkInterface[] GetNetworkInterfaces()
if (result == Interop.IpHlpApi.ERROR_SUCCESS) if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{ {
// Linked list of interfaces. // Linked list of interfaces.
IntPtr ptr = buffer; Interop.IpHlpApi.IpAdapterAddresses* adapterAddresses = (Interop.IpHlpApi.IpAdapterAddresses*)buffer;
while (ptr != IntPtr.Zero) while (adapterAddresses != null)
{ {
// Traverse the list, marshal in the native structures, and create new NetworkInterfaces. // Traverse the list, marshal in the native structures, and create new NetworkInterfaces.
Interop.IpHlpApi.IpAdapterAddresses adapterAddresses = Marshal.PtrToStructure<Interop.IpHlpApi.IpAdapterAddresses>(ptr); interfaceList.Add(new SystemNetworkInterface(in fixedInfo, in *adapterAddresses));
interfaceList.Add(new SystemNetworkInterface(in fixedInfo, in adapterAddresses)); adapterAddresses = adapterAddresses->next;
ptr = adapterAddresses.next;
} }
} }
} }
...@@ -146,12 +143,11 @@ internal SystemNetworkInterface(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Int ...@@ -146,12 +143,11 @@ internal SystemNetworkInterface(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Int
{ {
// Store the common API information. // Store the common API information.
_id = ipAdapterAddresses.AdapterName; _id = ipAdapterAddresses.AdapterName;
_name = ipAdapterAddresses.friendlyName; _name = ipAdapterAddresses.FriendlyName;
_description = ipAdapterAddresses.description; _description = ipAdapterAddresses.Description;
_index = ipAdapterAddresses.index; _index = ipAdapterAddresses.index;
_physicalAddress = ipAdapterAddresses.address; _physicalAddress = ipAdapterAddresses.Address;
_addressLength = ipAdapterAddresses.addressLength;
_type = ipAdapterAddresses.type; _type = ipAdapterAddresses.type;
_operStatus = ipAdapterAddresses.operStatus; _operStatus = ipAdapterAddresses.operStatus;
...@@ -172,12 +168,7 @@ internal SystemNetworkInterface(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Int ...@@ -172,12 +168,7 @@ internal SystemNetworkInterface(in Interop.IpHlpApi.FIXED_INFO fixedInfo, in Int
public override PhysicalAddress GetPhysicalAddress() public override PhysicalAddress GetPhysicalAddress()
{ {
byte[] newAddr = new byte[_addressLength]; return new PhysicalAddress(_physicalAddress);
// Buffer.BlockCopy only supports int while addressLength is uint (see IpAdapterAddresses).
// Will throw OverflowException if addressLength > Int32.MaxValue.
Buffer.BlockCopy(_physicalAddress, 0, newAddr, 0, checked((int)_addressLength));
return new PhysicalAddress(newAddr);
} }
public override NetworkInterfaceType NetworkInterfaceType { get { return _type; } } public override NetworkInterfaceType NetworkInterfaceType { get { return _type; } }
......
...@@ -20,7 +20,7 @@ internal sealed class SystemUnicastIPAddressInformation : UnicastIPAddressInform ...@@ -20,7 +20,7 @@ internal sealed class SystemUnicastIPAddressInformation : UnicastIPAddressInform
private readonly uint _preferredLifetime; private readonly uint _preferredLifetime;
private readonly byte _prefixLength; private readonly byte _prefixLength;
internal SystemUnicastIPAddressInformation(Interop.IpHlpApi.IpAdapterUnicastAddress adapterAddress) internal SystemUnicastIPAddressInformation(in Interop.IpHlpApi.IpAdapterUnicastAddress adapterAddress)
{ {
IPAddress ipAddress = adapterAddress.address.MarshalIPAddress(); IPAddress ipAddress = adapterAddress.address.MarshalIPAddress();
_innerInfo = new SystemIPAddressInformation(ipAddress, adapterAddress.flags); _innerInfo = new SystemIPAddressInformation(ipAddress, adapterAddress.flags);
...@@ -135,15 +135,15 @@ public override long DhcpLeaseLifetime ...@@ -135,15 +135,15 @@ public override long DhcpLeaseLifetime
} }
// Helper method that marshals the address information into the classes. // Helper method that marshals the address information into the classes.
internal static UnicastIPAddressInformationCollection MarshalUnicastIpAddressInformationCollection(IntPtr ptr) internal static unsafe UnicastIPAddressInformationCollection MarshalUnicastIpAddressInformationCollection(IntPtr ptr)
{ {
UnicastIPAddressInformationCollection addressList = new UnicastIPAddressInformationCollection(); UnicastIPAddressInformationCollection addressList = new UnicastIPAddressInformationCollection();
while (ptr != IntPtr.Zero) Interop.IpHlpApi.IpAdapterUnicastAddress* pIpAdapterAddress = (Interop.IpHlpApi.IpAdapterUnicastAddress*)ptr;
while (pIpAdapterAddress != null)
{ {
Interop.IpHlpApi.IpAdapterUnicastAddress addr = Marshal.PtrToStructure<Interop.IpHlpApi.IpAdapterUnicastAddress>(ptr); addressList.InternalAdd(new SystemUnicastIPAddressInformation(in *pIpAdapterAddress));
addressList.InternalAdd(new SystemUnicastIPAddressInformation(addr)); pIpAdapterAddress = pIpAdapterAddress->next;
ptr = addr.next;
} }
return addressList; return addressList;
......
...@@ -1034,8 +1034,8 @@ private static unsafe IdentityReferenceCollection TranslateToNTAccounts(Identity ...@@ -1034,8 +1034,8 @@ private static unsafe IdentityReferenceCollection TranslateToNTAccounts(Identity
for (int i = 0; i < rdl.Entries; i++) for (int i = 0; i < rdl.Entries; i++)
{ {
Interop.LSA_TRUST_INFORMATION ti = (Interop.LSA_TRUST_INFORMATION)Marshal.PtrToStructure<Interop.LSA_TRUST_INFORMATION>(new IntPtr((long)rdl.Domains + i * Marshal.SizeOf<Interop.LSA_TRUST_INFORMATION>())); Interop.LSA_TRUST_INFORMATION* ti = (Interop.LSA_TRUST_INFORMATION*)rdl.Domains + i;
ReferencedDomains[i] = Marshal.PtrToStringUni(ti.Name.Buffer, ti.Name.Length / sizeof(char)); ReferencedDomains[i] = Marshal.PtrToStringUni(ti->Name.Buffer, ti->Name.Length / sizeof(char));
} }
Interop.LSA_TRANSLATED_NAME[] translatedNames = new Interop.LSA_TRANSLATED_NAME[sourceSids.Count]; Interop.LSA_TRANSLATED_NAME[] translatedNames = new Interop.LSA_TRANSLATED_NAME[sourceSids.Count];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册