Skip to content

Commit

Permalink
Removed dependencies on all x64 preprocessor indications and just mad…
Browse files Browse the repository at this point in the history
…e all structures work for both 32 and 64-bit (#346)
  • Loading branch information
dahall committed Dec 22, 2022
1 parent b2e972c commit 532f430
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 90 deletions.
11 changes: 4 additions & 7 deletions PInvoke/DbgHelp/MiniDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,15 +875,12 @@ public enum THREAD_WRITE_FLAGS
[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>
Expand Down
1 change: 0 additions & 1 deletion PInvoke/DbgHelp/Vanara.PInvoke.DbgHelp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
77 changes: 32 additions & 45 deletions PInvoke/IpHlpApi/IpTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,19 +868,16 @@ public struct IP_ADAPTER_ADDRESSES : ILinkedListElement<IP_ADAPTER_ADDRESSES>
// _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>
Expand Down Expand Up @@ -927,19 +924,16 @@ public struct IP_ADAPTER_ANYCAST_ADDRESS : ILinkedListElement<IP_ADAPTER_ANYCAST
// _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>
{
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>Reserved.</summary>
public uint Reserved;
/// <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>
Expand Down Expand Up @@ -1019,19 +1013,16 @@ public struct IP_ADAPTER_DNS_SUFFIX : ILinkedListElement<IP_ADAPTER_DNS_SUFFIX>
// _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>
{
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>Reserved.</summary>
public uint Reserved;
/// <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>
Expand Down Expand Up @@ -1302,19 +1293,16 @@ public struct IP_ADAPTER_INFO : ILinkedListElement<IP_ADAPTER_INFO>
// _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>
Expand Down Expand Up @@ -1442,11 +1430,13 @@ public struct IP_ADAPTER_PREFIX : ILinkedListElement<IP_ADAPTER_PREFIX>
[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>
Expand Down Expand Up @@ -1549,19 +1539,16 @@ public struct IP_ADAPTER_UNICAST_ADDRESS : ILinkedListElement<IP_ADAPTER_UNICAST
// _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>
{
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 Reserved;
/// <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>
Expand Down
1 change: 0 additions & 1 deletion PInvoke/IpHlpApi/Vanara.PInvoke.IpHlpApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion PInvoke/SetupAPI/SetupAPI.DiFuncs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3507,7 +3507,7 @@ public void Adjust(Guid? classGuid, DIGCF flags = DIGCF.DIGCF_ALLCLASSES, string

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;
}
Expand Down
10 changes: 1 addition & 9 deletions PInvoke/SetupAPI/SetupAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2951,14 +2951,10 @@ public struct SP_DEVINFO_DATA
// _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>
Expand Down Expand Up @@ -2995,11 +2991,7 @@ public struct SP_DEVINFO_LIST_DETAIL_DATA
// 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>
Expand Down
1 change: 0 additions & 1 deletion PInvoke/VirtDisk/Vanara.PInvoke.VirtDisk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 0 additions & 1 deletion PInvoke/Ws2_32/Vanara.PInvoke.Ws2_32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
17 changes: 5 additions & 12 deletions PInvoke/Ws2_32/ws2def.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,8 @@ public enum SCOPE_LEVEL
[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
Expand Down Expand Up @@ -1707,19 +1703,16 @@ public struct SOCKADDR_STORAGE
// 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>
Expand Down
8 changes: 1 addition & 7 deletions System/Computer/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -930,13 +931,6 @@ public class DeviceManager
/// <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
Expand Down
2 changes: 2 additions & 0 deletions UnitTests/PInvoke/IpHlpApi/IpHlpApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void _StructSizeTest()
{
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]
Expand Down
15 changes: 10 additions & 5 deletions UnitTests/PInvoke/SetupAPI/SetupAPITests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +17,16 @@ public class SetupAPITests
[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()
{
Expand Down
7 changes: 7 additions & 0 deletions UnitTests/PInvoke/Ws2_32/Ws2tcpipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public class Ws2tcpipTests

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()
{
Expand Down

0 comments on commit 532f430

Please sign in to comment.