Removed dependencies on all x64 preprocessor indications and just made all structures work for both 32 and 64-bit (#346)

pull/363/head
David Hall 2022-12-21 20:33:41 -07:00
parent b2e972c77e
commit 532f43037b
13 changed files with 66 additions and 93 deletions

View File

@ -875,15 +875,12 @@ namespace Vanara.PInvoke
[In, Optional] IntPtr CallbackParam);
/// <summary/>
[StructLayout(LayoutKind.Sequential, Size =
#if x64
1232)]
#else
716)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct CONTEXT
{
private ulong f0;
// Structure size is 716 under x86 and 1232 under x64, not using arrays or unsafe to make blittable
private readonly int i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12, i13, i14, i15, i16, i17, i18, i19, i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, i30, i31, i32, i33, i34, i35, i36, i37, i38, i39, i40, i41, i42, i43, i44, i45, i46, i47, i48, i49, i50;
private readonly IntPtr p001, p002, p003, p004, p005, p006, p007, p008, p009, p010, p011, p012, p013, p014, p015, p016, p017, p018, p019, p020, p021, p022, p023, p024, p025, p026, p027, p028, p029, p030, p031, p032, p033, p034, p035, p036, p037, p038, p039, p040, p041, p042, p043, p044, p045, p046, p047, p048, p049, p050, p051, p052, p053, p054, p055, p056, p057, p058, p059, p060, p061, p062, p063, p064, p065, p066, p067, p068, p069, p070, p071, p072, p073, p074, p075, p076, p077, p078, p079, p080, p081, p082, p083, p084, p085, p086, p087, p088, p089, p090, p091, p092, p093, p094, p095, p096, p097, p098, p099, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119, p120, p121, p122, p123, p124, p125, p126, p127, p128, p129;
}
/// <summary></summary>

View File

@ -2,7 +2,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ProjectExtensions>
<SupportedDlls>DbgHelp.dll;ImageHlp.dll</SupportedDlls>
<UseX64>true</UseX64>
</ProjectExtensions>
<PropertyGroup>
<Description>PInvoke API (methods, structures and constants) imported from Windows DbgHelp.dll.</Description>

View File

@ -868,19 +868,16 @@ namespace Vanara.PInvoke
// _IP_ADAPTER_ANYCAST_ADDRESS_XP { union { ULONGLONG Alignment; struct { ULONG Length; DWORD Flags; }; }; struct
// _IP_ADAPTER_ANYCAST_ADDRESS_XP *Next; SOCKET_ADDRESS Address; } IP_ADAPTER_ANYCAST_ADDRESS_XP, *PIP_ADAPTER_ANYCAST_ADDRESS_XP;
[PInvokeData("iptypes.h", MSDNShortId = "2626fc86-e29b-4162-8625-207c709d67ed")]
[StructLayout(LayoutKind.Sequential, Pack = 8,
#if x64
Size = 32)]
#else
Size = 24)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct IP_ADAPTER_ANYCAST_ADDRESS : ILinkedListElement<IP_ADAPTER_ANYCAST_ADDRESS>
{
private long Alignment;
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>Specifies flags for this address.</summary>
public IP_ADAPTER_CAST_FLAGS Flags;
public IP_ADAPTER_CAST_FLAGS Flags { get => (IP_ADAPTER_CAST_FLAGS)Alignment.HighPart(); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), (int)value); }
/// <summary>
/// <para>Type: <c>struct _IP_ADAPTER_ANYCAST_ADDRESS*</c></para>
@ -927,19 +924,16 @@ namespace Vanara.PInvoke
// _IP_ADAPTER_DNS_SERVER_ADDRESS_XP { union { ULONGLONG Alignment; struct { ULONG Length; DWORD Reserved; }; }; struct
// _IP_ADAPTER_DNS_SERVER_ADDRESS_XP *Next; SOCKET_ADDRESS Address; } IP_ADAPTER_DNS_SERVER_ADDRESS_XP, *PIP_ADAPTER_DNS_SERVER_ADDRESS_XP;
[PInvokeData("iptypes.h", MSDNShortId = "96855386-9010-40df-8260-16b43ad6646f")]
[StructLayout(LayoutKind.Sequential, Pack = 8,
#if x64
Size = 32)]
#else
Size = 24)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct IP_ADAPTER_DNS_SERVER_ADDRESS : ILinkedListElement<IP_ADAPTER_DNS_SERVER_ADDRESS>
{
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
private long Alignment;
/// <summary>Reserved.</summary>
public uint Reserved;
/// <summary>Specifies the length of this structure.</summary>
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>Reserved</summary>
public int Reserved { get => Alignment.HighPart(); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), value); }
/// <summary>
/// <para>A pointer to the next DNS server address structure in the list.</para>
@ -1019,19 +1013,16 @@ namespace Vanara.PInvoke
// _IP_ADAPTER_GATEWAY_ADDRESS_LH { union { ULONGLONG Alignment; struct { ULONG Length; DWORD Reserved; }; }; struct
// _IP_ADAPTER_GATEWAY_ADDRESS_LH *Next; SOCKET_ADDRESS Address; } IP_ADAPTER_GATEWAY_ADDRESS_LH, *PIP_ADAPTER_GATEWAY_ADDRESS_LH;
[PInvokeData("iptypes.h", MSDNShortId = "CA38504A-1CC9-4ABA-BD4E-1B2EAD6F588B")]
[StructLayout(LayoutKind.Sequential, Pack = 8,
#if x64
Size = 32)]
#else
Size = 24)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct IP_ADAPTER_GATEWAY_ADDRESS : ILinkedListElement<IP_ADAPTER_GATEWAY_ADDRESS>
{
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
private long Alignment;
/// <summary>Reserved.</summary>
public uint Reserved;
/// <summary>Specifies the length of this structure.</summary>
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>Reserved</summary>
public int Reserved { get => Alignment.HighPart(); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), value); }
/// <summary>
/// <para>A pointer to the next gateway address structure in the list.</para>
@ -1302,19 +1293,16 @@ namespace Vanara.PInvoke
// _IP_ADAPTER_MULTICAST_ADDRESS_XP { union { ULONGLONG Alignment; struct { ULONG Length; DWORD Flags; }; }; struct
// _IP_ADAPTER_MULTICAST_ADDRESS_XP *Next; SOCKET_ADDRESS Address; } IP_ADAPTER_MULTICAST_ADDRESS_XP, *PIP_ADAPTER_MULTICAST_ADDRESS_XP;
[PInvokeData("iptypes.h", MSDNShortId = "b85a6e0a-df2c-4608-b07a-191b34440a43")]
[StructLayout(LayoutKind.Sequential, Pack = 8,
#if x64
Size = 32)]
#else
Size = 24)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct IP_ADAPTER_MULTICAST_ADDRESS : ILinkedListElement<IP_ADAPTER_MULTICAST_ADDRESS>
{
private long Alignment;
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>Specifies flags for this address.</summary>
public IP_ADAPTER_CAST_FLAGS Flags;
public IP_ADAPTER_CAST_FLAGS Flags { get => (IP_ADAPTER_CAST_FLAGS)Alignment.HighPart(); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), (int)value); }
/// <summary>
/// <para>Type: <c>struct _IP_ADAPTER_MULTICAST_ADDRESS*</c></para>
@ -1442,11 +1430,13 @@ namespace Vanara.PInvoke
[StructLayout(LayoutKind.Sequential)]
public struct IP_ADAPTER_UNICAST_ADDRESS : ILinkedListElement<IP_ADAPTER_UNICAST_ADDRESS>
{
private long Alignment;
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>This member is reserved and should be set to zero.</summary>
public uint Flags;
public uint Flags { get => unchecked((uint)Alignment.HighPart()); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), unchecked((int)value)); }
/// <summary>
/// <para>Type: <c>struct _IP_ADAPTER_UNICAST_ADDRESS*</c></para>
@ -1549,19 +1539,16 @@ namespace Vanara.PInvoke
// _IP_ADAPTER_WINS_SERVER_ADDRESS_LH { union { ULONGLONG Alignment; struct { ULONG Length; DWORD Reserved; }; }; struct
// _IP_ADAPTER_WINS_SERVER_ADDRESS_LH *Next; SOCKET_ADDRESS Address; } IP_ADAPTER_WINS_SERVER_ADDRESS_LH, *PIP_ADAPTER_WINS_SERVER_ADDRESS_LH;
[PInvokeData("iptypes.h", MSDNShortId = "AF9A40C4-63DB-4830-A689-1DFE4DC2CAB7")]
[StructLayout(LayoutKind.Sequential, Pack = 8,
#if x64
Size = 32)]
#else
Size = 24)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct IP_ADAPTER_WINS_SERVER_ADDRESS : ILinkedListElement<IP_ADAPTER_WINS_SERVER_ADDRESS>
{
/// <summary>Specifies the length of this structure.</summary>
public uint Length;
private long Alignment;
/// <summary>This member is reserved and should be set to zero.</summary>
public uint Reserved;
/// <summary>Specifies the length of this structure.</summary>
public uint Length { get => Alignment.LowPart(); set => Alignment = Macros.MAKELONG64(value, Alignment.HighPart()); }
/// <summary>Reserved</summary>
public int Reserved { get => Alignment.HighPart(); set => Alignment = Macros.MAKELONG64(Alignment.LowPart(), value); }
/// <summary>
/// <para>A pointer to the next WINS server address structure in the list.</para>

View File

@ -2,7 +2,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ProjectExtensions>
<SupportedDlls>iphlpapi.dll</SupportedDlls>
<UseX64>true</UseX64>
</ProjectExtensions>
<PropertyGroup>
<Description>PInvoke API (methods, structures and constants) imported from Windows IpHlpApi.dll.</Description>

View File

@ -3507,7 +3507,7 @@ namespace Vanara.PInvoke
private SP_DEVINFO_LIST_DETAIL_DATA GetDetail()
{
var data = StructHelper.InitWithSize<SP_DEVINFO_LIST_DETAIL_DATA>();
SP_DEVINFO_LIST_DETAIL_DATA data = new() { cbSize = IntPtr.Size == 4 ? 550U : (uint)Marshal.SizeOf(typeof(SP_DEVINFO_LIST_DETAIL_DATA)) };
Win32Error.ThrowLastErrorIfFalse(SetupDiGetDeviceInfoListDetail(handle, ref data));
return data;
}

View File

@ -2951,14 +2951,10 @@ namespace Vanara.PInvoke
// _SP_DEVINFO_LIST_DETAIL_DATA_W { DWORD cbSize; GUID ClassGuid; HANDLE RemoteMachineHandle; WCHAR
// RemoteMachineName[SP_MAX_MACHINENAME_LENGTH]; } SP_DEVINFO_LIST_DETAIL_DATA_W, *PSP_DEVINFO_LIST_DETAIL_DATA_W;
[PInvokeData("setupapi.h", MSDNShortId = "NS:setupapi._SP_DEVINFO_LIST_DETAIL_DATA_W")]
#if x64
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 8)]
#else
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
#endif
public struct SP_DEVINFO_LIST_DETAIL_DATA
{
/// <summary>The size, in bytes, of the SP_DEVINFO_LIST_DETAIL_DATA structure.</summary>
/// <summary>The size, in bytes, of the SP_DEVINFO_LIST_DETAIL_DATA structure. Set this value to 550 on 32-bit builds.</summary>
public uint cbSize;
/// <summary>
@ -2995,11 +2991,7 @@ namespace Vanara.PInvoke
// InstallMsgHandlerContext; HSPFILEQ FileQueue; ULONG_PTR ClassInstallReserved; DWORD Reserved; CHAR DriverPath[MAX_PATH]; }
// SP_DEVINSTALL_PARAMS_A, *PSP_DEVINSTALL_PARAMS_A;
[PInvokeData("setupapi.h", MSDNShortId = "NS:setupapi._SP_DEVINSTALL_PARAMS_A")]
#if x64
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 8)]
#else
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
#endif
public struct SP_DEVINSTALL_PARAMS
{
/// <summary>The size, in bytes, of the SP_DEVINSTALL_PARAMS structure.</summary>

View File

@ -2,7 +2,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ProjectExtensions>
<SupportedDlls>virtdisk.dll</SupportedDlls>
<UseX64>true</UseX64>
</ProjectExtensions>
<PropertyGroup>
<Description>PInvoke API (methods, structures and constants) imported from Windows VirtDisk.dll.</Description>

View File

@ -2,7 +2,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ProjectExtensions>
<SupportedDlls>ws2_32.dll</SupportedDlls>
<UseX64>true</UseX64>
</ProjectExtensions>
<PropertyGroup>
<Description>PInvoke API (methods, structures and constants) for WinSock and WinSock2 imported from Windows Ws2_32.dll.</Description>

View File

@ -337,12 +337,8 @@ public static partial class Ws2_32
[PInvokeData("ws2def.h")]
public static SizeT SIZEOF_SOCKET_ADDRESS_LIST(SizeT AddressCount) => Marshal.OffsetOf(typeof(SOCKET_ADDRESS_LIST), "Address").ToInt32() + Marshal.SizeOf(typeof(SOCKET_ADDRESS)) * AddressCount;
#if x64
public static readonly SizeT MAX_NATURAL_ALIGNMENT = sizeof(ulong);
#else
/// <summary>The maximum natural alignment</summary>
public static readonly SizeT MAX_NATURAL_ALIGNMENT = sizeof(uint);
#endif
public static readonly SizeT MAX_NATURAL_ALIGNMENT = IntPtr.Size;
[StructLayout(LayoutKind.Sequential)]
private struct AlignedStruct<T> where T : struct
@ -1707,19 +1703,16 @@ public static partial class Ws2_32
// https://docs.microsoft.com/en-us/windows/win32/api/ws2def/ns-ws2def-socket_address typedef struct _SOCKET_ADDRESS { LPSOCKADDR
// lpSockaddr; INT iSockaddrLength; } SOCKET_ADDRESS, *PSOCKET_ADDRESS, *LPSOCKET_ADDRESS;
[PInvokeData("ws2def.h", MSDNShortId = "37fbcb96-a859-4eca-8928-8051f95407b9")]
[StructLayout(LayoutKind.Sequential,
#if x64
Size = 16)]
#else
Size = 8)]
#endif
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct SOCKET_ADDRESS
{
/// <summary>A pointer to a socket address represented as a SOCKADDR structure.</summary>
public IntPtr lpSockaddr;
private IntPtr len;
/// <summary>The length, in bytes, of the socket address.</summary>
public int iSockaddrLength;
public int iSockaddrLength { get => len.ToInt32(); set => len = new(value); }
/// <summary>Gets the <see cref="SOCKADDR_INET"/> from this instance.</summary>
/// <returns>The <see cref="SOCKADDR_INET"/> value pointed to by this instance.</returns>

View File

@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.Collections;
using Vanara.Extensions;
@ -930,13 +931,6 @@ namespace Vanara.Diagnostics
/// <summary>Gets the setup classes available on the machine.</summary>
/// <value>A class that provides the collection of setup classes.</value>
public IEnumerable<DeviceClass> GetSetupClasses() => new DeviceClassCollection(0, MachineName);
internal static SP_DEVINFO_LIST_DETAIL_DATA GetDevInfoDetail(HDEVINFO hdi)
{
var disData = StructHelper.InitWithSize<SP_DEVINFO_LIST_DETAIL_DATA>();
Win32Error.ThrowLastErrorIfFalse(SetupDiGetDeviceInfoListDetail(hdi, ref disData));
return disData;
}
}
// TODO

View File

@ -36,6 +36,8 @@ public partial class IpHlpApiTests
{
foreach (var s in typeof(Vanara.PInvoke.IpHlpApi).GetNestedStructSizes())
TestContext.WriteLine(s);
Console.WriteLine($"Sz:{Marshal.SizeOf<IP_ADAPTER_ANYCAST_ADDRESS>()},Next:{Marshal.OffsetOf<IP_ADAPTER_ANYCAST_ADDRESS>("Next")},Add:{Marshal.OffsetOf<IP_ADAPTER_ANYCAST_ADDRESS>("Address")}");
}
[Test]

View File

@ -1,12 +1,7 @@
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.Collections;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Vanara.Extensions;
using Vanara.InteropServices;
using static Vanara.PInvoke.SetupAPI;
namespace Vanara.PInvoke.Tests
@ -22,6 +17,16 @@ namespace Vanara.PInvoke.Tests
[OneTimeTearDown]
public void _TearDown() => hDevInfo?.Dispose();
[Test]
public void _StructSizeTest()
{
//foreach (var s in typeof(Vanara.PInvoke.SetupAPI).GetNestedStructSizes())
// TestContext.WriteLine(s);
TestHelper.DumpStructSizeAndOffsets<SP_DEVINFO_LIST_DETAIL_DATA>();
TestHelper.DumpStructSizeAndOffsets<SP_DEVINSTALL_PARAMS>();
}
[Test]
public void SetupDiEnumDeviceInfoTest()
{

View File

@ -20,6 +20,13 @@ namespace Vanara.PInvoke.Tests
public static IPAddress localIP4 => Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
[Test]
public void _StructSizeTest()
{
foreach (var s in typeof(Vanara.PInvoke.Ws2_32).GetNestedStructSizes())
TestContext.WriteLine(s);
}
[Test]
public void ConstTest()
{