From fb607a799eb2c30cb96528ecf6e5510714fe252d Mon Sep 17 00:00:00 2001 From: dahall Date: Wed, 4 May 2022 10:59:33 -0600 Subject: [PATCH] Fixed #296 - invalid `MIB_UDP6ROW_OWNER_PID.dwOwningPid` value when calling `GetExtendedUdpTable` --- PInvoke/IpHlpApi/UdpMib.cs | 10 +++------- PInvoke/Shared/InteropServices/SafeElementArray.cs | 4 ++-- UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs | 2 ++ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/PInvoke/IpHlpApi/UdpMib.cs b/PInvoke/IpHlpApi/UdpMib.cs index 0d9b0026..6a3f27c0 100644 --- a/PInvoke/IpHlpApi/UdpMib.cs +++ b/PInvoke/IpHlpApi/UdpMib.cs @@ -210,7 +210,7 @@ namespace Vanara.PInvoke // _MIB_UDP6ROW_OWNER_PID { UCHAR ucLocalAddr[16]; DWORD dwLocalScopeId; DWORD dwLocalPort; DWORD dwOwningPid; } // MIB_UDP6ROW_OWNER_PID, *PMIB_UDP6ROW_OWNER_PID; [PInvokeData("udpmib.h", MSDNShortId = "d3d02485-381b-4058-b4b9-0a2c9c365f43")] - [StructLayout(LayoutKind.Sequential)] + [StructLayout(LayoutKind.Sequential, Size = 28, Pack = 4)] public struct MIB_UDP6ROW_OWNER_PID { /// @@ -697,14 +697,10 @@ namespace Vanara.PInvoke { } - /// - /// The number of MIB_UDP6ROW_OWNER_PID elements in table. - /// + /// The number of MIB_UDP6ROW_OWNER_PID elements in table. public uint dwNumEntries => Count; - /// - /// An array of MIB_UDP6ROW_OWNER_PID structures returned by a call to GetExtendedUdpTable. - /// + /// An array of MIB_UDP6ROW_OWNER_PID structures returned by a call to GetExtendedUdpTable. public MIB_UDP6ROW_OWNER_PID[] table { get => Elements; set => Elements = value; } /// Performs an implicit conversion from to . diff --git a/PInvoke/Shared/InteropServices/SafeElementArray.cs b/PInvoke/Shared/InteropServices/SafeElementArray.cs index d64777a8..f16d4d22 100644 --- a/PInvoke/Shared/InteropServices/SafeElementArray.cs +++ b/PInvoke/Shared/InteropServices/SafeElementArray.cs @@ -16,8 +16,8 @@ namespace Vanara.InteropServices /// The memory methods to use for allocation. public class SafeElementArray : SafeMemoryHandle, IEnumerable where TMem : IMemoryMethods, new() where TElem : struct where TPrefix : IConvertible { - private static readonly int ElemSize = Marshal.SizeOf(typeof(TElem)); - private static readonly int PrefixSize = Marshal.SizeOf(typeof(TPrefix)); + private static readonly int ElemSize = InteropExtensions.SizeOf(typeof(TElem)); + private static readonly int PrefixSize = InteropExtensions.SizeOf(typeof(TPrefix)); /// Initializes a new instance of the class. protected SafeElementArray() : this(0) { } diff --git a/UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs b/UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs index 22294b03..ae1dd785 100644 --- a/UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs +++ b/UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs @@ -277,8 +277,10 @@ namespace Vanara.PInvoke.Tests Assert.That(t5.dwNumEntries, Is.GreaterThan(0)); var t10 = GetExtendedUdpTable(UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID); Assert.That(t10.dwNumEntries, Is.GreaterThan(0)); + TestContext.WriteLine("UDP PID: " + string.Join(",", t10.Select(t => t.dwOwningPid))); var t11 = GetExtendedUdpTable(UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, ADDRESS_FAMILY.AF_INET6); Assert.That(t11.dwNumEntries, Is.GreaterThan(0)); + TestContext.WriteLine("UDP6 PID: " + string.Join(",", t11.Select(t => t.dwOwningPid))); }, Throws.Nothing); Assert.That(() => GetExtendedUdpTable(UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE), Throws.InvalidOperationException); Assert.That(() => GetExtendedUdpTable(UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE), Throws.InvalidOperationException);